根据 Woocommerce 购物车重量应用渐进折扣

我正在寻找一种根据购物车总重量应用折扣(百分比)的方法。例子:

  • 10至25公斤,设置5%折扣

  • 26 至 50 公斤,设置 7.5% 折扣

  • 51至100公斤,设置10%折扣

  • 101 至 150 公斤,设置 12.5% 折扣

  • 超过150公斤,设置15%折扣

当应用其中一项规则时,应该会出现一条消息,如图所示的付款折扣。 付款方式折扣

如果还可以显示一个消息框,例如“添加到购物车消息框”,该消息框显示客户必须订购多少才能获得第二个折扣规则中的第一个,例如:订购**公斤以上并获得 5% 的折扣。

我不知道如何实现这一目标,所以不幸的是我没有尝试任何东西。提前致谢。


蛊毒传说
浏览 74回答 2
2回答

MYYA

您可以使用负费用根据购物车重量进行渐进折扣,如下所示:add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_discount', 30, 1 );function shipping_weight_discount( $cart ) {    if ( is_admin() && ! defined( 'DOING_AJAX' ) )        return;    $cart_weight   = $cart->get_cart_contents_weight();    $cart_subtotal = $cart->get_subtotal(); // Or $cart->subtotal;    $percentage    = 0;    if ( $cart_weight >= 10 && $cart_weight <= 25 ) {        $percentage = 5;    } elseif ( $cart_weight > 25 && $cart_weight <= 50 ) {        $percentage = 7.5;    } elseif ( $cart_weight > 50 && $cart_weight <= 100 ) {        $percentage = 10;    } elseif ( $cart_weight > 100 && $cart_weight <= 150 ) {        $percentage = 12.5;    } elseif ( $cart_weight > 150 ) {        $percentage = 15;    }    // Apply a calculated discount based on weight    if( $percentage > 0 ) {        $discount = $cart_subtotal * $percentage / 100;        $cart->add_fee( sprintf( __( 'Weight %s discount', 'woocommerce' ), $percentage.'%'), -$discount );    }}代码位于活动子主题(或活动主题)的 function.php 文件中。

万千封印

$applied_discount = 0;    add_filter( 'woocommerce_calculated_total', 'add_conditional_discount_by_weight', 10, 2 );    function add_conditional_discount_by_weight( $total, $cart ) {                global $applied_discount;                $total_weight = WC()->cart->get_cart_contents_weight(); // gets cart weight                if($total_weight >= 10 || $total_weight <= 25){                        $applied_discount = 5;            return $total*(1-($applied_discount/100));                    }    }        add_action( 'woocommerce_cart_totals_after_order_total', 'display_applied_discount' );    function display_applied_discount() {                global $applied_discount;                if($applied_discount > 0){            $discount = WC()->cart->total * ($applied_discount/100);        ?>        <tr class="order-total">            <th><?php esc_html_e( "Applied Discount ($applied_discount%)", 'woocommerce' ); ?></th>            <td><?php echo "$".$discount ?></td>        </tr>        <?php        }    }这是回答您问题的代码片段。我只添加了一种重量条件。我相信你自己能够满足其他条件。
打开App,查看更多内容
随时随地看视频慕课网APP