Hi guys,
I need your help I am trying to make a custom module that excludes products from free shipping for a Prestashop 1.6.
I have tried several things and here is my last attempt:
public function install()
{
return parent::install() &&
$this->registerHook('displayCarrierList');
}
public function hookActionCarrierProcess($params)
{
$carrierIdToModify = 36;
$categoryIdToExclude = 1308;
$freeShippingThreshold = 60;
if ($params['cart']->id_carrier == $carrierIdToModify) {
$hasExcludedCategoryProduct = false;
$totalExcludedCategoryValue = 0;
foreach ($params['cart']->getProducts() as $product) {
if (in_array($categoryIdToExclude, $product['id_category'])) {
$hasExcludedCategoryProduct = true;
} else {
$totalExcludedCategoryValue += $product['total_wt'];
}
}
if ($hasExcludedCategoryProduct || $totalExcludedCategoryValue >= $freeShippingThreshold) {
$params['cart']->getPackageShippingCost($carrierIdToModify, false);
$params['cart']->update();
} else {
$params['cart']->getPackageShippingCost($carrierIdToModify, true);
}
}
}
I also tried to make a override/classes/Cart.php
class Cart extends CartCore
{
public function getPackageShippingCost($id_carrier = null, $use_tax = true, Country $default_country = null, $product_list = null)
{
$carrierIdToExclude = 36;
$categoryIdToExclude = 1308;
if ($id_carrier == $carrierIdToExclude) {
$hasExcludedProducts = false;
foreach ($this->getProducts() as $product) {
if (in_array($categoryIdToExclude, $product['id_category'])) {
$hasExcludedProducts = true;
break;
}
}
if ($hasExcludedProducts || $this->getOrderTotal(true, Cart::ONLY_PRODUCTS) < 60) {
return 0; // Set shipping cost to 0 if conditions are met
}
}
return parent::getPackageShippingCost($id_carrier, $use_tax, $default_country, $product_list);
}
}
Both attempts did not work. I can't get to check if the cart has products from category 1308 or not. To make it clear my goal is If the customer has no products his cart from the category 1308 and the cart total is above 60€ he should be given free shipping. If he has a product from the category 1308 and the other products total is above 60€ then he should also get free shipping. In all other cases it should give shipping fee 5€.
Best regards.