2016-08-13 116 views
4

我在某个类别中有一些产品(ID为10的exapmle),该产品通常无法在导航中看到,因为它的附加产品不能在没有订购产品的情况下订购此类产品只能添加到购物车摘要页面中。清除Prestashop购物车如果购物车特定类别中的最后一个产品

我在购物车中添加普通产品,在总结页面之后,我可以添加一个Fancybox从类别10到购物车的其他产品。这工作。

但是,如果我从购物车中删除所有正常产品,我还需要自动从购物车中删除类别10中的所有产品,因为如果没有订购正常产品,此产品无法订购。

我认为它在ajax-cart.js中的东西,但我不知道确切的方式来指定类别手表。

+0

任何人的想法?也许我修改了CartController.php? – redpillcoders

+0

你在使用模块吗? – TheDrot

+0

哪个版本使用prestashop? –

回答

3

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

+0

很大,对代码的感谢。我创建了模块,但没有任何反应。也许是一个问题,因为我有一个阿贾克斯车,需要刷新电话? – redpillcoders

+0

如果您在删除正常产品后刷新订单页面,是否显示空购物车? – TheDrot

+0

好吧,我找到了解决办法:我的PS版本没有这个钩子“actionAfterDeleteProductInCart”,需要在CartController.php更多的输入行中的129胡克:: EXEC(“actionAfterDeleteProductInCart”,阵列( + \t \t \t \t“id_cart '=>(int)的这 - $>上下文> cart-> ID, + \t \t \t \t 'id_product'=>(INT)$这 - > id_product, + \t \t \t \t 'id_product_attribute'=>( INT)$这 - > id_product_attribute, + \t \t \t \t 'customization_id'=>(int)的这 - $> CUST omization_id, + \t \t \t \t 'id_address_delivery'=>(int)的这 - $> id_address_delivery + \t \t \t)); – redpillcoders

相关问题