Jump to content

How to exclude products from free shipping PS 1.6


Recommended Posts

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 by Carpe_diem
Necessary line of text (see edit history)
Link to comment
Share on other sites

 

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...