woocommerce购物车中产品的自定义价格

时间:2018-08-01 08:50:56

标签: php wordpress woocommerce hook

请完整阅读此问题。我找不到任何有用的东西来获得完全正常的功能。

STEP 1

商店中有一种免费产品,直到其数量与购物篮中其他商品的数量匹配为止。

STEP1

第2步

如果该产品的价格超过其他产品,则必须分为2:-免费,其数量与所购买的商品数量相同-已超过购物篮中其他商品的数量。

STEP2

为此,我使用了钩子:woocommerce_check_cart_items,它对我有用。

第3步

但是在结帐页面中的购物车之后:

STEP3

在付款过程中以及在管理面板中保存时,所有内容都会正确显示。

如果我使用woocommerce_before_calculate_totals挂钩,则会在产品添加到购物车事件中添加一个无限循环。

我对必要的更改进行了几次检查,仍然是一个无休止的循环。

也许,您知道其他钩子吗?

或者如何将我的自定义购物车保存在结帐页面?

我的代码:

add_action('woocommerce_check_cart_items', array('validate_all_cart_contents', 'init'), 10);


class validate_all_cart_contents
{

    private static $custom_price = 5; // This will be your custome price
    private static $sizing_kit_id = 57651;

    private static $count_products = 0;
    private static $count_sizing_kit = 0;

    private static $payment_count = 0;
    private static $free_count = 0;

    public static function init()
    {

        foreach (WC()->cart->cart_contents as $cart_content_product) {

            if ($cart_content_product["product_id"] == self::$sizing_kit_id) {
                self::$count_sizing_kit += $cart_content_product["quantity"];
            } else {
                self::$count_products += $cart_content_product["quantity"];
            }
        }

        self::$free_count = self::$count_sizing_kit;

        if (self::$count_products <= self::$count_sizing_kit) {
            self::$free_count = self::$count_products;
            self::$payment_count = self::$count_sizing_kit - self::$count_products;
        }


        if (self::$count_sizing_kit > 0 && self::$count_sizing_kit > self::$count_products && !self::check()) {


            self::clear_sizingkit_in_cart();

            /**
             * Payment sizing kit
             */

            self::add_to_cart_with_custom_price(self::$payment_count, self::$custom_price);

            /**
             * Free sizing kit
             */

            self::add_to_cart_with_custom_price(self::$free_count);

            WC()->cart->calculate_totals();
            WC()->cart->set_session();
            WC()->cart->maybe_set_cart_cookies();
        }

    }

    private static function check()
    {

        $correct_free_count = $correct_payment_count = (self::$count_products >= self::$count_sizing_kit) ? true : false;

        foreach (WC()->cart->cart_contents as $cart_content_product) {

            if ($cart_content_product["product_id"] == self::$sizing_kit_id) {

                if ($cart_content_product["line_total"] == 0 && $cart_content_product["quantity"] == self::$free_count) {
                    $correct_free_count = true;
                } else if ($cart_content_product["line_total"] > 0 && $cart_content_product["quantity"] == self::$payment_count) {
                    $correct_payment_count = true;
                }
            }
        }

        return $correct_free_count && $correct_payment_count;
    }

    private static function clear_sizingkit_in_cart()
    {
        /**
         * Remove sizing kit in cart
         */
        $keys_list = array();

        foreach (WC()->cart->cart_contents as $cart_content_product) {

            if ($cart_content_product['product_id'] == self::$sizing_kit_id) {
                $in_cart = WC()->cart->find_product_in_cart($cart_content_product['key']);

                $keys_list[] = $in_cart;
            }
        }


        if ($keys_list) {
            foreach ($keys_list as $key) {
                WC()->cart->remove_cart_item($key);
            }
        }
    }

    private static function add_to_cart_with_custom_price($sizingkit_cart_count, $sizingkit_cart_price = 0)
    {
        if ($sizingkit_cart_count == 0) return;

        $add_to_cart = WC()->cart->add_to_cart(self::$sizing_kit_id, $sizingkit_cart_count);

        if ($sizingkit_cart_price != 0) {

            $sizingkit_cart_price = $sizingkit_cart_price <= 30 ? $sizingkit_cart_price : 30;

            $in_cart = WC()->cart->find_product_in_cart($add_to_cart);

            foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
                if ($cart_item['key'] == $in_cart) {

                    $customPriceSizingKit[$in_cart] = $sizingkit_cart_price;

                    $cart_item['data']->set_price($sizingkit_cart_price);
                }
            }

        }

        return true;

    }

}

0 个答案:

没有答案
相关问题