0
votes

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

2
First you count the weight of all the products in the cart. Then with an IF statement you apply the discount and such.Mr J.
The rule on StackOverFlow is one question at the time… So avoid asking multiple questions at once if you don't want this question to be closed. Instead, you can ask multiple questions, one by one.LoicTheAztec
Sorry, I'm just new here, so I have to learn yet. Do I have to create a new thread for the second question or can I write it down here in a new comment?MediaCreandum

2 Answers

1
votes

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

1
votes

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