Set a percentage discount to Local pickup shipping method in Woocommerce
Asked Answered
A

2

5

I have a WordPress site which uses WooCommerce plugin. I would like to offer buyer 5% reduction from cart total if they select local pickup as a shipping method.

I already tried - 5 * [qty] and it doesn't seem to be working.

I also tried -0.95 * [cost] with no luck

enter image description here enter image description here

Aguirre answered 4/7, 2018 at 10:38 Comment(0)
A
10

I am using WooCommerce 3 and achieved the above result by writing a function inside the function.php of active theme.

function prefix_add_discount_line( $cart ) {
  $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
  $chosen_shipping_no_ajax = $chosen_methods[0];
  if ( 0 === strpos( $chosen_shipping_no_ajax, 'local_pickup' ) ) {

    // Define the discount percentage
    $discount = $cart->subtotal * 0.05;
    // Add your discount note to cart
    $cart->add_fee( __( 'Collection discount applied', 'yourtext-domain' ) , -$discount );
  }
}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line');
Aguirre answered 4/7, 2018 at 20:12 Comment(3)
Hello, can you explain this function please? I am trying to apply it to a website but find it difficult to readWhisenhunt
To add taxation to the fee, use true as the add_fee's third parameter. $cart->add_fee( __( 'Collection discount applied', 'yourtext-domain' ) , -$discount, true );Walkthrough
Can you do the same "discount logic" but only for certain category , for example: -10% discount only to products from "category A" if local pick-up is selected as "shipping method"Klimesh
A
3

The problem with the fee API is that it always apply Taxes for negative fee (Discount) and don't care about existing coupons discounts.

The code below, will set a defined discount percentage In the shipping method "Local pickup" itself.

You will need to set a reference shipping cost with a simple initial cost instead of your formula. It can be for example 10, and will be replaced by the code discount.

You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.

The code (where you will set your discount percentage):

add_filter('woocommerce_package_rates', 'local_pickup_percentage_discount', 12, 2);
function local_pickup_percentage_discount( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    // HERE define the discount percentage
    $percentage = 5; // 5%
    $subtotal = WC()->cart->get_subtotal();

    // Loop through the shipping taxes array
    foreach ( $rates as $rate_key => $rate ){
        $has_taxes = false;
        // Targetting "flat rate"
        if( 'local_pickup' === $rate->method_id ){
            // Add the Percentage to the label name (otional
            $rates[$rate_key]->label .= ' ( - ' . $percentage . '% )';
            // Get the initial cost
            $initial_cost = $new_cost = $rates[$rate_key]->cost;
            // Calculate new cost
            $new_cost = -$subtotal * $percentage / 100;
            // Set the new cost
            $rates[$rate_key]->cost = $new_cost;

            // Taxes rate cost (if enabled)
            $taxes = [];
            // Loop through the shipping taxes array (as they can be many)
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $rates[$rate_key]->taxes[$key] > 0 ){
                    // Get the initial tax cost
                    $initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
                    // Get the tax rate conversion
                    $tax_rate    = $initial_tax_cost / $initial_cost;
                    // Set the new tax cost
                    $taxes[$key] = $new_cost * $tax_rate;
                    $has_taxes   = true; // Enabling tax
                }
            }
            if( $has_taxes )
                $rates[$rate_key]->taxes = $taxes;
        }
    }
    return $rates;
}

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

Don't forget to disable "Enable debug mode" option in shipping settings.

enter image description here

Arius answered 4/7, 2018 at 21:52 Comment(2)
At the moment the VAT/Tax is being calculated from the original subtotal (e.g. 20% of 100 = 20), and the VAT is not recalculating after discount is applied. I don't know much about tax, but surely the VAT should be recalculated after user select local pickup.Aguirre
This causes a negative shipping total which is causing the order to fail. Is there a way around this failure?Bobbie

© 2022 - 2024 — McMap. All rights reserved.