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

问题描述

我想在结帐时根据数量阈值对特定产品打折:

  • 当产品> = 10单位时-----每单位优惠2美元
  • 当产品> = 20单位时-----每单位折扣3美元

所需的样本输出示例:

  • 乘积×10等于$ 730.00 |优惠是$ 20.00 |优惠后总计:$ 710.00
  • 乘积×20等于$ 1460.00 |优惠是$ 60.00 |折后总额:$ 1400.00 $ 1,400.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 );
          }
        }

我在做什么错?如何使它正常工作?

解决方法

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

function quantity_based_pricing_discount( $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
    $threshold2 = 20; // Change price if items > 20
    $discount_1 = 2; // Reduce unit price by 2 dollars
    $discount_2 = 3; // Reduce unit price by 3 dollars

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Targeting specific product id
        if( $cart_item['product_id'] == '283' ) {
            $price = $cart_item['data']->get_price();
            $qty   = $cart_item['quantity'];

            if ( $qty >= $threshold1 && $qty < $threshold2 ) {
                $discount = $discount_1;
            } elseif ( $qty >= $threshold2 ) {
                $discount = $discount_2;
            }

            if ( isset($discount) ) {
                $cart_item['data']->set_price( round( $price - $discount,2 ) );
            }
        }
    }
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...