Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8920093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:10:48+00:00 2026-06-15T06:10:48+00:00

I am creating a new cart addAction() in magento’s checkout/controllers/cartController.php. My need is, Whenever

  • 0

I am creating a new cart addAction() in magento’s checkout/controllers/cartController.php. My need is, Whenever a product is added to cart, all existing products in cart should be cleared except currently added one. That is,

If Nokia 3220 is added to an empty cart, It should be placed as an item.

And Later if Nokia N72 is added, Nokia 3220(ie the previous item) should be cleared from cart and Nokia N72 should be placed and so on.

I override the CartController.php and added the code to begining of addAction() in CartController.php,

$checkout_my_cart = Mage::getSingleton(‘checkout/cart’);
$current_items = $checkout_my_cart->getItems();
foreach ($current_items as $item)
{
$itemId = $item->getItemId();
$checkout_my_cart->removeItem($itemId)->save();
}

But It clears the entire cart when I add a new item to replace existing one! I think It should not do it since I added the code in the begining of Addaction(). I tried by defining the above code as a function and calling it with addAction(). But story seems to be the same.

Any help will be appreciated.

Please Help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:10:49+00:00Added an answer on June 15, 2026 at 6:10 am

    You should consider using observer rather then overriding controller. If you look at the addItem method within Mage_Sales_Model_Quote you will find that event sales_quote_add_item.

    You can observe it, fetch the quote_item (argument to event) and delete items other then this one (was typing by hand so please double check for errors):

    public function observeAddItem($observer) 
    {
      $item = $observer->getEvent()->getQuoteItem();
      $cart = Mage::getSingleton('checkout/cart');
      foreach ($cart->getQuote()->getItemsCollection() as $_item) {
        if($_item->getId() != $item->getId()) {
          $_item->isDeleted(true);
        }
      }
    }
    

    Overriding classes (especially controllers) should be your last resort.

    More info on how to observe event can be found on magento wiki:

    http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to MySQL. I am creating a checkout page in PHP. When the
I need UNION two tables with creating new field, where 1 for first table,
I need to restrict the development users from creating new schedules for SQL Agent
when i am creating new project i added API level 14 , i have
While creating new records. I need to create more records for the same model.
when i am creating new android project from visual studio 2010 it gives the
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
When I'm creating new project with GWT plug in it creates a skeleton project
I'm creating new Site Definitions using this method: http://weblogs.asp.net/paulballard/archive/2007/04/09/creating-a-custom-sharepoint-2007-portal-site-definition-using-the-portalprovisioningprovider-class.aspx and when they get created,
I'm trying to establish workflows for creating new projects and for setting old projects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.