Empty Prestashop cart if the last item in a particular basket category

I have some products in the category (ID 10 for exapmle), this product usually cannot be seen in the navigation, because its additional products and cannot be ordered without ordering a product outside of category 10. These products can only be added to the summary page with the basket purchases.

I add a normal product to the basket, and after the summary page I can add an additional product from category 10 to the basket through Fancybox. It works.

But if I remove all ordinary products from the basket, I also need to automatically remove all goods from category 10 from the basket, because this product cannot be ordered without ordering ordinary products.

I think this is something in ajax-cart.js, but I don’t know exactly how to specify chat categories.

+4
source share
1 answer

There is a hook actionAfterDeleteProductInCartthat starts after removing the product from the basket, where you can perform your checks. Therefore, create a module with this code.

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

Basically, after each removal of a product from the basket, we check the availability of products in the basket, scroll through each product and check their default category identifier. If there are only products with a category ID of 10, simply delete the entire cart.

+3
source

Source: https://habr.com/ru/post/1651142/


All Articles