0

This is my onpage.phtml page to get product id. I need to remove all other products in cart except the mention product id below if this product is on cart.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 

// retrieve quote items collection
$itemsCollection = $cart->getQuote()->getItemsCollection();

// get array of all items what can be display directly
$itemsVisible = $cart->getQuote()->getAllVisibleItems();

// retrieve quote items array
$items = $cart->getQuote()->getAllItems();

foreach($items as $item) {// product id 
    $it[] = $item->getProductId();
}
if (in_array(483, $it)){
  //What logic to make here?
}
1

Change your foreach function to this:

$quoteId = $cart->getQuote()->getId();
$quoteModel = $objectManager->create('Magento\Quote\Model\Quote');

foreach($items as $item) {// product id 
    $itemId = $item->getItemId();
    if($itemId == "483") {
        $quoteItem = $quoteModel->load($quoteId);
        $quoteItem->delete();
    }
}
  • Thanks for your response. But it is not working. – Ramesh KR Jan 23 '18 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.