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.
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