ayin Posted September 25, 2014 Share Posted September 25, 2014 (edited) 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 September 25, 2014 by ayin (see edit history) Link to comment Share on other sites More sharing options...
ayin Posted September 29, 2014 Author Share Posted September 29, 2014 Is it even possible? In theory should be doable. Any hints? Link to comment Share on other sites More sharing options...
NemoPS Posted September 29, 2014 Share Posted September 29, 2014 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) 1 Link to comment Share on other sites More sharing options...
ayin Posted October 1, 2014 Author Share Posted October 1, 2014 (edited) 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 October 1, 2014 by ayin (see edit history) Link to comment Share on other sites More sharing options...
[email protected] Posted October 3, 2014 Share Posted October 3, 2014 Also interesting. Link to comment Share on other sites More sharing options...
NemoPS Posted October 3, 2014 Share Posted October 3, 2014 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'); 1 Link to comment Share on other sites More sharing options...
ayin Posted October 3, 2014 Author Share Posted October 3, 2014 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 More sharing options...
NemoPS Posted October 4, 2014 Share Posted October 4, 2014 Well no, you should first go through products $(jsonData.products).each(function(index, product){ }); Inside it, you can try using my method. As for those functions, they are jquery's http://api.jquery.com/parents/ http://api.jquery.com/addclass/ 1 Link to comment Share on other sites More sharing options...
ayin Posted October 6, 2014 Author Share Posted October 6, 2014 (edited) 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. 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 October 6, 2014 by ayin (see edit history) Link to comment Share on other sites More sharing options...
NemoPS Posted October 8, 2014 Share Posted October 8, 2014 Unless you re-parse that part in a php file, then send it though ajax,no. You can try adding those variables via js as well, though I have to clue at this stage on how to 1 Link to comment Share on other sites More sharing options...
ayin Posted October 8, 2014 Author Share Posted October 8, 2014 Hey, So in your experience, what would you do to refresh the page after user input (add to cart)? JQuery and implement the logic there? Thanks Link to comment Share on other sites More sharing options...
NemoPS Posted October 10, 2014 Share Posted October 10, 2014 window.location.href = "mylink"; This will refresh the page 1 Link to comment Share on other sites More sharing options...
ayin Posted October 13, 2014 Author Share Posted October 13, 2014 (edited) Thanks all for your help. I have learned a lot! Hopefully future Prestashpopers might find this thread useful : ) Edited October 13, 2014 by ayin (see edit history) Link to comment Share on other sites More sharing options...
ayin Posted October 13, 2014 Author Share Posted October 13, 2014 (edited) 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 October 13, 2014 by ayin (see edit history) Link to comment Share on other sites More sharing options...
NemoPS Posted October 15, 2014 Share Posted October 15, 2014 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 1 Link to comment Share on other sites More sharing options...
ayin Posted October 31, 2014 Author Share Posted October 31, 2014 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 More sharing options...
NemoPS Posted November 1, 2014 Share Posted November 1, 2014 Correct, and then processChangeProductInCart Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now