跳水到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 &等)
你已經寫了產品添加到購物車 – DRAJI
是的,它會類似於「添加」。還有一個幫助刪除的方法(或者你可以自己寫): $ this-> helper('checkout/cart') - > getRemoveUrl($ item) – Chadiso
所有的魔法都發生在setLocation()中,所以你需要將刪除網址傳遞給它。祝你好運!) – Chadiso