【问题标题】:Weight based progressive discount displays remaining weight to get a better discount message基于重量的累进折扣显示剩余重量以获得更好的折扣信息
【发布时间】:2020-08-11 14:28:00
【问题描述】:

我正在使用Apply a progressive discount based onWoocommerce cart total weight 答案代码(对我上一个问题)根据购物车总重量添加批量折扣。

代码很完美,但我想在购物车页面(仅购物车)上方显示一个横幅,例如“已添加到购物车”消息,其中显示一条消息以及客户必须为下一个折扣规则订购更多的数量.

示例消息:“如果您以 10 欧元的价格订购,您将获得 10% 的折扣”

我试图为消息框找到正确的代码,但找不到正确的代码。我只能找到wc_add_to_cart_message_html,但我很确定这个不是正确的。

抱歉所有这些问题,但我对 PHP 很陌生,想学习。 希望任何人都可以提供帮助。

【问题讨论】:

    标签: php wordpress woocommerce message discount


    【解决方案1】:

    更新

    您必须将所有相关代码替换为以下代码,这将根据购物车重量进行折扣,并显示一条自定义消息,显示剩余重量以获得更好的百分比折扣(显示百分比)。

    提示信息:由于是基于重量的折扣,因此无法显示剩余费用。相反,您可以显示获得更好折扣所需的剩余重量。

    代码如下:

    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 < 30 ) 
        {
            $percentage       = 5;
            $remaining_weight = 30 - $cart_weight;
            $next_percent     = 7.5;
        } 
        elseif ( $cart_weight >= 30 && $cart_weight < 70 ) 
        {
            $percentage       = 7.5;
            $remaining_weight = 70 - $cart_weight;
            $next_percent     = 10;
        } 
        elseif ( $cart_weight >= 70 && $cart_weight < 130 ) 
        {
            $percentage       = 10;
            $remaining_weight = 130 - $cart_weight;
            $next_percent     = 12.5;
        } 
        elseif ( $cart_weight >= 130 && $cart_weight < 200 ) {
            $percentage       = 12.5;
            $remaining_weight = 200 - $cart_weight;
            $next_percent = 15;
        } 
        elseif ( $cart_weight >= 200 ) 
        {
            $percentage       = 15;
            $next_percent     = false;
        } 
        else 
        {
            $next_percent = 5;
            $remaining_weight = 10 - $cart_weight;
        }
    
        // 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 );
        }
    
        if ( did_action( 'woocommerce_cart_calculate_fees' ) >= 2 )
            return;
    
        // Display a custom message
        if ( is_cart() && $next_percent ) { 
            wc_add_notice( sprintf( 
                __("If you order for %s extra, you will receive a %s discount.", "woocommerce"), 
                wc_format_weight($remaining_weight), $next_percent.'%'
            ), 'notice' );
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    购物车中显示的消息截图:

    【讨论】:

    • 我知道它应该是重量而不是数量。感谢您清除它。
    • 我把步骤改成[10, 5], [30, 7.5], [70, 10], [130, 12.5], [200, 15],发现代码中有一个小bug。如果我在购物车中添加 1 公斤,则消息说我必须多订购 9 公斤才能获得 5% 的折扣。这是不正确的。应该是 10 公斤(折扣必须从 10 开始,而不是达到 10 公斤)。当我在购物车中多加 9 公斤时,它会应用 5% 的折扣,并且消息说我必须多订购 20 公斤才能获得 7.5% 的折扣。当我这样做时,会出现正确的消息(订购 40 公斤以上以获得 7.5%),但我仍然有 5% 的折扣而不是 7.5%。
    • 我试图在 stept 中改变这一点,方法是用加一改变每个重量。但这并没有给我解决方案。我认为它在$steps[0][0] &amp;&amp; $cart_weight &lt;= $steps[1][0],但我不明白这些值。很抱歉我的无知。
    • 感谢您的反应。我明白你的解释。但是它没有正确应用折扣。请参阅此screenshot。你可以在这里看到我必须多订购 1 公斤才能获得 5% 的折扣。但是当我这样做时,5% 的折扣仍然存在。只有消息更改为新变量。希望你能理解我的问题。消息和折扣计算之间的差异似乎与 1 不同。
    • 你我的英雄,谢谢你。这确实很完美!我只注意到消息中没有正确显示折扣百分比,而只有 %,而我认为代码是完美的?
    猜你喜欢
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多