• Resolved emzdesign

    (@emzdesign)


    I’m working on a site for a client and the site is in maintenance mode at the moment so can’t share the link.

    Basically what I’m trying to do is set up two different shipping costs. A base cost, and a cost if someone spends over £40.

    I basically want to have a similar setup to what you get when adding Free Delivery – an option to choose a minimum amount before a new shipping cost is applied.

    I want the shipping cost to be £6, and if they spend over £40 to change the shipping cost to £1.

    Is this possible?

Viewing 6 replies - 1 through 6 (of 6 total)
  • I am sure there are plugins out there, but if you are happy with code in your functions.php, then you can use something like this :-

    If you have different rates then you can adjust them individually by having an extra $cost check

    
    // Extra discount on shipping for orders of values of above 150 or 100.
    function tarkan_adjust_shipping_rate( $rates ) {
    
            $cart_subtotal = WC()->cart->subtotal;
    
            // Check if the subtotal is greater than value specified
            if ( $cart_subtotal >= 150 ) {
    
                    // Loop through each shipping rate
                    foreach ( $rates as $rate ) {
    
                            // Store the previous cost in $cost
                            $cost = $rate->cost;
    
                            // Adjust the cost as needed
                            // original shipping greater than 6 discount by 5 
                            if ( $cost = 6 ) {
                                    // discount rate by 5
                                    $rate->cost = $cost - 5;
                            }
                            // Optional discount for other shipping rates 
                            if ( $cost >= 10 ) {
                                    $rate->cost = $cost - 8;
                            }
                    }
            }
            return $rates;
    }
    add_filter( 'woocommerce_package_rates', 'tarkan_adjust_shipping_rate', 10 );
    
    Luminus Alabi

    (@luminus)

    Automattic Happiness Engineer

    @emzdesign,

    Feel free to check out the code that @tarkan has kindly provided for you. If that works just as you want then you’re all set.

    If you’re looking for a plugin to do this for you without needing to mess with code, you’ll want to take a look at the Table Rate Shipping extension.

    Thread Starter emzdesign

    (@emzdesign)

    @tarkan thank you so much, that works really well. However, I probably should have stated in my first post that this is for a specific Shipping Zone.

    My main shipping zone does have free delivery over £40, whereas I have a specific zone that I need the delivery to be £1 when someone spends over £40.

    This has worked for that specific shipping zone, although it has now changed the free delivery on my main shipping zone to be £1 as well.

    Really sorry, I probably should have clarified. Is it possible to modify this code to work for a specific shipping zone?

    • This reply was modified 7 years, 1 month ago by emzdesign.
    Thread Starter emzdesign

    (@emzdesign)

    Hello again

    I think I’ve managed to do it. Luckily my main shipping zone cost is £5 rather than £6, so I used the additional code for other shipping rates and managed to get it to work.

    Here is my modified code (although it’s not much different from the original) ??

    // Extra discount on shipping for orders of values of above 150 or 100.
    function tarkan_adjust_shipping_rate( $rates ) {
    
            $cart_subtotal = WC()->cart->subtotal;
    
            // Check if the subtotal is greater than value specified
            if ( $cart_subtotal >= 39.99 ) {
    
                    // Loop through each shipping rate
                    foreach ( $rates as $rate ) {
    
                            // Store the previous cost in $cost
                            $cost = $rate->cost;
    
                            // Adjust the cost as needed
                            // original shipping greater than 6 discount by 5 
                            if ( $cost >= 6 ) {
                                    // discount rate by 5
                                    $rate->cost = $cost - 5;
                            }
                            // Optional discount for other shipping rates 
                            if ( $cost >= 5 ) {
                                    $rate->cost = $cost - 5;
                            }
                    }
            }
            return $rates;
    }
    add_filter( 'woocommerce_package_rates', 'tarkan_adjust_shipping_rate', 10 );

    Thanks again for your help.

    Luminus Alabi

    (@luminus)

    Automattic Happiness Engineer

    @emzdesign

    I’m glad to see you were able to get this sorted. Thanks to @tarkan for the code provided.

    I’m going to mark this as resolved now. If you have any further questions, you can start a new thread.

    @emzdesign – No problem – it is useful code especially if you have many shipping zones and need a global way of discounting for larger orders.

    If your shipping zone has a specific price then you can only adjust that by doing if ($cost == 5 ) { … So that would only match the shipping which is £5.

    • This reply was modified 7 years, 1 month ago by tarkan.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Setting up different shipping cost if spend over X amount’ is closed to new replies.