Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have some products in a category (ID 10 for exapmle), this products normally can't see in the navigation because its additional products and cannot ordering without ordering a product outside of category 10. This products can only add in the shopping cart summary page.

I add a normal product to the cart, and after the summary page I can add over a Fancybox a additional product from category 10 to the cart. This works.

But if I delete all normal products from the cart I also need to delete automatically all products from category 10 from the cart, because this product(s) cannot ordering without ordering normal products.

I think its something in ajax-cart.js but I don't know exactly the way to specify the category watch.

like image 438
redpillcoders Avatar asked Dec 05 '25 18:12

redpillcoders


1 Answers

There is a hook actionAfterDeleteProductInCart which runs after removing product from cart where you can do your checks. So 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 every product deletion from cart we check if there are still products in cart, loop through every product and check their default category ID. If only products with category ID 10 exist then just delete the whole cart.

like image 181
TheDrot Avatar answered Dec 08 '25 09:12

TheDrot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!