Objectives of the blog
After reading this blog we will be able to calculate Free Shipping based on final Total Price of Cart.
Problem Statement
Presently, when we add products into the cart and move to the checkout section to avail discount and shipping then we see that discount applies after shipping.
Let’s say,
- Cart total amount is $100 which avail Free Shipping and some discount of 5% is also added
- Then after discount Total Amount of cart set to below $100 but still Free Shipping applies which should not happen. Free Shipping must apply on the Final Total amount of cart after discount.
- This is the default process of Prestashop. Refer the screenshot below where shipping must be Free if Order Total is > 50 but it has applied Free Shipping while Order Total is < 50 after discount –
Why the current issue is there:
Current issue is there because shipping amount is being calculated on the Order Total amount which is set into the Cart before applying discount. You can check the lines of code below where Order Total without discount amount is being used to calculate shipping –
$shipping_cost += $carrier->getDeliveryPriceByPrice($the_total_price, $id_zone, (int)$this->id_currency);
Requirement
We need to modify the below mentioned file to fix the same. We will be overriding the corresponding lines of class file to avoid any changes in Core files.
Below is the list of class file which will be overridden-
1. Root dir\classes\Cart.php
Procedure to Solve the issue-
Follow the below mentioned steps –
1. Rewrite function getPackageShippingCost
2. After the line given below –
// Order total in default currency without fees $order_total = $this->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING, $product_list);
3. Add following statement –
$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);
4. Replace the lines mentioned below –
Replace this line
$check_delivery_price_by_price = Carrier::checkDeliveryPriceByPrice($row['id_carrier'], $total_order, (int)$id_zone, (int)$this->id_currency);
With
$check_delivery_price_by_price = Carrier::checkDeliveryPriceByPrice($row['id_carrier'], $price_to_apply_shipment, (int)$id_zone, (int)$this->id_currency);
————AND—————–
Replace this line
$shipping_cost += $carrier->getDeliveryPriceByPrice($the_total_price, $id_zone, (int)$this->id_currency);
With
$shipping_cost += $carrier->getDeliveryPriceByPrice($price_to_apply_shipment, $id_zone, (int)$this->id_currency);
5. Clear the cache and run.
Refer the screenshot below after applying changes into the code –
Summary
After following the above procedure, you will be able to calculate Shipping on the Final Total amount of Cart.