Jump to content

3 logical operators not working.


oisiaoeh
Go to solution Solved by requinix,

Recommended Posts

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).

Link to comment
Share on other sites

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.  

Link to comment
Share on other sites

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 by requinix
please use [code] tags when posting code
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 !!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.