Jump to content

Cart.php add - addCartRule() function


edouard.p

Recommended Posts

Hi all,

 

I'm pretty new to Prestashop. One of my client recently asked me to have a look on bugs on a Prestashop (1.5.5.0) website, so I'm trying to understand how it works.

 

On of the bugs is: when a cart rule is applied to the cart, no others can be applied in addition, as a result only on cart rule can be applied. Even if all cart rules have been set to be compatible with all others.

 

So I analysed Prestashop code to understand the mechanism of adding a cart rule to the cart, and there is a line a can't understand in addCartRule() from Cart class (Cart.php).

	public function addCartRule($id_cart_rule)
	{
		// You can't add a cart rule that does not exist
		$cartRule = new CartRule($id_cart_rule, Context::getContext()->language->id);
		if (!Validate::isLoadedObject($cartRule))
			return false;
		
		if (Db::getInstance()->getValue('SELECT id_cart_rule FROM '._DB_PREFIX_.'cart_cart_rule WHERE id_cart = '.(int)$this->id))
			return false;
			
		// Add the cart rule to the cart
		if (!Db::getInstance()->insert('cart_cart_rule', array(
			'id_cart_rule' => (int)$id_cart_rule,
			'id_cart' => (int)$this->id
		)))
			return false;

		Cache::clean('Cart::getCartRules'.$this->id.'-'.CartRule::FILTER_ACTION_ALL);
		Cache::clean('Cart::getCartRules'.$this->id.'-'.CartRule::FILTER_ACTION_SHIPPING);
		Cache::clean('Cart::getCartRules'.$this->id.'-'.CartRule::FILTER_ACTION_REDUCTION);
		Cache::clean('Cart::getCartRules'.$this->id.'-'.CartRule::FILTER_ACTION_GIFT);
	
		if ((int)$cartRule->gift_product)
			$this->updateQty(1, $cartRule->gift_product, $cartRule->gift_product_attribute, false, 'up', 0, null, false);

		return true;
	}

To me this line:

if (Db::getInstance()->getValue('SELECT id_cart_rule FROM '._DB_PREFIX_.'cart_cart_rule WHERE id_cart = '.(int)$this->id))
   return false; 

checks if there is already a rule for the current cart. But it doesn't check if the current rule is already applied, it just return false if there is already a rule for this cart.

 

Am I wrong ?

So, to me, as a result, if there is already a rule applied to this cart, no others can be added because these lines will return false every time.

 

I understand that the idea here is to avoid to add a rule which is already applied to the cart, but I think a condition like 'AND id_cart_rule = ' . (int) $id_cart_rule is missing.

Of course I might be completely wrong as I still new to Prestashop, that's why I would much appreciate some explaination about these lines.
I'm also sorry to ask question about core code of an outdated version...

Thanks a lot guys !

Edouard

Edited by edouard.p (see edit history)
Link to comment
Share on other sites

You are exactly right. It's a bug that's been fixed in later versions of PrestaShop. Here's what those lines look like in PrestaShop v1.6.1.6:

        if (Db::getInstance()->getValue('SELECT id_cart_rule FROM '._DB_PREFIX_.'cart_cart_rule WHERE id_cart_rule = '.(int)$id_cart_rule.' AND id_cart = '.(int)$this->id)) {
            return false;
        }
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...