Saturday, May 2, 2015

Prestashop minimum quantity product by customer group

I'm working on prestashop 1.5 that invalidates quantities under 10 for each product for professionals, note that profesionnals are customers linked to groups with ID 4 or 5.

the best solution i'did is:

1- Override CartController under override/controllers/front/CartController
2- Added this little snipet: (where 4 and 5 are groups ids) (target method is processChangeProductInCart then add your controle)
  $arr = $this->context->customer->getGroups();
                $minQuantity = -1;
                if(in_array(4,$arr) || in_array(5,$arr)) $minQuantity=10;
                if($qty_to_check<$minQuantity)
                   $this->errors[] = Tools::displayError('Minimum order quantity is 10 for profesionnals');

3- be careful to add this code out of an if surrount that does not cover all scenarios, in our cas i've added it just after stock controle.
4- you can remark that later in the controller there's another quantity check made by prestashop using var called minimum_quantity, i didn't altered that code, i think it'sb not quiet careful to do it, since custom code have to be clear and separated from original code to avoid unwanted results

i hop it helps