2016-08-13 112 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

相關問題