1.the ajax function in cart page phtml:
function change($this){
//update cart item qty
var item_id = $this.attr('item_id');
var qty = $this.val();
$("#ajax-loading-div").show();
$.ajax({
url: "<?php echo $this->getUrl('mycheckout/cart/updateProductQty');?>",
type: "POST",
dataType:"json",
data:{item_id:item_id,qty:qty},
success: function(data) {
//update the cart page
$('.big-cart').html(data['big_cart']);
//update the cart header
$('.cart-header').html(data['cart_header']);
//update the cart header count;
$('#top_link_cart').html(data['cart_header_cnt_text']);
//hide the loding box
$("#ajax-loading-div").hide();
}
});
}
2.the php function for ajax:
public function updateProductQtyAction()
{
$item_id = Mage::app()->getRequest()->getParam('item_id');
$qty = Mage::app()->getRequest()->getParam('qty');
Mage::getSingleton('core/session', array('name'=>'frontend')); // GET THE SESSION
$simbol= Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol(); // GET THE CURRENCY SIMBOL
$store=Mage::app()->getStore()->getCode(); // GET THE STORE CODE
$cart = Mage::getSingleton('checkout/cart'); //->getItemsCount();
$items = $cart->getItems();
foreach ($items as $item) { // LOOP
if($item->getId()==$item_id){ // IS THIS THE ITEM WE ARE CHANGING? IF IT IS:
$item->setQty($qty); // UPDATE ONLY THE QTY, NOTHING ELSE!
$cart->save(); // SAVE
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
break;
}
}
$data = array();
$block = $this->getLayout()->createBlock('checkout/cart_sidebar');
$block->setTemplate('checkout/cart/cartheader-ajax.phtml')
->addItemRender('simple','checkout/cart_item_renderer','checkout/cart/sidebar/default.phtml')
->addItemRender('grouped','checkout/cart_item_renderer_grouped','checkout/cart/sidebar/default.phtml')
->addItemRender('configurable','checkout/cart_item_renderer_configurable','checkout/cart/sidebar/default.phtml');
$html = $block->toHtml();
$data['cart_header'] = $html;
$block = $this->getLayout()->createBlock('checkout/cart');
$block->setTemplate('checkout/cart-ajax.phtml')
->addItemRender('simple','checkout/cart_item_renderer','checkout/cart/item/default.phtml')
->addItemRender('grouped','checkout/cart_item_renderer_grouped','checkout/cart/item/default.phtml')
->addItemRender('configurable','checkout/cart_item_renderer_configurable','checkout/cart/item/default.phtml');
$html = $block->toHtml();
$data['big_cart'] = $html;
$cart = Mage::getModel('checkout/cart')->getQuote()->getData();
$qty = $cart['items_qty'];
$data['cart_header_cnt_text'] = $this->__('Shopping Cart') . ' (' . $qty . ')';
echo json_encode($data);
}
3. delete retun url fix function :
/**
* Delete shoping cart item action
*/
public function deleteAction()
{
$id = (int) $this->getRequest()->getParam('id');
if ($id) {
try {
$this->_getCart()->removeItem($id)
->save();
} catch (Exception $e) {
$this->_getSession()->addError($this->__('Cannot remove the item.'));
var_dump($e->getMessage());
exit();
}
}
$last_list_url = $_SERVER['HTTP_REFERER'];
header('Location: ' . $last_list_url);
$_SESSION['last_list_url'] = $last_list_url;
exit();
//$this->_redirectReferer(Mage::getUrl('*/*'));
}

本文介绍了一种使用Ajax更新购物车中商品数量的方法,并提供了一个PHP实现示例。此外,还展示了如何通过PHP实现从购物车中删除商品的功能。

被折叠的 条评论
为什么被折叠?



