Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Commit

Permalink
fixes #475 variation stock issues
Browse files Browse the repository at this point in the history
fixes #475 variation stock issues by correcting mechanism for cascading
update from base language and adding update of stock status
  • Loading branch information
Jon007 committed Feb 6, 2021
1 parent b9ae245 commit 4a4837f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/Hyyan/WPI/Product/Stock.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,34 @@ public static function SyncStock( \WC_Product $product ) {
$targetValue = $product_with_stock->get_stock_quantity();

//update all the translations to the same stock level..
$product_translations = ($product_with_stock->is_type( 'variation' )) ?
Variation::getRelatedVariation( get_post_meta( $product_with_stock->get_id(), Variation::DUPLICATE_KEY, true ), true ) :
Utilities::getProductTranslationsArrayByObject( $product_with_stock );
$product_translations = [];
if ($product_with_stock->is_type( 'variation' )) {
$base_variation_id = Utilities::get_translated_variation($product_with_stock->get_id(),pll_default_language());
$product_translations = Variation::getRelatedVariation(
get_post_meta( $base_variation_id, Variation::DUPLICATE_KEY, true )
, true );
if ($base_variation_id!=$product_id_with_stock){
if (($key = array_search($product_id_with_stock, $product_translations)) !== false) {
unset($product_translations[$key]);
}
$key = array_search($base_variation_id, $product_translations);
if ( $key === false ) {
$product_translations[]=$base_variation_id;
}
}
} else {
$product_translations = Utilities::getProductTranslationsArrayByObject( $product_with_stock );
}

if ( $product_translations ) {
$target_status=$product_with_stock->get_stock_status();
foreach ( $product_translations as $product_translation ) {
if ( $product_translation != $product_with_stock->get_id() ) {
$translation = wc_get_product( $product_translation );
if ( $translation ) {
wc_update_product_stock( $translation, $targetValue );
//here the product stock is updated without saving then wc_update_product_stock_status will update and save status
wc_update_product_stock( $translation, $targetValue, 'set', true );
wc_update_product_stock_status ($product_translation, $target_status);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Hyyan/WPI/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ public static function get_variation_parentid($variation)
*/
public static function get_translated_variation($product_id, $lang)
{
$langparam = $lang;
if (! $lang) {
$lang = pll_current_language();
}
Expand Down Expand Up @@ -492,6 +493,7 @@ public static function get_translated_variation($product_id, $lang)
'meta_value' => $variationmaster,
'post_type' => 'product_variation',
'post_parent' => $translated_id,
'lang' => $langparam, //allow empty param to get all languages
));
//return the related variation if there is one
if (count($posts)) {
Expand Down

0 comments on commit 4a4837f

Please sign in to comment.