Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a percentage discount to Local pickup shipping method in Woocommerce

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

like image 699
Yama Avatar asked Jul 04 '18 10:07

Yama


2 Answers

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');
like image 114
Yama Avatar answered Oct 12 '22 08:10

Yama


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

like image 24
LoicTheAztec Avatar answered Oct 12 '22 08:10

LoicTheAztec