Magento2 Remove cart Item using Jquery Ajax

With out page refresh you can delete one by one Cart item using Custom programatically using Jquery and Ajax and Custom magento2 Controller for Delete Cart itrm.

1.Html

<div class=”paddt-3″>
<a href=”javascript:void(0)” onclick=”RemoveCartitem(<?php echo $_item->getId()?>)” item_id=”<?php echo $_item->getId()?>” title=”Remove item” class=””><b class=”close1 close_icn1″ data=”107″>X</b>
</a>
</div>

2. Controller


      namespace Medley\Search\Controller\Add;

    use Magento\Framework\App\Action\Context;
    use Magento\Framework\View\Result\PageFactory;

class Removeselleritem extends \Magento\Framework\App\Action\Action
{
	
protected $cart;
protected $product;
 
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Data\Form\FormKey $formKey,
\Magento\Checkout\Model\Cart $cart,
\Magento\Catalog\Model\Product $product,
array $data = []) {
	$this->formKey = $formKey;
	$this->cart = $cart;
	$this->product = $product;  	
	parent::__construct($context);
}
 
public function execute()
 {
  $post = $this->getRequest()->getPostValue();
  $productId =$post['productid'];     // Your Product Id
  $this->cart->removeItem($productId);
  $this->cart->save();
	
	$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cartObj = $objectManager->get('\Magento\Checkout\Model\Cart');
echo $totalItems = $cartObj->getQuote()->getItemsCount(); //Current cart Item counts
   
    }
}

3.Jquery

function RemoveCartitem(productid)
{
	require([
	'jquery',
	'jquery/jquery.cookie',
	'jquery/ui',
     ], function($,Component) {
		 
		 
		                           $.ajax({
										url:"search/add/removeselleritem",
										type:'POST',
										showLoader: true,
										data: {productid:productid},
										success:function(response){
											
											    $.ajax({
													url:'orderrequest/prescription/sellercartdetails',
													type:'GET',
													success:function(responsecart){
														console.log(responsecart);
														$("#cart_item").show();
														$("#cart_item").html(responsecart);
														$(".skip-cart_storefront .count").html(response);
														
														}
												});
											}
			                         });
		 
	});
	
	
}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons
Social Media Auto Publish Powered By : XYZScripts.com