Jump to content

PS 1.6 Module trigger event on Ajax Add to cart


Recommended Posts

Hi all, 

 

I've been studying how PrestaShop modules work lately and it would be great if someone could point me in the right direction.

I want to trigger an event (javascript method to be exact, let's call it "MyShop") that would get all the product details in it.

I have some older code but either it was working with older version of PS either it's only for normal add to cart and not ajax. 

The issue is that it doesn't even get triggered at all after pressing "add to cart" button, it's actually a piece of code from Google Analytics module. 

 

Here is a piece of modified code from my module's main .php file that is supposed to get triggered on the add to cart, it's using "hookActionCartSave()" but I am not sure if that is still available in 1.6 or got replaced by something else or whether it just simply doesn't work with ajax and I should look elsewhere? 

 

 /**
         * hook save cart event to implement addtocart and remove from cart functionality
         */
        public function hookActionCartSave()
        {
            if (!isset($this->context->cart))
                return;
            
            $cart = array(
                          'controller' => Tools::getValue('controller'),
                          'addAction' => Tools::getValue('add') ? 'add' : '',
                          'removeAction' => Tools::getValue('delete') ? 'delete' : '',
                          'extraAction' => Tools::getValue('op'),
                          'qty' => (int)Tools::getValue('qty', 1)
                          );
            
            $cart_products = $this->context->cart->getProducts();
            
            if (isset($cart_products) && count($cart_products))
                foreach ($cart_products as $cart_product)
                if ($cart_product['id_product'] == Tools::getValue('id_product'))
                    $add_product = $cart_product;
            
            if ($cart['removeAction'] == 'delete')
            {
                $add_product_object = new Product((int)Tools::getValue('id_product'), true, (int)Configuration::get('PS_LANG_DEFAULT'));
                if (Validate::isLoadedObject($add_product_object))
                {
                    $add_product['name'] = $add_product_object->name;
                    $add_product['manufacturer_name'] = $add_product_object->manufacturer_name;
                    $add_product['category'] = $add_product_object->category;
                    $add_product['reference'] = $add_product_object->reference;
                    $add_product['link_rewrite'] = $add_product_object->link_rewrite;
                    $add_product['link'] = $add_product_object->link_rewrite;
                    $add_product['price'] = $add_product_object->price;
                    $add_product['ean13'] = $add_product_object->ean13;
                    $add_product['id_product'] = Tools::getValue('id_product');
                    $add_product['id_category_default'] = $add_product_object->id_category_default;
                    $add_product['out_of_stock'] = $add_product_object->out_of_stock;
                    $add_product = Product::getProductProperties((int)Configuration::get('PS_LANG_DEFAULT'), $add_product);
                }
            }
            
            if (isset($add_product) && !in_array((int)Tools::getValue('id_product'), self::$products))
            {
                self::$products[] = (int)Tools::getValue('id_product');
                $ga_products = $this->wrapProduct($add_product, $cart, 0, true);
                if (array_key_exists('id_product_attribute', $ga_products) && $ga_products['id_product_attribute'] != '' && $ga_products['id_product_attribute'] != 0)
                    $id_product = $ga_products['id_product_attribute'];
                else
                    $id_product = Tools::getValue('id_product');
                if (isset($this->context->cookie->MyShop_cart))
                    $MyShop_cart = unserialize($this->context->cookie->MyShop_cart);
                else
                    $MyShop_cart = array();
                if ($cart['removeAction'] == 'delete')
                    $ga_products['quantity'] = -1;
                elseif ($cart['extraAction'] == 'down')
                {
                    if (array_key_exists($id_product, $MyShop_cart))
                        $ga_products['quantity'] = $MyShop_cart[$id_product]['quantity'] - $cart['qty'];
                    else
                        $ga_products['quantity'] = $cart['qty'] * -1;
                }
                elseif (Tools::getValue('step') <= 0) // Sometimes cartsave is called in checkout
                {
                    if (array_key_exists($id_product, $MyShop_cart))
                        $ga_products['quantity'] = $MyShop_cart[$id_product]['quantity'] + $cart['qty'];
                }
                $MyShop_cart[$id_product] = $ga_products;
                $this->context->cookie->MyShop_cart = serialize($MyShop_cart);
                
                $this->didCartChange = 1;
            }
        }

 

I know that regarding what I am looking for a lot is handled in blockcart/ajax-cart.js and controllers/front/CartController.php but I am not precisely sure where or what to look for and this way I was hoping someone will lead me to a proper direction or save me some time on searching.

 

I also assume that regarding the javascript potion I have to modify the template itself, this is a piece of code from my module's header.tpl:

 

var MyShop_blockcart_log = function(json) {
    var products = json.products;
    var product;
    var ps = [];
    var p;

    for(var i=0; i<products.length; i++) {
        product = products[i];
        p = {};

        p.id = product.id;
        p.link = product.link;
        p.imageUrl = product.id_image.replace("medium_default", "large_default");
        p.quantity = product.quantity;
        p.name = product.name;
        p.price = "{$currency->sign} " + (parseInt(product.price_float) / product.quantity).toFixed(2);


        ps.push(p);
    }

    MyShop("log", "product", "cart", ps);
}

That code does not cause any errors, it just doesn't get triggered after adding products to the cart and it should.

Do I have to bind it to the add to cart button via my header.tpl file for example? And if yes then how could I do it?

 

Thanks in advance, 

Cheers.

Edited by IgorSp (see edit history)
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...