如果购物车特定类别中的最后一个产品,请清除Prestashop购物车

时间:2016-08-13 07:51:40

标签: php jquery ajax prestashop

我有一些类别的产品(ID 10适用于exapmle),此产品通常无法在导航中看到,因为其附加产品无法订购而无需订购10类以外的产品。此产品只能添加到购物车摘要页。

我将一个普通产品添加到购物车中,在摘要页面之后,我可以通过Fancybox将另一种产品从类别10添加到购物车。这很有效。

但如果我从购物车中删除所有普通产品,我还需要自动删除购物车中10类的所有产品,因为如果没有订购普通产品,该产品无法订购。

我认为它在ajax-cart.js中的内容,但我不知道指定类别监视的方式。

1 个答案:

答案 0 :(得分:2)

有一个钩子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的产品,则只需删除整个购物车。

相关问题