Thursday, 22 September 2016

How to get cart item remove or empty (truncate) in magento 2

Company name :- Sbr
Module name :- Checkout
File path :- /app/code/Sbr/Checkout/etc
============= File name events.xml =====================

<?xml version="1.0"?>
<!--
/**
 * @author     SB , SBR
 * @package    Sbr_Checkout
 * @copyright  Copyright (c) 2015 Sbr Limited (http://www.Sbr.co.nz)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch_checkout_cart_add">
        <observer name="beforecart"
                  instance="Sbr\Checkout\Observer\BeforeCart"/>
    </event>
</config>

=================================================

============= File name module.xml =====================
<?xml version="1.0"?>
<!--
/**
 * @author     SB, Sbr
 * @package  
 * @copyright  Copyright (c) 2015 Sbr
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Sbr_Checkout" setup_version="2.0.4">      
    </module>
</config>


======================================================
============= File name BeforeCart.php =====================
File path :- /app/code/Sbr/Checkout/Observer

<?php

namespace Sbr\Checkout\Observer;
/*
ini_set("display_errors", "On");
error_reporting(E_ALL);
*/
use Magento\Framework\Event\ObserverInterface;

class BeforeCart implements ObserverInterface {
   
    protected $cart;
   
    public function __construct(
        \Magento\Checkout\Model\Cart $cart
    ) {
         $this->cart = $cart;
     
    }
   
    public function execute(\Magento\Framework\Event\Observer $observer) {

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager   =   $_objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currentStore   =   $storeManager->getStore();
$currentStore_id = $currentStore->getId();
if($currentStore_id==1):
$count=count($this->cart->getItems());
if($count>=1){
foreach($this->cart->getItems() as $_item){
$sku=trim($_item['sku']);
if(($sku=='basic1')||($sku=='starter')||($sku=='professional')||($sku=='premium')){
$this->cart->truncate();
}
}
}

endif;
    }

}

=================================================================




==============File name registration.php ========================
File path:- app/code/Sbr/Checkout
<?php
 
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Sbr_Checkout',
    __DIR__
);

====================================

2 comments:

  1. Great Information sharing .. I am very happy to read this article .. thanks for giving us go through info.Fantastic nice. I appreciate this post.
    newizze.com

    ReplyDelete