Prestashop : How to Empty cart?

2019-09-09 16:01发布

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

2条回答
forever°为你锁心
2楼-- · 2019-09-09 16:37

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']);
    }
}
查看更多
疯言疯语
3楼-- · 2019-09-09 16:39

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();

    }
查看更多
登录 后发表回答