Apply a progressive discount based on Woocommerce cart weight

旧巷老猫 提交于 2020-08-26 13:31:07

问题


I'm looking for a way to apply a discount (percentage) based on cart total weight. Example:

  • 10 to 25 kg, set a 5% discount
  • 26 to 50 kg, set a 7,5% discount
  • 51 to 100 kg, set a 10% discount
  • 101 to 150 kg, set a 12,5% discount
  • more than 150 kg, set a 15% discount

When one of the rules will be applied there should be a message also like this payment discount shown in the picture. Payment method discount

And if also possible showing a message box like the 'added to cart message box' which shows how much customers have to order to get the first of a second discounted rule like: Order **kg more and get 5% discount.

I have no idea how to achieve this, so I unfortunately did not tried anything. Thanks in advance.

Regards, Vasco


回答1:


You can use a negative fee to make a progressive discount based on cart weight as follow:

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 );
    }
}

Code goes in function.php file of your active child theme (or active theme).

Based on: Add custom fee based on total weight in Woocommerce




回答2:


Welcome to 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
        }
    }

Here's a code snippet that answers your question. I only added one weight condition. I believe you'll be able to fill in the other conditions yourself.

The snippet can be added to your functions.php (as long as it is a child theme, otherwise your code will be deleted after a theme update!) or as a code snippet using plugins such as Code Snippets



来源:https://stackoverflow.com/questions/63304091/apply-a-progressive-discount-based-on-woocommerce-cart-weight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!