Jump to content

Hook On Adding A Product To A Cart


yfort

Recommended Posts

Hi

 

I'm a beginner in prestashop development, and i have to develop a module that will add products to cart (with quantity = 0) when a user adds a product to cart.

 

My first question is very basic : how to detect when user adds a product ? The 'cart' hook is nice, but also trigegred on cart creation, product update, product delete, etc. and i can't figure a way to guess which user action triggered the hook.

 

Then, will i have problems with having products with a quantity of 0 ? If yes, is there a workaround ?

 

Eventually, will there be a way to avoid recursivity (if i can add 0 products to cart, i'll use it, but if i have to add 1, i can't figure how i'll do)

 

Thanks in advance

Link to comment
Share on other sites

Really nobody ?

 

Well, for question 2, the only answer i found is "it's not possible", and for question 1 and 3, my solution for prestashop 1.4 : (getConsumables($product) is a filter wrapper on $product->getAccessories() )

 

public function hookCart($params) {
 $cookie = $params['cookie'] ;
 $cart = $params['cart'] ;
 // Semaphore to avoid recursivity
 if ( $cookie->__get('xlcn_consumable_adding') == 'true' )
  return false ;
 $cookie->__set('xlcn_consumable_adding', 'true') ;
 // Get current products
 $products = $cart->getProducts() ;
 $products_id = array() ;
 foreach ( $products as $product )
  array_push($products_id, $product['id_product']) ;
 // Compare with previous ones
 $prev_cookie = $cookie->__get('xlcn_consumable_cart') ;
 $cookie->__set('xlcn_consumable_cart', implode(',', $products_id)) ;
 $added = array_diff($products_id, explode(',', $prev_cookie)) ;
 // Act on each added
 foreach ( $added as $product_id ) {
  $product = new Product($product_id) ;
  foreach ( getConsumables($product, $id_lang) as $accessory )
   $cart->updateQty(1, $accessory['id_product']) ;
 }
 $cookie->__set('xlcn_consumable_adding', false) ;
 return true ;
}

 

Requires of course $this->registerHook('cart') in install, and adds each accessories of a product when any product is added to cart, non recursively

Link to comment
Share on other sites

  • 5 years later...

Why do you want to adding a product to cart with quantity 0? Having a product with quantity 0 in your cart is the same as NOT having that product in the cart. Or, in reverse logic, you'd already have every single product in your cart, but with the quantity 0. lol

 

But besides that, you probably want to use the hook 

actionBeforeCartUpdateQty

or

actionCartSave

 

There is more discussion about appropriate hooks here: 

https://stackoverflow.com/questions/43316822/ps-1-6-module-trigger-event-on-ajax-add-to-cart

Edited by theillo (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...