Jump to content

Programmatically adding a product to cart by reference


Recommended Posts

Hi guys/girls


I'm new to Prestashop development and this forum, and just wanted to say Hi and find out if someone has already done this (searched the forum but didn't find anything).


I am building a module for Prestashop that advises the user on which product to choose. Basically, after a question/answer sequence -  I get back a line item (or multiples) containing the product reference, and the quantity of this product that one would need in order to accomplish a certain goal. This runs off external data but we are going to ensure that the products that are recommended are always available in the shop.


The question/answer sequence runs fine in the front office and I have the output inside a javascript object.


I now want to add the matching product from the prestashop database to the cart, by passing the cart the reference number of the product, the shop id and the quantity.


I can't use the product ID in the prestashop database since it isn't linked to the external database except through the reference.


Is there perhaps a function to automatically do it? If not any ideas where I should start? 


Hope I made myself clear


Edited by ruan567 (see edit history)
Link to comment
Share on other sites

  • 2 years later...
  • 3 years later...

I recomended , you use WebService and API for prestashop, you can try


$id_cart = (int)$this->context->cookie->id_cart;
$id_customer = (int)(isset($this->context->customer))? $this->context->customer->id : 0;
$cart=new Cart($id_cart);
$addressDeliveryID = $cart->id_address_delivery;
$shopID = (int)Context::getContext()->shop->id;
$rq = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS("select * from ato_product where reference ='".$reference."'");
if (isset ($rq[0]['id_product'])){
$productAttributeID = Product::getDefaultAttribute($rq[0]['id_product']);
$success &= $cart->updateQty(
new Shop((int)$cart->id_shop),
If have any problem you cant ask me in http://digitaldot.es/soporte-prestashop
Edited by digitalDot (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...