Prestashop : How to Empty cart?

我是研究僧i 提交于 2019-12-25 06:52:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!