Update cart item qty ajax to update cart header and cart page self

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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('*/*'));
    }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值