TonyKapa Posted May 25, 2022 Share Posted May 25, 2022 Hello! Im using default shop - version 1.7.7.7 I want to create a module where I call a function in Javascript when a product is added to cart. So far my code is this public function hookActionCartSave() { $cart = $this->context->cart; $products_cart = Context::getContext()->cart->getProducts(); foreach ($products_cart as $pr) { $this->context->smarty->assign(array( 'id_product' => $pr['id_product'], 'price' => $pr['total_wt'], 'name' => $pr['name'], 'discount' => $pr['reduction'], 'brand' => $pr['manufacturer_name'], 'category' => $pr['category'], 'quantity' => $pr['quantity'] )); } return $this->display(__FILE__ , 'views/templates/hook/addtocart.tpl') ; } And my tpl {literal} <script> document.addEventListener('DOMContentLoaded', function () { dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: "add_to_cart", ecommerce: { items: [ { item_id: {/literal}"{$id_product}"{literal}, item_name: {/literal}"{$name}"{literal}, currency: "EUR", discount: {/literal}{$discount}{literal}, item_brand: {/literal}"{$brand}"{literal}, item_category: {/literal}"{$category}"{literal}, price: {/literal}{$price}{literal}, quantity: {/literal}{$quantity}{literal} } ] } }); }, false); </script> {/literal} The problem is that when I add a product to cart nothing happens - the cart isnt updating (only if i refresh the page) - hook code isnt running I also tried hookActionCartUpdateQuantityBefore and it's not working either. Code is running fine in other hooks ( displayheader , displayleftcolumn ). I am fairly new to php and module development so tell me if theres a more optimal way to do it. Thanks in advance! Link to comment Share on other sites More sharing options...
Ress Posted May 25, 2022 Share Posted May 25, 2022 It's not quite right what you're doing, you return that tpl, but that hook is an action hook, it doesn't return anything to the front end, so it will never run that js for you. https://devdocs.prestashop.com/1.7/themes/reference/javascript-events/ You have the "updateCart" event, which is triggered after you add a product to the cart. You can bind to it, do an ajax, there you can find out the last product added to the cart (you have a function in the cart class). After you have found the product, you prepare all your data (brand, category, etc), and send an array of them back, after which you can run that code. Link to comment Share on other sites More sharing options...
TonyKapa Posted May 25, 2022 Author Share Posted May 25, 2022 (edited) Thank you, you helped me a lot, I didnt even know prestashop had javascript events. I managed to save the cart as an object and get the data i wanted but if you could help me with the ajax and the function you mentioned that would be great. Edited May 25, 2022 by TonyKapa (see edit history) Link to comment Share on other sites More sharing options...
Ress Posted May 26, 2022 Share Posted May 26, 2022 I'm attaching a sample, which of course you have to adapt. Javascript if (typeof (prestashop) != 'undefined') { $(document).ready(function() { prestashop.on('updateCart', function() { $.ajax({ url: 'your_front_controller_url', type: 'POST', dataType: 'json', success: function (data) { // in the "data" variable you will have the data of the product that you will add in your code } }); }); }); } PHP //this is how you bring the data of the last product added to the cart. you make an array of what you need, and send them back to the front $productDetails = $this->context->cart->getLastProduct(); Link to comment Share on other sites More sharing options...
TonyKapa Posted May 27, 2022 Author Share Posted May 27, 2022 Thank you, you helped me so much, i managed to do what i want. Is there a similar function or a way to get the product details when it is removed from the cart? 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