Programatically clear the cart of a logged-in user The 2019 Stack Overflow Developer Survey Results Are InWhy is shopping cart not emptied after placing an order?Cart empties itself very quicklyCart ID in MagentoHow to create Mouseover Shopping Cart Icon in right header?Logout user while preserving cart contentsGrand total is zero with items in the cartUnable to get cart items for a user on login eventHow to redirect cart page from checkout onepage when product out of stock ?Magento 2 - how to set a message for the customer cart page after removing cart items via cronjobImplementing a persistent cart for guestsMagento 2 Clear cart is not working

What information about me do stores get via my credit card?

How to charge AirPods to keep battery healthy?

Does adding complexity mean a more secure cipher?

What is this business jet?

Relationship between Gromov-Witten and Taubes' Gromov invariant

Is it okay to consider publishing in my first year of PhD?

I am an eight letter word. What am I?

How can I add encounters in the Lost Mine of Phandelver campaign without giving PCs too much XP?

How to support a colleague who finds meetings extremely tiring?

How do PCB vias affect signal quality?

How to type a long/em dash `—`

What is the light source in the black hole images?

Getting crown tickets for Statue of Liberty

Is it possible for absolutely everyone to attain enlightenment?

What is the meaning of Triage in Cybersec world?

What's the name of these plastic connectors

Why was M87 targeted for the Event Horizon Telescope instead of Sagittarius A*?

What do I do when my TA workload is more than expected?

APIPA and LAN Broadcast Domain

What is the motivation for a law requiring 2 parties to consent for recording a conversation

A word that means fill it to the required quantity

Straighten subgroup lattice

$EDITOR environment variable won't set

How come people say “Would of”?



Programatically clear the cart of a logged-in user



The 2019 Stack Overflow Developer Survey Results Are InWhy is shopping cart not emptied after placing an order?Cart empties itself very quicklyCart ID in MagentoHow to create Mouseover Shopping Cart Icon in right header?Logout user while preserving cart contentsGrand total is zero with items in the cartUnable to get cart items for a user on login eventHow to redirect cart page from checkout onepage when product out of stock ?Magento 2 - how to set a message for the customer cart page after removing cart items via cronjobImplementing a persistent cart for guestsMagento 2 Clear cart is not working



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








6















TL;DR
When a customer is logged in, Mage::getSingleton('checkout/session')->clear(); doesn't clear their cart. I need something that will.




I've asked this as part of another question, but i discovered it was actually two problems, other other one now fixed.



I'm programatically creating an order in a controller using the items in the cart. After the order is saved the cart should be cleared. I do this using



Mage::getSingleton('checkout/session')->clear();


