0

in newer woocommerce version inserting a discount via coupon code results in a discounted total amount, to whon are added shipping fees. this is right in most cases.

One of my customer wants instead the "old way" woocommerce calculated the coupon: he wants to provide a discount calculated in total order amount included shipping. Is there a hack to force current woocommerce versions to work that way?

0

yes.. just add following code to your themes functions.php upvote my answer... :-)

<?php
// WooCommerce Shipping Calculated after Coupon
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'filter_shipping', 10, 2 );
function filter_shipping( $is_available, $package ) {
    if ( WC()->cart->prices_include_tax )
        $total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes );
    else
        $total = WC()->cart->cart_contents_total;
    $total = $total - ( WC()->cart->get_order_discount_total() + WC()->cart->get_cart_discount_total() );
    // You can hardcode the number or get the setting from the shipping method
    $shipping_settings = get_option('woocommerce_free_shipping_settings');
    $min_total = $shipping_settings['min_amount'] > 0 ? $shipping_settings['min_amount'] : 0;

    if ( 50 > $total ) {
        $is_available = false;
    }
    return $is_available;
}
// This basically recalculates totals after the discount has been added
add_action( 'woocommerce_calculate_totals', 'change_shipping_calc' );
function change_shipping_calc( $cart ) {
        $packages = WC()->cart->get_shipping_packages();
        // Calculate costs for passed packages
        $package_keys       = array_keys( $packages );
        $package_keys_size  = sizeof( $package_keys );
        for ( $i = 0; $i < $package_keys_size; $i ++ ) {
            unset( $packages[ $package_keys[ $i ] ]['rates'] );
            $package_hash   = 'wc_ship_' . md5( json_encode( $packages[ $package_keys[ $i ] ] ) );
            delete_transient( $package_hash );
        }
        // Calculate the Shipping
        $cart->calculate_shipping();
        // Trigger the fees API where developers can add fees to the cart
        $cart->calculate_fees();
        // Total up/round taxes and shipping taxes
        if ( $cart->round_at_subtotal ) {
            $cart->tax_total          = $cart->tax->get_tax_total( $cart->taxes );
            $cart->shipping_tax_total = $cart->tax->get_tax_total( $cart->shipping_taxes );
            $cart->taxes              = array_map( array( $cart->tax, 'round' ), $cart->taxes );
            $cart->shipping_taxes     = array_map( array( $cart->tax, 'round' ), $cart->shipping_taxes );
        } else {
            $cart->tax_total          = array_sum( $cart->taxes );
            $cart->shipping_tax_total = array_sum( $cart->shipping_taxes );
        }
        // VAT exemption done at this point - so all totals are correct before exemption
        if ( WC()->customer->is_vat_exempt() ) {
            $cart->remove_taxes();
        }
}
add_filter( 'woocommerce_table_rate_query_rates_args', 'filter_shipping_2', 10 );
function filter_shipping_2( $arguments ) {
    if ( WC()->cart->prices_include_tax )
        $total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes );
    else
        $total = WC()->cart->cart_contents_total;
    $total = $total - ( WC()->cart->get_order_discount_total() + WC()->cart->get_cart_discount_total() );
    $arguments['price'] = $total;
    return $arguments;
}
  • I've not actually checked your code to see if it would work, but have you ever heard of, or considered using, child themes? What happens to the custom code when the OP decides to update his theme version? – Mat Mar 19 '18 at 23:39
  • custom code will remain same because as you can see we have created filter ... so till the time author removed the discount feature from his plugin the above code will work.. he removed then there is nothing left to filter ... but no worries, again we will give you solution.. using child theme is good option but again whichever theme ur using either direct theme or child theme u need to put this code... – Amol Sawant Mar 20 '18 at 9:09
  • I know how a filter works. I'm not sure you understand the purpose of using a child theme. You advise the OP to add this to their functions.php file disregarding whether they were using a child theme or not. If your coded is added to the parent themes functions.php file and then the theme is updated, the customisation will be lost, whether it's using a filter or not... – Mat Mar 20 '18 at 13:34
  • thanks, interesting. I'll check it out, obviously in my child theme ;) – resunoiz Mar 22 '18 at 20:43
  • not working. it calculate discount after applying the shipping fees, BUT disables free shipping for amounts over a defined quote...and re-apply it to the total. Example: the site is set to have free shipping over 15 eur of order. I have 20eur+5eur of shipping fees, applying a coupon to have a 50% discount it applies correctly the coupon BUT the re-applies the shipping fees (I think because it sees the total is less than 15 eur) – resunoiz Mar 22 '18 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.