Search code examples
prestashopprestashop-1.7prestashop-helper-classes

Prestashop 1.7.4.2 - free shipping based on final total price


I need shipping calculation after discount applied

Ex.
Total cart : 65$
Free shipping start : 65$
Discount : 5%

After discount it become under 65$ and still get free shipping I need it to calculate discount before shipping


Solution

  • First set your ranges by carrier (0-65: X EUR, above 65: 0 EUR)

    Do an override of the class Cart.php

    Rewrite function getPackageShippingCost
    

    After line

    // Order total in default currency without fees
    $order_total = $this->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING, $product_list);
    

    Add this

    //Recalcul total pour panier et livraison gratuite 
    $listeDiscounts = $this->getCartRules();        
    $total_discounts = 0;
    if (is_array($listeDiscounts)) {
        if (isset($listeDiscounts[0]['value_real']))
            $total_discounts = $listeDiscounts[0]['value_real'];            
    }
    $price_to_apply_shipment = floatval($order_total) - floatval($total_discounts);
    

    Replace

    //$check_delivery_price_by_price = Carrier::checkDeliveryPriceByPrice($row['id_carrier'], $total_order, (int)$id_zone, (int)$this->id_currency);
    by                    
    $check_delivery_price_by_price = Carrier::checkDeliveryPriceByPrice($row['id_carrier'], $price_to_apply_shipment, (int)$id_zone, (int)$this->id_currency);
    

    And

    //$shipping_cost += $carrier->getDeliveryPriceByPrice($the_total_price, $id_zone, (int)$this->id_currency);
    By 
    $shipping_cost += $carrier->getDeliveryPriceByPrice($price_to_apply_shipment, $id_zone, (int)$this->id_currency);
    

    Clear the cache and run