Jump to content

How to highlight products that are, already, in shopping cart?


Recommended Posts

Hello,

 

I wonder if you guys could give me some hints and tips to highlight products that are in the customer's shopping cart and be updated/refreshed everytime the customer add a new product.

 

I was looking at produt-list.tpl and i figured out that:

 

    1) I can change the CSS to show a different layout for images already in cart;

 

        1.1) {if} inCart() then class=A, else class=B {/if}

 

        1.2)  Using PHP, I can, maybe, create a static method in Cart class that returns wheter a ProductId is in the cart.

 

        1.3) HOWEVER, after reviewing the source code, I still have no clue of how the "click" on 'Add to cart' button works. I mean, the product-list.tpl is invoked again to refresh the page? Only the "hooked" Cart module / box is refreshed?

 

If you guys could give me some hints it would be awesome.

Thanks

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

I am not sure I understood your question, you mean you need to know if a product is in the cart already, while clicking add to cart?
As a starting point, if you use the ajax cart you should inspect the "add" method of ajax-cart.js (theme folder/js/blockcart/js, or blockcart/js)

  • Like 1
Link to comment
Share on other sites

I am not sure I understood your question, you mean you need to know if a product is in the cart already, while clicking add to cart?

As a starting point, if you use the ajax cart you should inspect the "add" method of ajax-cart.js (theme folder/js/blockcart/js, or blockcart/js)

 

And, if the product is cart already, then change the class (.tpl) of the product's image so it appear as "Selected"/"Highlighted".

 

 

EDIT: I inspected the ajax-cart.js and the .tpl files and i got a better understanding of the funcionality.

What i don't understand yet, is how can i tell the "Product-list.tpl" page to refresh and "style" the product image according to its existance in the cart.

 

 

Thanks for the help

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

after grabbing the json data with all cart products (and it seems you did). Use an each look to go through them. Inside that, something like (use the correct variable, I used product.id_product as an example)

 

$('a[data-id-product=+product.id_product+]').parents('.ajax_block_product').addClass('myclass');

  • Like 1
Link to comment
Share on other sites

after grabbing the json data with all cart products (and it seems you did). Use an each look to go through them. Inside that, something like (use the correct variable, I used product.id_product as an example)

 

$('a[data-id-product=+product.id_product+]').parents('.ajax_block_product').addClass('myclass');

 

 

Hi Nemo1,

 

I am a bit confused how the code you suggested should work.

 

1. Should i put it in line ~363 of the file "ajax-cart.js", at the end of the funcion add(), in the end of the sub-funcion successful()?

2. Can you link me some guides/documentation to better interpret your statment? These methods "parents()" and "addClass" must be written somewhere, right?

      $('a[data-id-product=+product.id_product+]').parents('.ajax_block_product').addClass('myclass');"

3. I tried to put the code as in step 1, with no visual results.

 

 

Really glad that you can help : )

Regards

Link to comment
Share on other sites

Thanks all for the help.
I managed to learn JQuery and change the class(CSS) of some HTML tags whenever an Item is added to my Cart.
 
Thanks all for help
 

=========================== 
EDIT: Another question.   :unsure:

Is there a way to re-evaluate the .tpl file?
I mean, when i click the "add-to-cart" button, it should re-evaluate my following php/.tpl code:

<a id="alreadyInCart{$product.id_product|intval}">
	{if
		($cart->containsProduct($product.id_product, $product.id_product_attribute))
	}
		{l s="Already in cart"}: {$cart->getNbOfOneProduct($product.id_product)}
	{/if}
</a>

Or this is only achieved through PHP code in "cart-ajax.js" file?

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

Hello,

 

Now i have a question regarding AJAX. It seems that using ajax i ought to be able to refresh my "Product-List" page without using "location.reload".

 

 

 Reading the code of "ajax-cart-js" in line ~300 it has the following code:

		$.ajax({
			type: 'POST',
			headers: { "cache-control": "no-cache" },
			url: baseUri + '?rand=' + new Date().getTime(),
			async: true,
			cache: false,
			dataType : "json",
			data: 'controller=cart&add=1&ajax=true&qty=' + ((quantity && quantity != null) ? quantity : '1')
					+ '&id_product=' + idProduct 
					+ '&token=' + static_token
					+ ( (parseInt(idCombination) && idCombination != null) ? '&ipa=' + parseInt(idCombination): ''),

Can you, please, tell me how Prestashop knows what PHP file uses to handle this request?

 

Thanks

 

 

EDIT: by testing, i think, somehow, it calls the following function from "CartController.php"... but i still dont understand how and why

 

 /**
  * This process add or update a product in the cart
  */
 protected function processChangeProductInCart()
Edited by ayin (see edit history)
Link to comment
Share on other sites

  • 3 weeks later...

It's this one

 

baseUri

 

Defined in header.tpl, sending these values

 

data: 'controller=cart&add=1&ajax=true&qty=' + ((quantity && quantity != null) ? quantity : '1')

 

That is why it calls the cart controller

 

Hi,

 

I am looking in the CartController.php file and, if I am not mistaken the AJAX POST with that data = "controller=cart&...." will invoke the method CartControllerCore."public function postProcess()"?

 

Thanks

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