Reputation: 1
Wondering if anyone can help me resolve my problem here. I am looking for a way to apply a coupon code to a Single (least expensive) product item in a Cart. e.g. customer add 2 SG bats at price of 100 and 200 respectively.
I want to apply 30% coupon code to my first bat (rs 100 X .70 = 70 price) for the first bat since its a cheapest item in the cart. So customer will pay 270 rs for total purchase.
I have tried to add this coupon on the Woo commerce by product but it applies to all the products (300 X .70 = 210 price).
I tried different functions of Cart object to retrieve the each item pricing without any success.
global $woocommerce;
$lowestPrice=1000;
$myproduct_price=0;
foreach ( $cart_object->cart_contents as $key => $value ) {
$myproduct_price=$value['data']->price;
if($myproduct_price < $lowerPrice) $lowestprice=$myproduct_price;
}
this code is giving me error.
Upvotes: 0
Views: 1214
Reputation: 2810
Basically there are 2 steps:
Complete Code: (Tested)
global $woocommerce;
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$product_price[] = get_option('woocommerce_tax_display_cart') == 'excl' ? $_product->get_price_excluding_tax() : $_product->get_price_including_tax(); /*Store all product price from cart items in Array */
}
}
$lowestprice = min($product_price); // Lowest Price from array
Hope it will help you :)
Upvotes: 1