actualweb Posted June 17, 2013 Share Posted June 17, 2013 I'm developing a new functionality in prestashop which is something like a "fast purchase". I've created a new page with a form where I introduce product codes and quantities and then I press a button which saves them in the cart. The problem is that after a succesful login, if I visit directly this new page, it seems that the cart is not initialised, so when I try to store the new products, as the new cart does not exist in ps_cart, I cannot save them properly. Otherwise, after login, if I add a product using prestashop typical way (from product page for example), then the cart is succesfully created, so more products can be added using my way because the cart's ID is being propagated. What I've done so far is something like this: class QuickbuyControllerCore extends FrontController{ public $php_self = 'quickbuy.php'; public function init() { parent::init(); } public function initContent() { parent::initContent(); $this->setTemplate(_PS_THEME_DIR_.'quickbuy.tpl'); } public function process(){ global $cart, $cookie; // do some stuff to get product codes if(!empty($products)){ foreach ($products as $id_product => $qty){ $exists = Db::getInstance()->getValue('SELECT id_product FROM `'._DB_PREFIX_.'cart_product` WHERE id_product = ' . $id_product . ' AND id_cart = '.(int)$cart->id); if($exists === false){ $query = 'INSERT INTO `'._DB_PREFIX_.'cart_product`(`id_cart`, `id_product`, `id_product_attribute`, `quantity`, `date_add`) VALUES '; $query .= '('.(int)$cart->id.', '.(int)$id_product.', 0 , '.(int)$unidades.', NOW()),'; $res = Db::getInstance()->Execute(rtrim($query, ',')); } else{ $upd = 'UPDATE `'._DB_PREFIX_.'cart_product` SET quantity = quantity + ' . (int)$unidades . ' WHERE id_product = '.(int) $id_product.' AND id_cart = '.(int)$cart->id; Db::getInstance()->Execute($upd); } } } } // Other methods like SetMedia, DisplayContent,... } Prestashop may call some method to create the cart and I suppose I am leaving it out, but I cannot find out which method I have to call to create the cart. I've also tried calling $cart->save() and $cookie->write(), and this created a new cart, but when I refresh the page, the cart's ID has been lost. Any idea on why the cart ID is not propagation through pages? EDIT: I forgot say that my prestashop version is 1.4.5.1 Link to comment Share on other sites More sharing options...
actualweb Posted June 17, 2013 Author Share Posted June 17, 2013 Finally I got it! I had to use this code: if (!isset($cart->id) OR !$cart->id){ $cart->add(); if ($cart->id) $cookie->id_cart = (int)($cart->id); } It seems I had tested out all combinations of it except that one. Link to comment Share on other sites More sharing options...
PascalVG Posted June 17, 2013 Share Posted June 17, 2013 Thanks for sharing the solution. I mark this post as solved. 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