如果购物车中最后一个产品属于特定类别,则清除prestshop购物车


Clear Prestashop cart if the last product in the cart specific category

我在一个类别中有一些产品(例如ID 10),这个产品通常无法在导航中看到,因为它是额外的产品,如果不订购类别10以外的产品就无法订购。此产品只能添加到购物车摘要页面。

我将一个普通产品添加到购物车中,在摘要页面之后,我可以通过一个fanybox从类别10添加一个额外的产品到购物车中。这工作。

但是,如果我从购物车中删除所有普通产品,我也需要自动从购物车中删除类别10中的所有产品,因为该产品在不订购普通产品的情况下无法订购。

我想这是ajax-cart.js中的一些东西,但我不知道具体如何指定类别手表

有一个钩子actionAfterDeleteProductInCart,从购物车中删除产品后运行,您可以在其中进行检查。用下面的代码创建一个模块。

class CartExtraProductsCleaner extends Module {
    public function __construct() {
        $this->name = 'cartextraproductscleaner';
        $this->tab = 'front_office_features';
        $this->version = '1.0';
        $this->author = 'whatever';
        parent::__construct();
        $this->displayName = $this->l('Cart extra products cleaner.');
        $this->description = $this->l('Module deletes additional products from cart when there are no standalone products in cart.');
    }
    public function install() {
        return parent::install() && $this->registerHook('actionAfterDeleteProductInCart');
    }
    public function hookActionAfterDeleteProductInCart($params) {
        if ($this->context->cart->nbProducts()) {
            $only_additional_products = true;
            foreach ($this->context->cart->getProducts() as $product) {
                if ($product['id_category_default'] != 10) {
                    $only_additional_products = false;
                    break;
                }
            }
            if ($only_additional_products) {
                $this->context->cart->delete();
            }
        }
    }
}

基本上每次从购物车中删除产品后,我们都会检查购物车中是否还有产品,遍历每个产品并检查其默认类别ID。如果只存在类别ID为10的产品,则只需删除整个购物车。