【问题标题】:Apply a progressive discount based on Woocommerce cart weight根据 Woocommerce 购物车重量应用累进折扣
【发布时间】:2020-11-27 22:14:32
【问题描述】:

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

  • 10 到 25 公斤,设置 5% 折扣
  • 26 至 50 公斤,设置 7.5% 折扣
  • 51 至 100 公斤,设置 10% 折扣
  • 101 至 150 公斤,设置 12.5% 的折扣
  • 150公斤以上,优惠15%

当其中一个规则将被应用时,应该有一条消息,也像图片中显示的此付款折扣一样。 Payment method discount

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

我不知道如何实现这一点,所以很遗憾我没有尝试任何东西。 提前致谢。

问候, 瓦斯科

【问题讨论】:

  • 首先计算购物车中所有产品的重量。然后使用 IF 语句应用折扣等。
  • StackOverFlow 的规则是一次只回答一个问题……因此,如果您不想结束这个问题,请避免同时提出多个问题。相反,您可以一个一个地提出多个问题。
  • 对不起,我是新来的,所以我还得学习。我是否必须为第二个问题创建一个新主题,或者我可以在新评论中写下来吗?

标签: php wordpress woocommerce discount


【解决方案1】:

欢迎来到 StackOverflow!

    $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
        }
    }

这里有一个代码 sn-p 可以回答您的问题。我只添加了一个重量条件。我相信你可以自己填写其他条件。

sn-p 可以添加到你的functions.php 中(只要是child theme,否则你的代码会在主题更新后被删除!)或者作为代码sn-p 使用诸如@ 之类的插件987654322@

【讨论】:

    【解决方案2】:

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

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

    基于:Add custom fee based on total weight in Woocommerce

    【讨论】:

    • 谢谢。这个选项有多安全?我的意思是,有人很容易操纵折扣吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 2013-07-10
    • 2020-10-18
    • 2014-03-28
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多