1

In magento, Is it possible to make selected categories products can be purchased standalone, currently this feature is available for recurring profile enabled products.

I need this feature for particular categories products.

In this category i have simple or virtual products only.

When i add this product to cart, it need to check whether some products is already in cart, if not it can be added to cart else it should report error message,

This products can order individually not combined with other products.

Any Ideas?

  • it's not very clear what you're asking for. A simple product can be purchased standalone. But I guess that's not what you mean. Please update your question with some more specific information – Sander Mangel Feb 20 '14 at 9:30
  • A simple or virtual product can be purchased standalone but this for selected categories only. – Helphin Feb 20 '14 at 9:38
  • Then add those products only to the categories you want them to be purchased from? – Sander Mangel Feb 20 '14 at 9:43
  • sander you not understand clearly. My question is, whether This feature can able to do and only for particular category?. – Helphin Feb 20 '14 at 9:48
  • So you can purchase only 1 of this product? – Sander Mangel Feb 20 '14 at 9:53
2

This can be done with an observer in a custom extension. You need to hook 2 events, 1 for adding products to cart, the second is for updating the items in the cart.

config.xml

<config>
    [...]
    <frontend>
        <events>
            <checkout_cart_update_items_before>
                <observers>
                    <[module]_update_item>
                        <class>[module]/observer</class>
                        <method>updateItem</method>
                    </[module]_update_item>
                </observers>
            </checkout_cart_update_items_before>
            <checkout_cart_product_add_before>
                <observers>
                    <[module]_add_product>
                        <class>[module]/observer</class>
                        <method>addProduct</method>
                    </[module]_add_product>
                </observers>
            </checkout_cart_product_add_before>
        </events>
    </frontend>
    [...]
</config>

And your Observer.php would look something like this. 2 methods, one for each event checking that the product or item added is not crossing the purchase limit. To limit this to a select number of products I'm working with an attribute called limit_purchase_quantity. If it has a value of 1 then there is a purchase limit. You will need to create this attribute or modify the code.

class [Namespace]_[Module]_Model_Observer
{

    /**
     * addProduct
     * check product exists when adding to cart
     * 
     * @param object $observer
     * @return n/a
     */
    public function addProduct(Varien_Event_Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();

        $item_collection = Mage::getModel('sales/quote_item')->getCollection()
                                ->setQuote(Mage::getSingleton('checkout/session')->getQuote())
                                ->addFieldToFilter('product_id', $product->getId());

        if ($product->getData('limit_purchase_quantity') == 1 && $item_collection->getSize() > 0)
        {
            Mage::getSingleton('core/session')->addError(Mage::helper('[module]')->__('You\'re only allowed to purchase one of these items'));
            Mage::app()->getResponse()->setRedirect(Mage::helper('checkout/cart')->getCartUrl());
            Mage::app()->getResponse()->sendResponse();
            exit;
        }
    }


    /**
     * updateItem
     * prevents from updating cart quantity
     * 
     * @param object $observer
     * @return n/a
     */
    public function updateItem(Varien_Event_Observer $observer)
    {
        $event = $observer->getEvent();
        $data = $event->getInfo();
        $quote = $event->getCart()->getQuote();

        foreach ($data as $item_id => $qty)
        {
            if (
                $quote->getItemById($item_id)->getProduct()->getData('limit_purchase_quantity') == 1 && // does this product have a purchase limit
                $qty['qty'] > 1 // and is quantity over 1
            )
            {
                Mage::getSingleton('core/session')->addError(Mage::helper('[module]')->__('You\'re only allowed to purchase one of these items'));
                Mage::app()->getResponse()->setRedirect(Mage::helper('checkout/cart')->getCartUrl());
                Mage::app()->getResponse()->sendResponse();
                exit;
            }
        }
    }
}

I haven't tested this code but took parts from another working extension I've made before so you might have to tweak at some points

  • Sander, not working. both observer function not called. I have also searched but both the events "checkout_cart_update_items_before" and "checkout_cart_product_add_before" not found. – Helphin Feb 20 '14 at 10:53
  • i have created the action now the function calling, and its working fine . – Helphin Feb 20 '14 at 11:19
  • I'm glad I could help. Be sure to accept the answer to close your question if it solved the issue :) – Sander Mangel Feb 20 '14 at 11:20
  • You're welcome. Glad I could help – Sander Mangel Feb 20 '14 at 12:08

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.