2014-02-06 38 views
1

我一直在用magento cart pro擴展。對於阿賈克斯添加到購物車的功能,我跟着這下面的鏈接Excellance Magento BlogAjax從magento中刪除商品

它的工作Fine.But我已經搜索了很多關於阿賈克斯從購物車刪除項目,但谷歌返回我總是與第三方擴展。我找不到從購物車中刪除產品的教程。所以我認爲,從免費的第三方擴展中選擇一種可以添加到購物車並從購物車功能中刪除的推薦。我發現了以下擴展功能,這兩種功能都在功能上面,同時也在工作。 Ajax cart pro

我檢查了這個擴展編碼部分。但我真的很困惑,我找不到哪些代碼在執行刪除功能。他們只覆蓋控制器文件中的deleteAction,我想。我甚至無法理解我在做什麼。我只需要添加ajax刪除功能的指導。

如果有人有想法或找到任何這方面的教程,請與我分享您的想法朋友。

回答

4

跳水到Ajaxcart延伸,在控制器的動作,我們可以找到(是的,你是對的!),下面的行實際上是刪除一個項目:

$ this - > _ getCart() - > removeItem($ id) - > save();

public function deleteAction() { 
    $id = (int) $this->getRequest()->getParam('id'); 
    if ($id) { 
     try { 
      $this->_getCart()->removeItem($id) 
        ->save(); 
     } catch (Exception $e) { 
      $_response = Mage::getModel('ajaxcart/response'); 
      $_response->setError(true); 
      $_response->setMessage($this->__('Cannot remove the item.')); 
      $_response->send(); 

      Mage::logException($e); 
     } 
    } 

    $_response = Mage::getModel('ajaxcart/response'); 

    $_response->setMessage($this->__('Item was removed.')); 

    //append updated blocks 
    $this->getLayout()->getUpdate()->addHandle('ajaxcart'); 
    $this->loadLayout(); 

    $_response->addUpdatedBlocks($_response); 

    $_response->send(); 
} 

所以回答你的問題「它的代碼做了刪除」,這絕對是其中;)

而要了解整個過程中,你應該注意以下幾點:

  • ajaxcart.js - 他們重寫Magento的setLocation函數,然後做ajax調用(在ajaxCartSubmit方法中):

    var oldSetLocation = setLocation; 
    var setLocation = (function() { 
        return function(url){ 
         if(url.search('checkout/cart/add') != -1) { 
          //its simple/group/downloadable product 
          ajaxcart.ajaxCartSubmit(url); 
    
  • 渲染按鈕使用網址幫手,就像這樣:

    <button type="button" title="Add to Cart" class="button btn-cart" 
        onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product, $additional = array('qty' => 1));?>')"> 
    + 
    </button> 
    
  • updateBlocks。有一個觀察者和響應模塊抓取塊更新/替換前端,呈現其內容,並把EM到JSON響應塊的列表。從核心佈局和從附加ajaxcart手柄(佈局/ ajaxcart.xml)採取

    $updated_blocks = unserialize(Mage::getStoreConfig('ajaxcart/general/update_blocks')); 
    // ... for each do a $layout->getBlock('<blockName>')->toHtml() and add to json responce 
    
  • A嵌段定義。 (cart_sidebar,top.links &等)

+0

你已經寫了產品添加到購物車 – DRAJI

+0

是的,它會類似於「添加」。還有一個幫助刪除的方法(或者你可以自己寫): $ this-> helper('checkout/cart') - > getRemoveUrl($ item) – Chadiso

+0

所有的魔法都發生在setLocation()中,所以你需要將刪除網址傳遞給它。祝你好運!) – Chadiso

0

你可以參考下面的代碼

<?php 
$id = '100'; // replace product id with your id 

$cartHelper = Mage::helper('checkout/cart'); 
$items = $cartHelper->getCart()->getItems(); 
foreach($items as $item): 
if($item->getProduct()->getId() == $id): 
$itemId = $item->getItemId(); 
$cartHelper->getCart()->removeItem($itemId)->save(); 
break; 
endif; 
endforeach; 
?> 
+0

謝謝你的回覆.yeah!我從核心文件中獲得了刪除操作代碼。但我不知道,如何通過ajax調用這個控制器函數! – DRAJI