为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
蛊毒传说 2023-09-22 15:08:27
我正在寻找一种根据购物车总重量应用折扣(百分比)的方法。例子:10至25公斤,设置5%折扣26 至 50 公斤,设置 7.5% 折扣51至100公斤,设置10%折扣101 至 150 公斤,设置 12.5% 折扣超过150公斤,设置15%折扣当应用其中一项规则时,应该会出现一条消息,如图所示的付款折扣。 付款方式折扣如果还可以显示一个消息框,例如“添加到购物车消息框”,该消息框显示客户必须订购多少才能获得第二个折扣规则中的第一个,例如:订购**公斤以上并获得 5% 的折扣。我不知道如何实现这一目标,所以不幸的是我没有尝试任何东西。提前致谢。
查看完整描述

2 回答

?
MYYA

TA贡献1868条经验 获得超4个赞

您可以使用负费用根据购物车重量进行渐进折扣,如下所示:


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 文件中。


查看完整回答
反对 回复 2023-09-22
?
万千封印

TA贡献1891条经验 获得超3个赞

$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

        }

    }

这是回答您问题的代码片段。我只添加了一种重量条件。我相信你自己能够满足其他条件。


查看完整回答
反对 回复 2023-09-22
  • 2 回答
  • 0 关注
  • 70 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信