0

I needed to force only one product per order and one amount.

The amount of the product already resolved, now need a solution for just one product per order.

A single product per application, the cart display only one product, where to buy the second notice that it is not possible to purchase more than one product per order

2
  • This is not clear. Do you want a maximum of 1 product in the shopping cart for each customer, or would you like to split an order for multiple products into multiple orders with one product?
    – Niels
    Commented Feb 4, 2015 at 14:24
  • I updated the question, I hope I have left clearer @user5972
    – Dexxtz
    Commented Feb 4, 2015 at 14:28

4 Answers 4

2

If you've already handled the amount of items per products and you want to restrict the user from adding to cart any additional products, you'll need to listen to an event to manage this. i.e. Can't add Product B, because you already have a Product A in the cart.

There's an event called checkout_cart_add_product_complete that may be suitable. The problem with this is that the product has already been added to the cart at this point.

Your observer would need to handle the following flow:

  1. Identify the product that the user tried to add to cart
  2. Remove this product from the current cart
  3. Add an error message to the session

This should remove the product every single time someone tries to add it to the cart, and then from this point should inform the user that they've exceeded their maximum.

If you've no experience setting up Events and Observers, I'd recommend reading up on the matter:

http://www.solvingmagento.com/events-and-observers-a-magento-tutorial/
http://code.tutsplus.com/tutorials/understanding-magento-observers--cms-21031

1
  • @Dexxtz let me know how it goes :)
    – Dan Hanly
    Commented Feb 4, 2015 at 21:59
1

My solution was that I added in head.phtml the code below I made:

<?php
$current_page = '';
if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms'):
    $current_page = Mage::getSingleton('cms/page')->getIdentifier();
endif;
if(empty($current_page)):
    $current_page = Mage::app()->getFrontController()->getRequest()->getRouteName();
endif;

if ($current_page == 'checkout') {
    $cartHelper = Mage::helper('checkout/cart');
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    if($quote->getItemsCount() > 1) {
        Mage::getSingleton('core/session')->addError('Message');
        $i = 0;
        foreach ($quote->getAllItems() as $item) {
            $i++;
            if ($i > 1)
                $cartHelper->getCart()->removeItem($item->getId())->save();
                Mage::app()->getResponse()->setRedirect(Mage::helper('core/url')->getCurrentUrl());
        }
    }
}
?>
1
  • I'd recommend against adding this code into a template. It goes against the MVC principles; as this is logic, it should be added to an appropriate class outside of the templating system. Using the event/observer pattern for this would be far better practice. If you ever change your theme, you'd lose this essential functionality; which is why it should exist outside of the templates.
    – Dan Hanly
    Commented Feb 8, 2015 at 23:17
0

You can set this in the Product configuration itself under Inventory

enter image description here

2
  • 1
    Is not the quantity of the product, but the amount of products
    – Dexxtz
    Commented Feb 4, 2015 at 14:36
  • Ok np. I thought it was Maximum Qty based on the original question. It appears that you want to restrict it to only 1 item in the entire cart. If this is the case then you should see this answer: magento.stackexchange.com/questions/9584/… Commented Feb 4, 2015 at 14:51
0

Take a look at How to allow only one product in a cart?

You could also try adding an observer for sales_quote_add_item

<sales_quote_add_item>
    <observers>
        <customoptions>
            <type>singleton</type>
            <class>magepal/observer</class>
            <method>checkoutCartProductAddBefore</method>
        </customoptions>
    </observers>
</sales_quote_add_item>

In your observer

.....
public function checkoutCartProductAddBefore($observer)
{
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    if($quote->getItemsCount()>=1){
        Mage::throwException('You can only buy one product at a time.');
    }
}

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