oisiaoeh Posted February 16, 2018 Share Posted February 16, 2018 I wanted to limit purchase of product id 618 to 1 so i added this statement. if ($quantity > 1 && $id_product =='618') Now i have another item which i want to limit the max quantity purchase so i changed the code to if ($quantity > 1 && ($id_product =='618' or $id_product =='625')) Now this is not working Can any one help me to check this or should there be a better way to write this code if there is a need to add more products next time, by the way this is code from prestashop and product id 625 has multiple attributes (not sure if that is the problem). Quote Link to comment Share on other sites More sharing options...
dodgeitorelse3 Posted February 16, 2018 Share Posted February 16, 2018 (edited) Create an array to hold all the limited id's then in your if statement check each id to see if it is in the limited id array Edited February 16, 2018 by dodgeitorelse3 Quote Link to comment Share on other sites More sharing options...
oisiaoeh Posted February 16, 2018 Author Share Posted February 16, 2018 Create an array to hold all the limited id's then in your if statement check each id to see if it is in the limited id array but mean while what is incorrect with the above statement? i am able to add more than 1 quantity for both the product ids Quote Link to comment Share on other sites More sharing options...
dodgeitorelse3 Posted February 16, 2018 Share Posted February 16, 2018 Remove single quotation marks at 618 and 625 Quote Link to comment Share on other sites More sharing options...
Barand Posted February 16, 2018 Share Posted February 16, 2018 Does "||" work better than "or" for you? Quote Link to comment Share on other sites More sharing options...
oisiaoeh Posted February 16, 2018 Author Share Posted February 16, 2018 Does "||" work better than "or" for you? Remove single quotation marks at 618 and 625 Done both and it is still not working, i even tried changing to other product id just in case it is the product attribute that is causing this problem. Quote Link to comment Share on other sites More sharing options...
dodgeitorelse3 Posted February 16, 2018 Share Posted February 16, 2018 What is the rest of the if statement? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted February 16, 2018 Share Posted February 16, 2018 the $id_product variable probably doesn't contain what you think (white-space, null, a completely different value..) what does adding var_dump($id_product); die; immediately before the if(...) statement show? also, what exact symptom or error are you getting that leads you to believe this isn't working? there could be a problem somewhere else that's causing you to incorrectly conclude this statement is the source of a problem. Quote Link to comment Share on other sites More sharing options...
oisiaoeh Posted February 16, 2018 Author Share Posted February 16, 2018 (edited) if ((int)$quantity <= 0) { return $this->deleteProduct($id_product, $id_product_attribute, (int)$id_customization, 0, $auto_add_cart_rule); } elseif (!$product->available_for_order || (Configuration::get('PS_CATALOG_MODE') && !defined('_PS_ADMIN_DIR_'))) { return false; } elseif ($quantity > 1 && ($id_product ==618 || $id_product ==888)) { // For example, 4 max per same product/product-combination allowed return false; } else { /* Check if the product is already in the cart */ $result = $this->containsProduct($id_product, $id_product_attribute, (int)$id_customization, (int)$id_address_delivery); /* Update quantity if product already exist */ if ($result) { if ($operator == 'up') { $sql = 'SELECT stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity FROM '._DB_PREFIX_.'product p '.Product::sqlStock('p', $id_product_attribute, true, $shop).' WHERE p.id_product = '.$id_product; $result2 = Db::getInstance()->getRow($sql); $product_qty = (int)$result2['quantity']; // Quantity for product pack if (Pack::isPack($id_product)) { $product_qty = Pack::getQuantity($id_product, $id_product_attribute); } $new_qty = (int)$result['quantity'] + (int)$quantity; if ($quantity > 1 && ($id_product ==618 || $id_product ==888)) { return false; } $qty = '+ '.(int)$quantity; Edited February 16, 2018 by requinix please use [code] tags when posting code Quote Link to comment Share on other sites More sharing options...
oisiaoeh Posted February 16, 2018 Author Share Posted February 16, 2018 the $id_product variable probably doesn't contain what you think (white-space, null, a completely different value..) what does adding var_dump($id_product); die; immediately before the if(...) statement show? also, what exact symptom or error are you getting that leads you to believe this isn't working? there could be a problem somewhere else that's causing you to incorrectly conclude this statement is the source of a problem. before this when i am just limiting the quantity to 1 product id, there will be a error message shown in checkout page telling me to change the quantity and does not allow me to add more than 1 to the quantity, therefore i know it is working. after editing the above code, i no longer get the error message and i can add more than 1 quantity to both items when checkout. Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted February 16, 2018 Solution Share Posted February 16, 2018 Are you sure you didn't change anything else? What if you delete the addition and have that one line to back to how it was before? Quote Link to comment Share on other sites More sharing options...
oisiaoeh Posted February 16, 2018 Author Share Posted February 16, 2018 Are you sure you didn't change anything else? What if you delete the addition and have that one line to back to how it was before? OMG, you are a lifesaver, you are right, i simply find and replace without realising that the next if statement was a different variable. $new_qty = (int)$result['quantity'] + (int)$quantity; if ($quantity > 1 && ($id_product ==618 || $id_product ==888)) variable in bold shoud be $new_qty now everything is working fine as per normal ! Thank you all of you guys, really really appreciate your help !! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.