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.
source
share