How to Remove cart products if a specific product is added in cartIs there any known way to create a cart item that doesn't exist as a product?Modify tax rate on cart quote items and recalculateSetting additional options for configurable product in Quote Item [Solved]Get product collection filter by from and to date in magento2.Attribute added to Mini Cart Magento 2remove items from cart in controller - magento2Can I change the taxes on a quote item in magento2 programmatically?Magento 2, Add Product in Cart in loopMagento 2.2.6: Programatically updating product custom optionsMagento 2 get quantity of product in cart without loop

How can I fix this gap between bookcases I made?

Is this food a bread or a loaf?

Need help identifying/translating a plaque in Tangier, Morocco

Lied on resume at previous job

Calculate Levenshtein distance between two strings in Python

What does 'script /dev/null' do?

What is the command to reset a PC without deleting any files

How is it possible for user's password to be changed after storage was encrypted? (on OS X, Android)

Is it wise to hold on to stock that has plummeted and then stabilized?

How would photo IDs work for shapeshifters?

Does bootstrapped regression allow for inference?

Filling an area between two curves

Are cabin dividers used to "hide" the flex of the airplane?

What is GPS' 19 year rollover and does it present a cybersecurity issue?

Is "plugging out" electronic devices an American expression?

aging parents with no investments

What to wear for invited talk in Canada

Extreme, but not acceptable situation and I can't start the work tomorrow morning

New order #4: World

Does it makes sense to buy a new cycle to learn riding?

Is there a name of the flying bionic bird?

Information to fellow intern about hiring?

Does a dangling wire really electrocute me if I'm standing in water?

How to manage monthly salary



How to Remove cart products if a specific product is added in cart


Is there any known way to create a cart item that doesn't exist as a product?Modify tax rate on cart quote items and recalculateSetting additional options for configurable product in Quote Item [Solved]Get product collection filter by from and to date in magento2.Attribute added to Mini Cart Magento 2remove items from cart in controller - magento2Can I change the taxes on a quote item in magento2 programmatically?Magento 2, Add Product in Cart in loopMagento 2.2.6: Programatically updating product custom optionsMagento 2 get quantity of product in cart without loop






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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 = MagentoFrameworkAppObjectManager::getInstance();
$cart = $objectManager->get('MagentoCheckoutModelCart');

// 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?










share|improve this question






























    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 = MagentoFrameworkAppObjectManager::getInstance();
    $cart = $objectManager->get('MagentoCheckoutModelCart');

    // 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?










    share|improve this question


























      0












      0








      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 = MagentoFrameworkAppObjectManager::getInstance();
      $cart = $objectManager->get('MagentoCheckoutModelCart');

      // 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?










      share|improve this question
















      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 = MagentoFrameworkAppObjectManager::getInstance();
      $cart = $objectManager->get('MagentoCheckoutModelCart');

      // 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?







      magento2 product cart shopping-cart-price-rules downloadable






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 5 at 10:31









      Himanshu

      1,054722




      1,054722










      asked Jan 23 '18 at 11:44









      Ramesh KRRamesh KR

      391112




      391112




















          1 Answer
          1






          active

          oldest

          votes


















          1














          Change your foreach function to this:



          $quoteId = $cart->getQuote()->getId();
          $quoteModel = $objectManager->create('MagentoQuoteModelQuote');

          foreach($items as $item) // product id
          $itemId = $item->getItemId();
          if($itemId == "483")
          $quoteItem = $quoteModel->load($quoteId);
          $quoteItem->delete();







          share|improve this answer

























          • Thanks for your response. But it is not working.

            – Ramesh KR
            Jan 23 '18 at 12:19











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f210631%2fhow-to-remove-cart-products-if-a-specific-product-is-added-in-cart%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Change your foreach function to this:



          $quoteId = $cart->getQuote()->getId();
          $quoteModel = $objectManager->create('MagentoQuoteModelQuote');

          foreach($items as $item) // product id
          $itemId = $item->getItemId();
          if($itemId == "483")
          $quoteItem = $quoteModel->load($quoteId);
          $quoteItem->delete();







          share|improve this answer

























          • Thanks for your response. But it is not working.

            – Ramesh KR
            Jan 23 '18 at 12:19















          1














          Change your foreach function to this:



          $quoteId = $cart->getQuote()->getId();
          $quoteModel = $objectManager->create('MagentoQuoteModelQuote');

          foreach($items as $item) // product id
          $itemId = $item->getItemId();
          if($itemId == "483")
          $quoteItem = $quoteModel->load($quoteId);
          $quoteItem->delete();







          share|improve this answer

























          • Thanks for your response. But it is not working.

            – Ramesh KR
            Jan 23 '18 at 12:19













          1












          1








          1







          Change your foreach function to this:



          $quoteId = $cart->getQuote()->getId();
          $quoteModel = $objectManager->create('MagentoQuoteModelQuote');

          foreach($items as $item) // product id
          $itemId = $item->getItemId();
          if($itemId == "483")
          $quoteItem = $quoteModel->load($quoteId);
          $quoteItem->delete();







          share|improve this answer















          Change your foreach function to this:



          $quoteId = $cart->getQuote()->getId();
          $quoteModel = $objectManager->create('MagentoQuoteModelQuote');

          foreach($items as $item) // product id
          $itemId = $item->getItemId();
          if($itemId == "483")
          $quoteItem = $quoteModel->load($quoteId);
          $quoteItem->delete();








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 23 '18 at 12:10

























          answered Jan 23 '18 at 12:02









          Prince PatelPrince Patel

          14.5k65481




          14.5k65481












          • Thanks for your response. But it is not working.

            – Ramesh KR
            Jan 23 '18 at 12:19

















          • Thanks for your response. But it is not working.

            – Ramesh KR
            Jan 23 '18 at 12:19
















          Thanks for your response. But it is not working.

          – Ramesh KR
          Jan 23 '18 at 12:19





          Thanks for your response. But it is not working.

          – Ramesh KR
          Jan 23 '18 at 12:19

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f210631%2fhow-to-remove-cart-products-if-a-specific-product-is-added-in-cart%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Sum ergo cogito? 1 nng

          419 nièngy_Soadمي 19bal1.5o_g

          Queiggey Chernihivv 9NnOo i Zw X QqKk LpB