【问题标题】:Bulk Discount for specific product ID in WooCommerceWooCommerce 中特定产品 ID 的批量折扣
【发布时间】:2021-01-29 08:56:17
【问题描述】:

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

  • 当产品 >= 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 );
          }
        }

我做错了什么?如何使这项工作没有错误?

【问题讨论】:

    标签: php wordpress woocommerce cart discount


    【解决方案1】:

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

    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 文件中。经过测试并且可以工作。

    【讨论】:

    • 啊,折扣是针对售价 73 美元的产品。该值是等于我需要的数量的百分比。我刚试过这个方法。有什么不对劲。产品价格变成了折扣价。 10 * 73 美元的产品变成了 20 美元。 20 是正确的数字,但应该是折扣,而不是商品价格。它小计 20。我应该编辑 op 以进行澄清。
    • 输出:总产品 × 10 $20.00 小计 $20.00 总计 $20.00 所需输出:产品 × 10 $730.00 折扣 - $20.00 总计 - $710.00
    • @nichlor 我已经更新了我的代码……我已经在上一个 WooCommerce 版本上对其进行了测试,它运行良好。如果这个答案回答了你的问题,你可以请accept回答,谢谢。
    • 是的,完美!我不得不对 $discount_1 & $discount_2 进行调整,从 20 & 30 更改为 2 & 3 ,并添加了产品 ID 号并像魅力一样工作!非常感谢。
    猜你喜欢
    • 2020-08-30
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 2021-03-14
    • 2021-05-16
    • 2019-02-06
    相关资源
    最近更新 更多