<commit_before><?php

/**
 * This file is part of the hyyan/woo-poly-integration plugin.
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Hyyan\WPI\Product;

/**
 * Product Stock
 *
 * Handle stock sync
 *
 * @author Hyyan
 */
class Stock
{

    /**
     * Construct object
     */
    public function __construct()
    {
        // sync stock
        add_action(
                'woocommerce_reduce_order_stock'
                , array($this, 'syncStock')
        );
        add_filter(
                'woocommerce_restore_order_stock_quantity'
                , array($this, 'restoreStockQuantity')
                , 10
                , 2
        );
    }

    /**
     * Sync stock for product and its translation
     *
     * @param \WC_Order $order
     *
     * @return boolean false if sync failed , true otherwise
     */
    public function syncStock($order)
    {
        // get array of defined langs
        $langs = pll_languages_list();

        // get array of ordered products
        $items = $order->get_items();

        foreach ($items as $item) {

            $productId = $item['product_id'];
            $productObject = wc_get_product($productId);

            // product not found
            if (!$productObject) {
                return false;
            }

            // product does not manage the stock
            if (!$productObject->managing_stock()) {
                return false;
            }

            $productLang = pll_get_post_language($productId);

            // product default lang can not be found
            if (!$productLang) {
                return false;
            }

            foreach ($langs as $name) {

                // skip the current product lang
                if ($productLang == $name) {
                    continue;
                }

                $translationID = pll_get_post($productId, $name);

                if ($translationID && ($transltedProduct = wc_get_product($translationID))) {
                    $transltedProduct->reduce_stock($item['qty']);
                }
            }

        }

        return true;
    }

    /**
     * Restore order stock quantity
     *
     * @param integer $change the stock change
     * @param integer $id     item id
     *
     * @return integer stock change
     */
    public function restoreStockQuantity($change, $id)
    {

        $orderId = absint($_POST['order_id']);
        $order = new \WC_Order($orderId);
        $items = $order->get_items();
        $product = $order->get_product_from_item($items[$id]);

        // get array of defined langs
        $langs = pll_languages_list();

        foreach ($items as $item) {

            $productId = $item['product_id'];
            $productObject = wc_get_product($productId);

            // product not found
            if (!$productObject) {
                return $change;
            }

            $productLang = pll_get_post_language($productId);

            // product default lang can not be found
            if (!$productLang) {
                return $change;
            }

            foreach ($langs as $name) {

                // skip the current product lang
                if ($productLang == $name) {
                    continue;
                }

                $translationID = pll_get_post($productId, $name);

                if ($translationID && ($transltedProduct = wc_get_product($translationID))) {
                    $transltedProduct->increase_stock($change);
                }
            }
        }

        return $change;
    }

}<commit_msg>Improved stock code to support product variations<commit_after><?php

/**
 * This file is part of the hyyan/woo-poly-integration plugin.
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Hyyan\WPI\Product;

use Hyyan\WPI\Utilities;

/**
 * Product Stock
 *
 * Handle stock sync
 *
 * @author Hyyan
 */
class Stock
{
    const STOCK_REDUCE_ACTION = 'reduce';
    const STOCK_INCREASE_ACTION = 'increase';

    /**
     * Construct object
     */
    public function __construct()
    {
        // sync stock
        add_action(
                'woocommerce_reduce_order_stock'
                , array($this, 'reduceStock')
        );
        add_filter(
                'woocommerce_restore_order_stock_quantity'
                , array($this, 'increaseStock')
        );
    }

    /**
     * Reduce stock for product and its translation
     *
     * @param \WC_Order $order
     */
    public function reduceStock($order)
    {

        /* Get array of ordered products */
        $items = $order->get_items();

        /* Reduce stock */
        foreach ($items as $item) {
            $this->change($item, self::STOCK_REDUCE_ACTION);
        }
    }

    /**
     * Increase stock for product and its translation
     *
     * @param integer $change the stock change
     *
     * @return integer stock change
     */
    public function increaseStock($change)
    {

        $orderId = absint($_POST['order_id']);
        $order = new \WC_Order($orderId);

        /* Get array of ordered products */
        $items = $order->get_items();

        /* Increase stock */
        foreach ($items as $item) {
            $item['change'] = $change;
            $this->change($item, self::STOCK_INCREASE_ACTION);
        }

        return $change;
    }

    /**
     * Change the product stock in the given order item
     *
     * @param array  $item   the order data
     * @param string $action STOCK_REDUCE_ACTION | STOCK_INCREASE_ACTION
     */
    protected function change(array $item, $action = self::STOCK_REDUCE_ACTION)
    {
        $productID = $item['product_id'];
        $productObject = wc_get_product($productID);
        $productLang = pll_get_post_language($productID);

        $variationID = $item['variation_id'];

        /* Handle Products */
        if ($productObject && $productLang) {

            /* Get the translations IDS */
            $translations = Utilities::getProductTranslationsArrayByObject(
                            $productObject
            );

            /* Remove the current product from translation array */
            unset($translations[$productLang]);

            $isManageStock = $productObject->managing_stock();
            $isVariation = $variationID && $variationID > 0;
            $method = ($action === self::STOCK_REDUCE_ACTION) ?
                    'reduce_stock' :
                    'increase_stock';
            $change = ($action === self::STOCK_REDUCE_ACTION) ?
                    $item['qty'] :
                    $item['change'];

            /* Sync stock for all translation */
            foreach ($translations as $ID) {

                /* Only if product is managing stock */
                if ($isManageStock && !$isVariation) {
                    if (($translation = wc_get_product($ID))) {
                        $translation->$method($change);
                    }
                }
            }

            /* Handle variation */
            if ($isVariation) {
                $posts = VariationDuplicator::getRelatedVariation($variationID);
                foreach ($posts as $post) {
                    if ($post->ID == $variationID) {
                        continue;
                    }
                    $variation = wc_get_product($post);
                    if ($variation && $variation->managing_stock()) {
                        $variation->$method($change);
                    }
                }
            }
        }
    }

}
{'fraction_non_alphanumeric': 0.10963669157433652, 'fraction_numerical': 0.000515331100231899, 'mean_word_length': 1.5816428333887596, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4646149', 'n_tokens_mistral': 2142, 'n_tokens_neox': 2020, 'n_words': 500}