This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.


This topic contains 1 reply, has 2 voices.

Last updated by Vincenzo 1 year, 4 months ago.

Assigned support staff: Vincenzo.

Author Posts
January 17, 2019 at 7:47 am #3100683



I have a friend that made a product stocks report plugin. the basic idea is to send the products stocks each day via email and the reports will display the initial stock of the product, how much was purchased in the last amount of time and stock that is left.
Everything works fine until the part when orders are being deleted manually or crated manually on the admin side, then suddenly the initial stock getting greather than is was initialy and the report is messed up.
As my friend thinks, this is something to do with the stock calculation on the translated language but he cant figure this out, so we though to ask if there is some hook or function that is responsible for the translated stocks calculation?

This is how he explained his function:

1. First of all, I figure out how many products are available in the postmeta table, "_stock" field ( using the static value from the database ).
2. Searching for all orders with this product to figure out how many were purchased ( since there is no static number in the system ).
3. Then I calculate the sum of these 2 values ( item 1 and 2 ) to get the first stock.

the code:

public function getStockLeft($id)
if ($this->stockLeft) {
return (isset($this->stockLeft[$id])) ? $this->stockLeft[$id] : '';

$r = $this->wpdb->get_results("select p.ID, pm.meta_value
from " . $this->wpdb->prefix . "posts as p, " . $this->wpdb->prefix . "postmeta as pm
where p.post_type = 'product' and pm.post_id = p.ID
and pm.meta_key = '_stock' ;", ARRAY_A);

if ($r) {
foreach ($r as $value) {
$this->stockLeft[$value['ID']] = $value['meta_value'];

return (isset($this->stockLeft[$id])) ? $this->stockLeft[$id] : '';


public function getStockPurchased($id)
if ($this->stockPurchased) {
return (isset($this->stockPurchased[$id])) ? $this->stockPurchased[$id] : '';

$r = $this->wpdb->get_results("select woim.meta_value as pid, (
select _woim.meta_value from " . $this->wpdb->prefix . "woocommerce_order_itemmeta as _woim
where _woim.order_item_id = woi.order_item_id and _woim.meta_key = '_variation_id'
) as vid, (
select _woim.meta_value from " . $this->wpdb->prefix . "woocommerce_order_itemmeta as _woim
where _woim.order_item_id = woi.order_item_id and _woim.meta_key = '_qty'
) as vqty
from " . $this->wpdb->prefix . "woocommerce_order_itemmeta as woim, " . $this->wpdb->prefix . "woocommerce_order_items as woi
where woim.meta_key = '_product_id'
AND woi.order_item_type = 'line_item'
AND woi.order_item_id = woim.order_item_id
AND (SELECT COUNT(_P.ID) FROM " . $this->wpdb->prefix . "posts AS _P
WHERE _P.ID = woi.order_id
AND _P.post_type = 'shop_order'
AND _P.post_status != 'wc-cancelled'
AND _P.post_status != 'wc-refunded'
AND _P.post_status != 'wc-failed'
AND _P.post_status != 'wc-on-hold'
AND _P.post_status != 'wc-pending'
AND _P.post_status != 'trash'
);", ARRAY_A);

if ($r) {
foreach ($r as $value) {
if (isset($this->stockPurchased[$value['pid']])) {
$this->stockPurchased[$value['pid']] += $value['vqty'];
} else {
$this->stockPurchased[$value['pid']] = $value['vqty'];

return (isset($this->stockPurchased[$id])) ? $this->stockPurchased[$id] : '';



public function getFirstStock($id)
$firstStock = 0;

$firstStock += (isset($this->stockPurchased[$id])) ? $this->stockPurchased[$id] : 0;
$firstStock += (isset($this->stockLeft[$id])) ? $this->stockLeft[$id] : 0;

return ($firstStock) ? $firstStock : '';

January 17, 2019 at 9:54 am #3101135


Languages: English (English ) Italian (Italiano )

Timezone: Europe/Rome (GMT+02:00)

Hello, thank you for contacting WPML Support!

This may happen because the plugin seems to use direct database queries which do not take into account the language of the products.

Check this post for more details:

You may also try using REST API:

According to our support policy, we are not able to provide coding support for custom development and I am sorry to let you know that this sounds more like a custom programming work.
Our support policy does not include support for custom code unless it is directly related to the use of our WPML or WCML API, that you will find here:

At this point I would suggest you consider contacting one of our certified partners from this link if the issue goes beyond a simple WPML API hook:

You will get the custom assistance you need to get on with your project. I hope you understand.

Please let me know if you need further details.

Kind regards,