Quantity discount on 2nd item only in Woocommerce

后端 未结 3 1039
故里飘歌
故里飘歌 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:11

    add_action( 'woocommerce_before_calculate_totals', 'set_the_discount' );
    
    function set_the_discount( $cart ) {
    
    foreach ( $cart->cart_contents as $key => $value ) {    
    
        //please check whether your item is the second item of your cart or not?
    
        //if this is your second item in your cart
    
        //Set the 50% discount
    
        $product_id = $value['product_id'];
        $product = wc_get_product($product_id);
        $get_price = $product->get_price();
        $new_price = $get_price / 2;
        $value['data']->set_price($new_price);
      }
    
    }
    This hook can be used to set the price directly to the cart. Hope this may help
    

提交回复
热议问题