Jump to content

How to change behaviour of Cart/Controller


3cubes

Recommended Posts

Hello friends,

 

I have a technical issue I hope to solve with your help:

 

When you add  a product into cart it is done by the CartController and its Cart Class. Within the Cart class there is a method called updateQty. Within this function there is a check if cart already contains product or not. If cart contains product then increase its quantity when operator == up and decrease its quantity when operator == down, else if cart does not contain product then add product to cart when operator == up.

 

This is the certain function mentioned above from Cart.php

            /* 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;
					$qty = '+ '.(int)$quantity;

					if (!Product::isAvailableWhenOutOfStock((int)$result2['out_of_stock']))
						if ($new_qty > $product_qty)
							return false;
				}
				else if ($operator == 'down')
				{
					$qty = '- '.(int)$quantity;
					$new_qty = (int)$result['quantity'] - (int)$quantity;
					if ($new_qty < $minimal_quantity && $minimal_quantity > 1)
						return -1;
				}
				else
					return false;

				/* Delete product from cart */
				if ($new_qty <= 0)
					return $this->deleteProduct((int)$id_product, (int)$id_product_attribute, (int)$id_customization);
				else if ($new_qty < $minimal_quantity)
					return -1;
				else
					Db::getInstance()->execute('
						UPDATE `'._DB_PREFIX_.'cart_product`
						SET `quantity` = `quantity` '.$qty.', `date_add` = NOW()
						WHERE `id_product` = '.(int)$id_product.
						(!empty($id_product_attribute) ? ' AND `id_product_attribute` = '.(int)$id_product_attribute : '').'
						AND `id_cart` = '.(int)$this->id.(Configuration::get('PS_ALLOW_MULTISHIPPING') && $this->isMultiAddressDelivery() ? ' AND `id_address_delivery` = '.(int)$id_address_delivery : '').'
						LIMIT 1'
					);
			}
			/* Add product to the cart */
			elseif ($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);

				// Quantity for product pack
				if (Pack::isPack($id_product))
					$result2['quantity'] = Pack::getQuantity($id_product, $id_product_attribute);

				if (!Product::isAvailableWhenOutOfStock((int)$result2['out_of_stock']))
					if ((int)$quantity > $result2['quantity'])
						return false;

				if ((int)$quantity < $minimal_quantity)
					return -1;

				$result_add = Db::getInstance()->insert('cart_product', array(
					'id_product' => 			(int)$id_product,
					'id_product_attribute' => 	(int)$id_product_attribute,
					'id_cart' => 				(int)$this->id,
					'id_address_delivery' => 	(int)$id_address_delivery,
					'id_shop' => 				$shop->id,
					'quantity' => 				(int)$quantity,
					'date_add' => 				date('Y-m-d H:i:s'),
					'customprice' =>			(float)$customprice,
				));

				if (!$result_add)
					return false;
			}

This is cart's logical behaviour because if you already have a product in the cart and lets say you press again add-to-cart button for same product, it has same result as if you would press the plus-button in cart for increasing product quantity. It would be bad behaviour if pretashop would add same product to cart twice.

 

Everything is fine so far if you use Prestashop the regular way when every of your products has a fix price set in backoffice.

 

But in may case customer can add product to cart with a custom price. So now when you add product into cart with a custom price and you go back to product page and add same product but with a different custom price then of course cart acting like this:

 

It only increases the quantity of the first product you already added to cart because this product already exists in cart. The second custom price is beeing ignored.

 

Somebody has any idea in which file I have to change things to tell the cart controller that when same product is added to cart BUT with different price it should not increase the quantity of the first item instead controller should add the product again into cart like it would be a different product!!???

 

Any help is appreciated

 

Best regards

 

P.S. I am using Prestashop Version 1.5.6.2

Edited by 3cubes (see edit history)
Link to comment
Share on other sites

  • 9 months later...

I too was looking for how to solve this problem.
And I have not found any solution around, so I was forced to find it myself.
 
You have to make these small changes:
 
 
Inside the function getProducts() says this line
 
 

// Build GROUP BY
$sql->groupBy('unique_id');
// Build GROUP BY
// $sql->groupBy('unique_id');

 
While in the function updateQty() you must apply this small change
 
 

/* Update quantity if product already exist */
if ($result)
{
/* Update quantity if product already exist */
if (!$result)
{
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...