Jump to content

Show that product is already in cart on the product-list


Waroth

Recommended Posts

It's my first post here so I want to say "Hi!" to everyone.

Maybe my problem is trivial but it's not only my first contact with PrestaShop but also with SMARTY, Bootstrap, JS etc. (I'm more C/C++ unix guy...). So please be patient with me ;)

 

I've done(among other things) this:

http://mypresta.eu/en/art/developer/prestashop-quantity-field-on-product-list.html (works great - thanks Milosz Myszczuk*!)

 

But in addition to this I also want to show information(on product-list) for every product if it is already in cart(with quantity) or not.

Something like: "Already in cart: 22"

 

I was trying to extract this information same way as it is done in shopping-cart-product-line.tpl but for some reason(most probable cause - lack of knowledge) I cannot do this.

 

So here is a question:

How to do this?

 

*Albo bardziej po naszemu: Dzięki! ;)

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

The Cart class has a function named "containsProduct".  And since the FrontController already places the cart into the smarty engine, then you can do something like...

{if ({$cart->containsProduct($id_product, $id_product_attribute, $id_customization)}
Already in cart
{/if}

I did not test this code, but you should be able to play with this now

  • Like 2
Link to comment
Share on other sites

I did not test this code, but you should be able to play with this now

Thanks!

 

This works:

{if ($cart->containsProduct($product.id_product, $product.id_product_attribute))}
Already in cart
{/if}
Unfortunately I wasn't able to get quantity for specified product(Its easy to get quantity of everything from cart - nbProducts()).

Also it would be great to make input "quantity" box as user-friendly as in cart (shopping-cart.tpl) where you can change quantity and this instantaneously change value in cart on top of the page.

 

In other words - how to make this code from shopping-cart-product-line.tpl:

{code]{if !isset($customizedDatas.$productId.$productAttributeId) OR $quantityDisplayed > 0}

<input type="hidden" value="{if $quantityDisplayed == 0 AND isset($customizedDatas.$productId.$productAttributeId)}{$customizedDatas.$productId.$productAttributeId|@count}{else}{$product.cart_quantity-$quantityDisplayed}{/if}" name="quantity_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}_hidden" />
<input size="2" type="text" autocomplete="off" class="cart_quantity_input form-control grey" value="{if $quantityDisplayed == 0 AND isset($customizedDatas.$productId.$productAttributeId)}{$customizedDatas.$productId.$productAttributeId|@count}{else}{$product.cart_quantity-$quantityDisplayed}{/if}" name="quantity_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" />
<div class="cart_quantity_button clearfix">
{if $product.minimal_quantity < ($product.cart_quantity-$quantityDisplayed) OR $product.minimal_quantity <= 1}
<a rel="nofollow" class="cart_quantity_down btn btn-default button-minus" id="cart_quantity_down_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" href="{$link->getPageLink('cart', true, NULL, "add=1&id_product={$product.id_product|intval}&ipa={$product.id_product_attribute|intval}&id_address_delivery={$product.id_address_delivery|intval}&op=down&token={$token_cart}")|escape:'html':'UTF-8'}" title="{l s='Subtract'}">
<span><i class="icon-minus"></i></span>
</a>
{else}
<a class="cart_quantity_down btn btn-default button-minus disabled" href="#" id="cart_quantity_down_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" title="{l s='You must purchase a minimum of %d of this product.' sprintf=$product.minimal_quantity}">
<span><i class="icon-minus"></i></span>
</a>
{/if}
<a rel="nofollow" class="cart_quantity_up btn btn-default button-plus" id="cart_quantity_up_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}" href="{$link->getPageLink('cart', true, NULL, "add=1&id_product={$product.id_product|intval}&ipa={$product.id_product_attribute|intval}&id_address_delivery={$product.id_address_delivery|intval}&token={$token_cart}")|escape:'html':'UTF-8'}" title="{l s='Add'}"><span><i class="icon-plus"></i></span></a>
</div>
{/if}
work in product-list.tpl? Where those variables are initialized?

 

Sorry for trivial questions and thanks again for your example!

 

//EDIT

 

I wrote two methods in Cart.php:

	public function nbOfOneProduct($id_product)
	{
		if (!$this->id)
			return 0;

		return Cart::getNbOfOneProduct($this->id, $id_product);
	}

	public static function getNbOfOneProduct($id, $id_product)
	{
		$temp = (int)Db::getInstance()->getValue('
			SELECT SUM(`quantity`)
			FROM `'._DB_PREFIX_.'cart_product`
			WHERE `id_cart` = '.(int)$id.'
			AND `id_product` = '.(int)$id_product
		);

		return $temp;
}
+this thing in product-list:

						{if ($cart->containsProduct($product.id_product, $product.id_product_attribute))}
							Already in cart: {$cart->nbOfOneProduct($product.id_product)}
						{/if}
Bu i think it's not the best solution... Edited by Waroth (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 2 years later...

... and in product-tpl.pl :

<!-- check if you have this product in cart -->
{$idproductincart = Tools::getValue('id_product')}
{$idproductatrincart = Tools::getValue('id_product_attribute')}
{if ($cart->containsProduct({$idproductincart}, {$idproductatrincart}))}
<div class="alert alert-info">{l s='Already in cart'}</div>
{/if}
<!-- / check if you have this product in cart -->
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...