更新购物车内容中的产品小计

updating product subtotal in cart contents

我正在尝试编写我的活动插件。 问题来了,

我想在 $cart_items.

的产品中更新 line_subtotalline_total

1- woocommerce_cart_product_subtotal 没有真正起作用。

2- 我试图通过 cart_item_key

重新定义数组的元素
                foreach($cartItems as $hash => $perProduct)
                { 
                    if($perProduct['product_id'] = $lastItemsProductId)
                    {
                        if($lastItemsQuantity > 1)
                        {
                            $lineSubtotal = data_get($lastItemInCart, 'line_subtotal');
                            $lineTotal = data_get($lastItemInCart, 'line_total');
                            $newQuantity = $lastItemsQuantity-1; 
                            
                            $lastItemInCart['line_subtotal'] = $newQuantity * $lastItemsPrice;
                            $lastItemInCart['line_total'] = $newQuantity * $lastItemsPrice;  
                            $cartItems[$lastItemsKey] = $lastItemInCart;  
 
                            WC()->cart->calculate_totals();
             
 
                            
                        } else {
                             
                        }
                    }
                     
                }

此函数也在 woocommerce_before_calculate_totals 操作中运行。

当我尝试使用 woocommerce_get_cart_contents 过滤器时,我的购物车自动清空了。

3- 场景:

当我添加A产品时,(如果被系统选择)并且如果数量超过2,我想对该产品进行折扣。

有什么帮助吗?谢谢。

答案在这里

        /**
         * Add filter for order list 
         *
         * @param [int] $product_subtotal
         * @param [object] $product
         * @param [int] $quantity
         * @param [array] $cart
         * @return void
         */
        public function filterWoocommerceCartProductSubtotal($product_subtotal, $product, $quantity, $cart) : string
        {
            $appliedCoupons  = $cart ? $cart->get_applied_coupons() : null;
            $cartCount = $cart ? count($cart->get_cart()) : 0;
            $cartItems = $cart->get_cart();
            $lastItemInCart = last($cartItems);
            $lastItemsProductId = data_get($lastItemInCart, 'product_id', data_get($lastItemInCart, 'product_id'));
            $lastItemsPrice = $lastItemInCart['data']->get_regular_price();
            $lastItemsQuantity = data_get($lastItemInCart, 'quantity');
            $lastItemsKey = data_get($lastItemInCart, 'key');
            
            if( in_array('3al2ode', $appliedCoupons)){  
                if($lastItemsQuantity > 1) {
                    if(@data_get($product,'id') == $lastItemsProductId)
                    {
                        $newSubTotal = 0;
                        $price = $product->get_price();  

                        $newQuantity = $lastItemsQuantity-1; 
                        $quantity = $newQuantity; 
                        $newSubTotal += $price * $quantity;
                
                        return $newSubTotal ? wc_price($newSubTotal) : $product_subtotal; 
                    }
                } 
                
            }  
            return $product_subtotal;
            
        }

对了,滤镜应该是这样的;

add_filter( 'woocommerce_cart_product_subtotal', [$this,'filterWoocommerceCartProductSubtotal'], 10, 4);

按照你现在的场景-当我添加一个产品时,(它是由系统选择的)并且如果数量超过2,我想对该产品进行折扣。

你应该试试这个-

add_action( 'woocommerce_cart_calculate_fees','woocommerce_add_discount',10, 1 );
function woocommerce_add_discount() {
    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $cart_qty = count($woocommerce->cart->get_cart());

    if($cart_qty > 2)
    {
        $percentage     = 0.5;

        $cart_subtotal  = $woocommerce->cart->get_subtotal() ?: 0;

        //$cart_total     = $woocommerce->cart->cart_contents_total;
        //$shipping_total = $woocommerce->cart->get_shipping_total() ?: 0;
        //$tax_total      = $woocommerce->cart->get_taxes_total() ?: 0;
        //$grand_total    = $cart_total + $shipping_total + $tax_total;

        // Calculate the amount to reduce
        $discount = $cart_subtotal * $percentage;
        $woocommerce->cart->add_fee( 'Discount 50%', -$discount, true, '' );
    }
}

您可以update/modify相应地设置条件。

        /**
         * Add filter for order list 
         *
         * @param [int] $product_subtotal
         * @param [object] $product
         * @param [int] $quantity
         * @param [array] $cart
         * @return void
         */
        public function filterWoocommerceCartProductSubtotal($product_subtotal, $product, $quantity, $cart) : string
        {
            $appliedCoupons  = $cart ? $cart->get_applied_coupons() : null;
            $cartCount = $cart ? count($cart->get_cart()) : 0;
            $cartItems = $cart->get_cart();
            $lastItemInCart = last($cartItems);
            $lastItemsProductId = data_get($lastItemInCart, 'product_id', data_get($lastItemInCart, 'product_id'));
            $lastItemsPrice = $lastItemInCart['data']->get_regular_price();
            $lastItemsQuantity = data_get($lastItemInCart, 'quantity');
            $lastItemsKey = data_get($lastItemInCart, 'key');
            
            if( in_array('3al2ode', $appliedCoupons)){  
                if($lastItemsQuantity > 1) {
                    if(@data_get($product,'id') == $lastItemsProductId)
                    {
                        $newSubTotal = 0;
                        $price = $product->get_price();  

                        $newQuantity = $lastItemsQuantity-1; 
                        $quantity = $newQuantity; 
                        $newSubTotal += $price * $quantity;
                
                        return $newSubTotal ? wc_price($newSubTotal) : $product_subtotal; 
                    }
                } 
                
            }  
            return $product_subtotal;
            
        }

没错,滤镜应该是这样的;

add_filter( 'woocommerce_cart_product_subtotal', [$this,'filterWoocommerceCartProductSubtotal'], 10, 4);