Php WooCommerce中特定产品ID的批量折扣

Php WooCommerce中特定产品ID的批量折扣,php,wordpress,woocommerce,cart,discount,Php,Wordpress,Woocommerce,Cart,Discount,我想在结账时根据数量阈值对特定产品进行折扣: 当产品>=10件时------每件折扣2美元 当产品>=20件时------每件折扣3美元 所需输出示例: 产品×10等于730.00美元|折扣为20.00美元|折扣后总计:710.00美元 产品×20等于$1460.00 |折扣为$60.00 |折扣后总计$1400.00$1400.00 到目前为止,我有这个,但当我添加10个单位的产品到购物车,我得到一个严重的错误 我的代码: add_action( 'woocommerce_before

我想在结账时根据数量阈值对特定产品进行折扣:

  • 当产品>=10件时------每件折扣2美元
  • 当产品>=20件时------每件折扣3美元
所需输出示例:

  • 产品×10等于730.00美元|折扣为20.00美元|折扣后总计:710.00美元
  • 产品×20等于$1460.00 |折扣为$60.00 |折扣后总计$1400.00$1400.00
到目前为止,我有这个,但当我添加10个单位的产品到购物车,我得到一个严重的错误

我的代码:

add_action( 'woocommerce_before_calculate_totals', 'quantity_based_pricing', 9999 );
    function quantity_based_pricing( $cart ) {
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
    
        // Define discount rules and thresholds
        $threshold1 = 10; // Change price if items > 10
        $discount1 = 0.02739726027; // Reduce 73 dollar unit price by 2 dollars
        $threshold2 = 20; // Change price if items > 20
        $discount2 = 0.04109589041; // Reduce 73 dollar unit price by 3 dollars
          //compare
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item )
          // Get product id
          $product_id = $cart_item['product_id'];
    
            if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 && $cart_item = '283' ) {
             $price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );
             $cart_item['data']->set_price( $price );
          } elseif ( $cart_item['quantity'] >= $threshold2 && in_array( $product_id, $specific_product_ids ) ) {
             $price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
             $cart_item['data']->set_price( $price );
          }
        }
add_action('woocommerce_-before_-calculate_-total','quantity_-based_-pricing',9999);
基于功能数量的定价($cart){
if(is_admin()&&!defined('DOING_AJAX'))返回;
if(did_action('woocommerce_before_calculate_totals')>=2)返回;
//定义折扣规则和阈值
$threshold1=10;//如果项目>10,则更改价格
$折扣1=0.02739726027;//将73美元单价降低2美元
$threshold2=20;//如果项目>20,则更改价格
$折扣2=0.04109589041;//将73美元单价降低3美元
//比较
foreach($cart->get\u cart()作为$cart\u item\u key=>$cart\u item)
//获取产品id
$product_id=$cart_项目['product_id'];
如果($cart\u item['quantity']>=$threshold1和$cart\u item['quantity']<$threshold2和$cart\u item='283'){
$price=round($cart\u item['data']->get\u price()*(1-$discount1),2);
$cart_item['data']->set_price($price);
}elseif($cart\u item['quantity']>=$threshold2&&in\u数组($product\u id,$specific\u product\u id)){
$price=round($cart\u item['data']->get\u price()*(1-$discount2),2);
$cart_item['data']->set_price($price);
}
}

我做错了什么?如何使其不出错?

您的代码中有一些错误。请尝试以下操作:

功能数量基于定价折扣($cart){
if(定义了('DOING'uajax'))
返回;
如果(did_action('woocommerce_before_calculate_totals')>=2)
返回;
//定义折扣规则和阈值
$threshold1=10;//如果项目>10,则更改价格
$threshold2=20;//如果项目>20,则更改价格
$折扣_1=2;//单价降低2美元
$折扣_2=3;//单价降低3美元
//循环浏览购物车项目
foreach($cart->get\u cart()作为$cart\u项目){
//针对特定产品id
如果($cart\u item['product\u id']=='283'){
$price=$cart_item['data']->get_price();
$qty=$cart_物料['qty'];
如果($qty>=$threshold1&&$qty<$threshold2){
$discount=$discount\u 1;
}elseif($qty>=$threshold2){
$折扣=$折扣_2;
}
if(isset(折扣)){
$cart_item['data']->set_price(四舍五入($price-$折扣,2));
}
}
}
}

代码进入活动子主题(或活动主题)的functions.php文件。测试和工作。

啊,折扣是针对一款售价73美元的产品。该值是一个%的值,相当于我需要的数量。我刚试过这个方法。有点不对劲。产品价格变成了折扣价。10*73美元的产品变成了20美元。20是正确的数字,但应该是折扣,而不是项目价格。它作了小计20。我应该编辑op进行澄清。输出:总产品×10$20.00小计$20.00总计$20.00预期输出:产品×10$730.00折扣-$20.00总计-$710。00@nichlor我已经更新了我的代码…我已经在上一个WooCommerce版本上测试了它,它工作得非常完美。如果这个答案回答了你的问题,你可以请你回答,谢谢。是的,太完美了!我不得不对$打折1和$打折2进行调整,从20和30更改为2和3,并添加了产品ID号,效果非常好!非常感谢你。