Carpe_diem Posted November 29, 2023 Share Posted November 29, 2023 (edited) 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. Edited November 30, 2023 by Carpe_diem Necessary line of text (see edit history) Link to comment Share on other sites More sharing options...
QuickUpdate.net Posted November 30, 2023 Share Posted November 30, 2023 First of all in your first attempt the hook you register at install is different from the hook you actually code ("displayCarrierList" vs "ActionCarrierProcess") Link to comment Share on other sites More sharing options...
Carpe_diem Posted November 30, 2023 Author Share Posted November 30, 2023 13 minutes ago, QuickUpdate.net said: First of all in your first attempt the hook you register at install is different from the hook you actually code ("displayCarrierList" vs "ActionCarrierProcess") Its a typo as I tried several things and even if I change it to ActionCarrierProccess it does not work. Link to comment Share on other sites More sharing options...
QuickUpdate.net Posted November 30, 2023 Share Posted November 30, 2023 Ok assuming you will identify the correct hook and correct your code, this line: "$params['cart']->getPackageShippingCost($carrierIdToModify, false);" does nothing to the cart actually - it should be something like: $params['cart']['shippingcost' / 'shippingFees'] = $params['cart']->getPackageShippingCost($carrierIdToModify, false); -> you need to actually assign the value to some property of the cart (the name of the property you need to find out from the code somewhere) Link to comment Share on other sites More sharing options...
Carpe_diem Posted November 30, 2023 Author Share Posted November 30, 2023 can you give me some inputs on how to do: -> you need to actually assign the value to some property of the cart (the name of the property you need to find out from the code somewhere) based on my code Link to comment Share on other sites More sharing options...
QuickUpdate.net Posted November 30, 2023 Share Posted November 30, 2023 already gave you an example: $params['cart']['FIND_OUT_PROPERTY_NAME_YOU_NEED_TO_UPDATE'] = $params['cart']->getPackageShippingCost($carrierIdToModify, false); Link to comment Share on other sites More sharing options...
Carpe_diem Posted November 30, 2023 Author Share Posted November 30, 2023 I am new to this so that is why I am asking. Should I check in the classes/cart.php file Link to comment Share on other sites More sharing options...
QuickUpdate.net Posted November 30, 2023 Share Posted November 30, 2023 how did you write that code then? - > you need to do some kind of debugging / inspection of the ps code to figure out what is the shipping cost variable name and it should work from the code you posted / otherwise you are asking for someone else to write the code for you basically. Link to comment Share on other sites More sharing options...
Carpe_diem Posted November 30, 2023 Author Share Posted November 30, 2023 2 minutes ago, QuickUpdate.net said: how did you write that code then? - > you need to do some kind of debugging / inspection of the ps code to figure out what is the shipping cost variable name and it should work from the code you posted / otherwise you are asking for someone else to write the code for you basically. ok Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now