skip to Main Content

first of all I am not a pro developer and for a customer we have the following request.
Currently, in Woocommerce the shipping cost is calcutlated on basis of the total order amount ([fee percent="8"]) –> 8% of the order amount.

The customer wants now that if a coupon is applied the shipping cost should be calculated on the basis of the original amount NOT the new total amount with the coupon amount.

Any idea to resolve that smoothly ?

Thanks !
Julian

2

Answers


  1. You need to use woocommerce_package_rates filter hook in which get total cart amount except coupon amount and changes shiiping cost for each shipping rates in loop. Let me know if you getting any issue.

    add_filter( 'woocommerce_package_rates', 'custom_shipping_costs', 10, 2 );
    function custom_shipping_costs( $rates, $package ) {
        $carttotal = WC()->cart->get_cart_subtotal();
        
        $taxes = array();
        foreach ($rates[$rate_key]->taxes as $key => $tax){
            if( $rates[$rate_key]->taxes[$key] > 0 ){
                $taxes[$key] = $carttotal * 0.08;
            }
        }
        $rates[$rate_key]->taxes = $taxes;
    }
    
    Login or Signup to reply.
  2. Here are my solutions:

    1.

    add_filter( 'woocommerce_package_rates', 'custom_shipping_costs', 20, 2 );
    function custom_shipping_costs( $rates, $package ) {
    global $woocommerce;
        
        if (!empty(WC()->cart->applied_coupons)){
        
        $totalfees = WC()->cart->get_fees();  //$order->get_total_fees();
        $producttotal = WC()->cart->subtotal; // $order->get_subtotal();
        $total = (float)$totalfees + (float)$producttotal;
        
       // New shipping cost (can be calculated)
        $new_cost = $total * .08;
        $tax_rate = array();
    
        foreach( $rates as $rate_key => $rate ){
            // Excluding free shipping methods
            if( $rate->method_id != 'free_shipping'){
    
                // Set rate cost
                $rates[$rate_key]->cost = $new_cost;
    
                // Set taxes rate cost (if enabled)
                $taxes = array();
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $rates[$rate_key]->taxes[$key] > 0 )
                        $taxes[$key] = $new_cost * $tax_rate;
                }
                $rates[$rate_key]->taxes = $taxes;
    
            }
        }
        
        return $rates;
    }
    }
    
    if(!empty( $order->get_used_coupons() )) {
    
        $totalfees = $order->get_total_fees();
        $producttotal = $order->get_subtotal();
        $total = (float)$totalfees + (float)$producttotal;
        
    
    // Get the customer country code
    $country_code = $order->get_shipping_country();
    
    // Set the array for tax calculations
    $calculate_tax_for = array(
        'country' => $country_code,
        'state' => '', // Can be set (optional)
        'postcode' => '', // Can be set (optional)
        'city' => '', // Can be set (optional)
    );
    
    // Optionally, set a total shipping amount
    $new_ship_price =  $total * .08;
    
    // Get a new instance of the WC_Order_Item_Shipping Object
    $item = new WC_Order_Item_Shipping();
    
    $item->set_method_title( "Flat rate" );
    $item->set_method_id( "flat_rate:14" ); // set an existing Shipping method rate ID
    $item->set_total( $new_ship_price ); // (optional)
    $item->calculate_taxes($calculate_tax_for);
    
    $order->add_item( $item );
    
    $order->calculate_totals();
    
    $order->update_status('on-hold');
    
    // $order->save(); // If you don't update the order status
    }
    

    Resources 1 , 2, 3, 4

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search