2

Is there a way to only allow one item to be added to the shopping cart and then checked out.

I tried the event + observer addition in this link: How to allow only one product in a cart?

But it doesn't seem to work with V1.9.2.3 As I am still able to add more than item to the cart and make it all the way to checkout.

3
  • Possible duplicate of How to allow only one product in a cart? Mar 12, 2016 at 10:07
  • Post related code of what you have tried.
    – benmarks
    Mar 13, 2016 at 18:02
  • A created new folders under: app/code/community/CartLimit/Cart In there I made both an ect folder and a Model folder The ect folder has the config.xml and the module folder contains the observer.php file Mar 18, 2016 at 17:25

2 Answers 2

2

he events catalog_product_type_prepare_full_options and catalog_product_type_prepare_lite_options are your friends

<?xml version="1.0"?>
<config>
    <modules>
        <Fooman_Example>
            <version>0.1.0</version>
        </Fooman_Example>
    </modules>
    <global>
        <models>
            <fooman_example>
                <class>Fooman_Example_Model</class>
            </fooman_example>
        </models>
        <helpers>
            <fooman_example>
                <class>Fooman_Example_Helper</class>
            </fooman_example>
        </helpers>
    </global>
    <frontend>
        <events>
            <catalog_product_type_prepare_full_options>
                <observers>
                    <fooman_example_catalog_product_type_prepare>
                        <class>Fooman_Example_Model_Observer</class>
                        <method>catalogProductTypePrepare</method>
                    </fooman_example_catalog_product_type_prepare>
                </observers>
            </catalog_product_type_prepare_full_options>
        </events>
    </frontend>
</config>

Then in your Observer class

class Fooman_Example_Model_Observer
{
    public function catalogProductTypePrepare($observer)
    {
        $quote = Mage::getSingleton('checkout/session')->getQuote();
        if($quote->getItemsCount()>=1){
            Mage::throwException('You can only buy one product at a time.');
        }
    }
}
1
  • I added something similar to this except it was by somebody else and where it says fooman its their signature. But I have a config file in the ect folder and then in the module folder a php file called observer with the above code Mar 18, 2016 at 17:27
1

I don't think it's a good idea to throw error. Rather do init checkout before add product to cart will clear the old cart and create the cart with the newly added product alone. Init checkout is a child function available in mage checkout model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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