问题
I want to make Cart order list empty after a new product has been added to the cart. In fact only on product every time can be in cart. Tanx
回答1:
2 ways to add your custom logic :
- create your own module and hook it on "actionCartSave"
override "add" and "update" methods in "Cart" class (/override/classes/Cartp.php)
Edit : The 2nd way os wrong because infinite update loop.
Here is a module that do it :
class OneProductCart extends Module {
public function __construct() {
$this->name = 'oneproductcart';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'SJousse';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('One Product Cart');
$this->description = $this->l('Keep only last product in cart.');
}
public function install() {
return (parent::install() && $this->registerHook('actionCartSave'));
}
public function hookActionCartSave($params) {
$cart = $params['cart'];
$last = $cart->getLastProduct();
$prods = $cart->getProducts();
foreach ($prods as $prod)
if ($prod['id_product'] != $last['id_product'])
$cart->deleteProduct($prod['id_product']);
}
}
回答2:
For people using Prestashop v 1.4.9 and have created a module:
call global $smarty, $cart;
then run the function $cart->delete();
function hookHome($params)
{
global $smarty, $cart;
/** some code here **/
$cart->delete();
}
来源:https://stackoverflow.com/questions/14495653/prestashop-how-to-empty-cart