2013-01-24 52 views
0

我想在將新產品添加到購物車後使購物車訂單清空。其實只有產品每次都可以在購物車中購買。 坦Prestashop:如何清空購物車?

+0

$ products = $ this-> getProducts(); \t \t的foreach($產品爲$產品) \t \t { \t \t $這 - > deleteProduct($產品 - > ID); \t \t} – sirramin

+0

我在上面的代碼中使用方法添加在cart.php類 – sirramin

回答

4

2的方法來添加自定義邏輯:

  • 創建自己的模塊,並把它掛在「actionCartSave」
  • 覆蓋「添加」,在「購物車」類「更新」的方法( /override/classes/Cartp.php)

編輯:第二種方式os錯誤,因爲無限更新循環。

這裏是一個模塊,這樣做:

class OneProductCart extends Module { 
    public function __construct() { 
     $this->name = 'oneproductcart'; 
     $this->tab = 'front_office_features'; 
     $this->version = '1.0'; 
     $this->author = 'SJousse'; 
     $this->need_instance = 0; 
     parent::__construct(); 
     $this->displayName = $this->l('One Product Cart'); 
     $this->description = $this->l('Keep only last product in cart.'); 
    } 
    public function install() { 
     return (parent::install() && $this->registerHook('actionCartSave')); 
    } 
    public function hookActionCartSave($params) { 
     $cart = $params['cart']; 
     $last = $cart->getLastProduct(); 
     $prods = $cart->getProducts(); 

     foreach ($prods as $prod) 
      if ($prod['id_product'] != $last['id_product']) 
       $cart->deleteProduct($prod['id_product']); 
    } 
} 
+0

我想編輯類:cart.php 我必須在方法類中使用哪個函數? 我用fucntion deleteproduct但不wotk。 請求擴展更多 tanx – sirramin

+0

嘗試更新購物車類而不是添加方法。如果你想刪除現有的產品,這是一個更新。 – SJousse

+0

我在更新方法中使用此代碼: $ products = $ this-> getProducts(); foreach($ product as $ product) {$ this-> deleteProduct($ product-> id); } 添加到購物車後,我在頁面上得到錯誤503。 – sirramin

1

對於正在使用的Prestashop v 1.4.9並創建了一個模塊:

呼叫global $smarty, $cart;

然後運行功能$cart->delete();

function hookHome($params) 
    { 

    global $smarty, $cart; 
    /** some code here **/ 

    $cart->delete(); 

    } 
相關問題