which works fine most of the time. The problem comes when the customer is logged-in. If they are logged in the cart doesn't clear. I've found other solutions (like load the cart items and removed them one at a time in a loop) but they don't meet all my requirements. WHat I need is something that will:



  1. remove all the items (obviously)

  2. stay on the same page (why loading the cart doesn't seem to work. I end up redirected to the cart page)

  3. NOT go to another page and then come back (2 redirects won't work in this case)

Is there something different I need to do when they are logged in? How can I get the cart cleared? Thanks all for the help.










share|improve this question






















  • If you create an order programmatically following the normal Magento process, the cart should I think end up empty automatically. What steps are you taking to create the order?

    – Jonathan Hussey
    Oct 22 '14 at 10:31

















6















TL;DR
When a customer is logged in, Mage::getSingleton('checkout/session')->clear(); doesn't clear their cart. I need something that will.




I've asked this as part of another question, but i discovered it was actually two problems, other other one now fixed.



I'm programatically creating an order in a controller using the items in the cart. After the order is saved the cart should be cleared. I do this using



Mage::getSingleton('checkout/session')->clear();


which works fine most of the time. The problem comes when the customer is logged-in. If they are logged in the cart doesn't clear. I've found other solutions (like load the cart items and removed them one at a time in a loop) but they don't meet all my requirements. WHat I need is something that will:



  1. remove all the items (obviously)

  2. stay on the same page (why loading the cart doesn't seem to work. I end up redirected to the cart page)

  3. NOT go to another page and then come back (2 redirects won't work in this case)

Is there something different I need to do when they are logged in? How can I get the cart cleared? Thanks all for the help.










share|improve this question






















  • If you create an order programmatically following the normal Magento process, the cart should I think end up empty automatically. What steps are you taking to create the order?

    – Jonathan Hussey
    Oct 22 '14 at 10:31













6












6








6


1






TL;DR
When a customer is logged in, Mage::getSingleton('checkout/session')->clear(); doesn't clear their cart. I need something that will.




I've asked this as part of another question, but i discovered it was actually two problems, other other one now fixed.



I'm programatically creating an order in a controller using the items in the cart. After the order is saved the cart should be cleared. I do this using



Mage::getSingleton('checkout/session')->clear();


which works fine most of the time. The problem comes when the customer is logged-in. If they are logged in the cart doesn't clear. I've found other solutions (like load the cart items and removed them one at a time in a loop) but they don't meet all my requirements. WHat I need is something that will:



  1. remove all the items (obviously)

  2. stay on the same page (why loading the cart doesn't seem to work. I end up redirected to the cart page)

  3. NOT go to another page and then come back (2 redirects won't work in this case)

Is there something different I need to do when they are logged in? How can I get the cart cleared? Thanks all for the help.










share|improve this question














TL;DR
When a customer is logged in, Mage::getSingleton('checkout/session')->clear(); doesn't clear their cart. I need something that will.




I've asked this as part of another question, but i discovered it was actually two problems, other other one now fixed.



I'm programatically creating an order in a controller using the items in the cart. After the order is saved the cart should be cleared. I do this using



Mage::getSingleton('checkout/session')->clear();


which works fine most of the time. The problem comes when the customer is logged-in. If they are logged in the cart doesn't clear. I've found other solutions (like load the cart items and removed them one at a time in a loop) but they don't meet all my requirements. WHat I need is something that will:



  1. remove all the items (obviously)

  2. stay on the same page (why loading the cart doesn't seem to work. I end up redirected to the cart page)

  3. NOT go to another page and then come back (2 redirects won't work in this case)

Is there something different I need to do when they are logged in? How can I get the cart cleared? Thanks all for the help.







cart controllers login shopping-cart user






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 22 '14 at 10:02









Matt FlammMatt Flamm

124213




124213












  • If you create an order programmatically following the normal Magento process, the cart should I think end up empty automatically. What steps are you taking to create the order?

    – Jonathan Hussey
    Oct 22 '14 at 10:31

















  • If you create an order programmatically following the normal Magento process, the cart should I think end up empty automatically. What steps are you taking to create the order?

    – Jonathan Hussey
    Oct 22 '14 at 10:31
















If you create an order programmatically following the normal Magento process, the cart should I think end up empty automatically. What steps are you taking to create the order?

– Jonathan Hussey
Oct 22 '14 at 10:31





If you create an order programmatically following the normal Magento process, the cart should I think end up empty automatically. What steps are you taking to create the order?

– Jonathan Hussey
Oct 22 '14 at 10:31










4 Answers
4






active

oldest

votes


















7














Check quote status is_active for the quote (sales_flat_quote) regarding to the created order. If its active (value is 1) set it inactive ($quote->setIsActive(0)->save()) after successfully order creation and than clear checkout session.






share|improve this answer

























  • This seems to be doing it. The process it follows leaves magento before the quote is made inactive, then comes back to create the order after that would have happened. This seems to make it work just fine.

    – Matt Flamm
    Oct 27 '14 at 13:02


















4














This is against standard Magento logic, so you need a custom module that will observe customer_logout event and execute the following code bit:

foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item )
Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();



more info






share|improve this answer

























  • I'm not trying to clear it at log-out, rather to clear it after the order is made while still on the success screen

    – Matt Flamm
    Oct 23 '14 at 13:54


















2














/** @var Mage_Sales_Model_Quote $quote */
$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->removeAllItems()->save();





share|improve this answer























  • Method 'removeAllItems' not found

    – TheKitMurkit
    Dec 20 '17 at 21:49


















0














This should do a job (note that without checking if cart actually has items it will not work good):



$cart = Mage::getSingleton('checkout/cart');
if (count($cart->getItems()))
$cart->truncate();
$cart->save();






share|improve this answer























    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%2f40992%2fprogramatically-clear-the-cart-of-a-logged-in-user%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    7














    Check quote status is_active for the quote (sales_flat_quote) regarding to the created order. If its active (value is 1) set it inactive ($quote->setIsActive(0)->save()) after successfully order creation and than clear checkout session.






    share|improve this answer

























    • This seems to be doing it. The process it follows leaves magento before the quote is made inactive, then comes back to create the order after that would have happened. This seems to make it work just fine.

      – Matt Flamm
      Oct 27 '14 at 13:02















    7














    Check quote status is_active for the quote (sales_flat_quote) regarding to the created order. If its active (value is 1) set it inactive ($quote->setIsActive(0)->save()) after successfully order creation and than clear checkout session.






    share|improve this answer

























    • This seems to be doing it. The process it follows leaves magento before the quote is made inactive, then comes back to create the order after that would have happened. This seems to make it work just fine.

      – Matt Flamm
      Oct 27 '14 at 13:02













    7












    7








    7







    Check quote status is_active for the quote (sales_flat_quote) regarding to the created order. If its active (value is 1) set it inactive ($quote->setIsActive(0)->save()) after successfully order creation and than clear checkout session.






    share|improve this answer















    Check quote status is_active for the quote (sales_flat_quote) regarding to the created order. If its active (value is 1) set it inactive ($quote->setIsActive(0)->save()) after successfully order creation and than clear checkout session.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 22 '17 at 2:09









    Amit Bera

    59.9k1677178




    59.9k1677178










    answered Oct 22 '14 at 10:40









    Stefan BothnerStefan Bothner

    1866




    1866












    • This seems to be doing it. The process it follows leaves magento before the quote is made inactive, then comes back to create the order after that would have happened. This seems to make it work just fine.

      – Matt Flamm
      Oct 27 '14 at 13:02

















    • This seems to be doing it. The process it follows leaves magento before the quote is made inactive, then comes back to create the order after that would have happened. This seems to make it work just fine.

      – Matt Flamm
      Oct 27 '14 at 13:02
















    This seems to be doing it. The process it follows leaves magento before the quote is made inactive, then comes back to create the order after that would have happened. This seems to make it work just fine.

    – Matt Flamm
    Oct 27 '14 at 13:02





    This seems to be doing it. The process it follows leaves magento before the quote is made inactive, then comes back to create the order after that would have happened. This seems to make it work just fine.

    – Matt Flamm
    Oct 27 '14 at 13:02













    4














    This is against standard Magento logic, so you need a custom module that will observe customer_logout event and execute the following code bit:

    foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item )
    Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();



    more info






    share|improve this answer

























    • I'm not trying to clear it at log-out, rather to clear it after the order is made while still on the success screen

      – Matt Flamm
      Oct 23 '14 at 13:54















    4














    This is against standard Magento logic, so you need a custom module that will observe customer_logout event and execute the following code bit:

    foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item )
    Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();



    more info






    share|improve this answer

























    • I'm not trying to clear it at log-out, rather to clear it after the order is made while still on the success screen

      – Matt Flamm
      Oct 23 '14 at 13:54













    4












    4








    4







    This is against standard Magento logic, so you need a custom module that will observe customer_logout event and execute the following code bit:

    foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item )
    Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();



    more info






    share|improve this answer















    This is against standard Magento logic, so you need a custom module that will observe customer_logout event and execute the following code bit:

    foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item )
    Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();



    more info







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 5 '18 at 8:38









    Teja Bhagavan Kollepara

    2,98841949




    2,98841949










    answered Oct 22 '14 at 10:57









    ravi patelravi patel

    576418




    576418












    • I'm not trying to clear it at log-out, rather to clear it after the order is made while still on the success screen

      – Matt Flamm
      Oct 23 '14 at 13:54

















    • I'm not trying to clear it at log-out, rather to clear it after the order is made while still on the success screen

      – Matt Flamm
      Oct 23 '14 at 13:54
















    I'm not trying to clear it at log-out, rather to clear it after the order is made while still on the success screen

    – Matt Flamm
    Oct 23 '14 at 13:54





    I'm not trying to clear it at log-out, rather to clear it after the order is made while still on the success screen

    – Matt Flamm
    Oct 23 '14 at 13:54











    2














    /** @var Mage_Sales_Model_Quote $quote */
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $quote->removeAllItems()->save();





    share|improve this answer























    • Method 'removeAllItems' not found

      – TheKitMurkit
      Dec 20 '17 at 21:49















    2














    /** @var Mage_Sales_Model_Quote $quote */
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $quote->removeAllItems()->save();





    share|improve this answer























    • Method 'removeAllItems' not found

      – TheKitMurkit
      Dec 20 '17 at 21:49













    2












    2








    2







    /** @var Mage_Sales_Model_Quote $quote */
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $quote->removeAllItems()->save();





    share|improve this answer













    /** @var Mage_Sales_Model_Quote $quote */
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $quote->removeAllItems()->save();






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 21 '17 at 23:14









    zlikzlik

    211




    211












    • Method 'removeAllItems' not found

      – TheKitMurkit
      Dec 20 '17 at 21:49

















    • Method 'removeAllItems' not found

      – TheKitMurkit
      Dec 20 '17 at 21:49
















    Method 'removeAllItems' not found

    – TheKitMurkit
    Dec 20 '17 at 21:49





    Method 'removeAllItems' not found

    – TheKitMurkit
    Dec 20 '17 at 21:49











    0














    This should do a job (note that without checking if cart actually has items it will not work good):



    $cart = Mage::getSingleton('checkout/cart');
    if (count($cart->getItems()))
    $cart->truncate();
    $cart->save();






    share|improve this answer



























      0














      This should do a job (note that without checking if cart actually has items it will not work good):



      $cart = Mage::getSingleton('checkout/cart');
      if (count($cart->getItems()))
      $cart->truncate();
      $cart->save();






      share|improve this answer

























        0












        0








        0







        This should do a job (note that without checking if cart actually has items it will not work good):



        $cart = Mage::getSingleton('checkout/cart');
        if (count($cart->getItems()))
        $cart->truncate();
        $cart->save();






        share|improve this answer













        This should do a job (note that without checking if cart actually has items it will not work good):



        $cart = Mage::getSingleton('checkout/cart');
        if (count($cart->getItems()))
        $cart->truncate();
        $cart->save();







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 8 at 9:22









        Anton SuslovAnton Suslov

        112




        112



























            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%2f40992%2fprogramatically-clear-the-cart-of-a-logged-in-user%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

            Bulk add to cart function issuecart vs. mini cart issue … rwd themeRedirect Add to cart button to cart pageAdd to cart issue - Magento 2.1The requested Payment Method is not available When creating an orderM2: reason add-to-cart might not function in production modeAdd to cart issue in some android devicesMagento 2 - custom price can not add to subtotal and grand total after add to cartAdd to cart codeIssue with my cart module on pdp and cart pages, just keeps spinningBulk price and quantity update using rest api

            Magento2 - How to hide price filter only in specific categories?Multiselect price filter attribute in layered navigationhide only some categories from layered navigation in magentoRemove Price Filter on certain categoriescustomize layered price filter?Hide Price for a particular customer groupPrice filter in layered navigation not working correctly with price including tax in magento 2.2.3Magento 2 how to hide attribute at Layered navigation?Magento 2. how to hide price only for specific categoriesMagento 2 How can I hide the price and total from cart and checkout summary?Magento2: Can we add navigation layered filter like price filter for other attribute?