Jump to content

hookActionCartSave is not working


Recommended Posts

Hello! Im using default shop - version

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)
            '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 

    document.addEventListener('DOMContentLoaded', function () {
    dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
    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);

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

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.


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

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 by TonyKapa (see edit history)
Link to comment
Share on other sites

I'm attaching a sample, which of course you have to adapt.


if (typeof (prestashop) != 'undefined') {
    $(document).ready(function() {
        prestashop.on('updateCart', function() {
                  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


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

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