在Woocommerce购物车商品中寻找最低价格


Finding Lowest Price in Woocommerce Cart Items

想知道是否有人能帮我解决这里的问题。我正在寻找一种方法,将优惠券代码应用于购物车中的单一(最便宜(产品项目。例如,客户分别以100和200的价格添加2个SG蝙蝠。

我想将30%的优惠券代码应用于我的第一只蝙蝠(rs 100 X.70=70价格(,因为这是购物车中最便宜的商品。因此,客户将为全部购买支付270卢比。

我试着在Woo商业上按产品添加此优惠券,但它适用于所有产品(300 X。70=210价格(。

我尝试了Cart对象的不同功能来检索每个项目的定价,但没有成功。

 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;
}

这个代码给了我错误。

基本上有两个步骤:

  1. 获取所有购物车产品的价格并存储在一个阵列中
  2. 从数组中查找最小值

完整代码:(已测试(

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

希望它能帮助你:(