在Woocommerce购物车中找到最低价格 [英] Finding Lowest Price in Woocommerce Cart Items

查看:52
本文介绍了在Woocommerce购物车中找到最低价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我想将30%的优惠券代码应用到我的第一只蝙蝠(rs 100 X .70 = 70价格),因为它是购物车中最便宜的物品.因此,客户将为总购买支付270 rs.

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.

我已尝试将此Woo优惠券按产品添加到Woo贸易中,但它适用于所有产品(300 X .70 = 210价格).

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

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

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

此代码给我错误.

推荐答案

基本上有2个步骤:

  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

希望它会为您提供帮助:)

Hope it will help you :)

这篇关于在Woocommerce购物车中找到最低价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