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