Quantity discount on 2nd item only in Woocommerce

后端 未结 3 973
故里飘歌
故里飘歌 2021-01-16 04:41

I want to achieve a global discount for all products but only for the 2nd product item.

What do I mean? If the customer buys \"Jacket\" no discount is given.

3条回答
  •  别那么骄傲
    2021-01-16 05:02

    foreach ( $cart->cart_contents as $key => $values ) {
                        $product_id = $values['product_id'];
    
                        foreach ($cart->get_cart_item_quantities() as $key => $value){
                            //print_r($key[1]);
                            $key = array_keys($cart->get_cart_item_quantities())[1];
                            if($key == $product_id){
    
                                $product = wc_get_product($product_id);
                                $get_price = $product->get_price();
                                $new_price = $get_price / 2;
                                $values['data']->set_price($new_price);
    
                                break;
                            }
                        }
                    }
    //Please try this, it will work for sure in same above hook :)
    

提交回复
热议问题