1

Actually we are going to sell Items very cheap on our Sale Day for Marketing So we want that one customer can place only one order on that day.

I have already made a module so that customer will have only one product in entire cart but I am not getting how can I limit Number of orders by customer on that day.

1

You can achieve this on a same way as you are checking cart for one product at a time.

You have to call a event checkout_cart_product_add_after OR checkout_cart_product_add_before

In this same method you will get the customer detail from session . You just need to load all order of the current customer and filter with current date. if you get any order data then you can remove that quote and give a message there.

class Fooman_Example_Model_Observer
{
    public function catalogProductTypePrepare($observer)
    {
        $quote = Mage::getSingleton('checkout/session')->getQuote();
        if (Mage::getSingleton('customer/session')->isLoggedIn()) {
            $customerData = Mage::getSingleton('customer/session')->getCustomer();
            $customerId = $customerData->getId();

            $fromDate = date('Y-m-d 00:00:00');
            $toDate = date('Y-m-d 59:59:59');
            $order = Mage::getModel('sales/order')->getCollection()
                ->addFieldToFilter('customer_id', $customerId)
                ->addAttributeToFilter('created_at', array('from' => $fromDate, 'to' => $toDate));
    }

        if (count($order)) {
            Mage::throwException('You already ordered today.');
        }
    }
}

The events xml

    <?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>
        <sales_order_place_before>
        <observers>
            <library>
            <type>model</type>
            <class>library/observer</class>
            <method>AddProductSaveBefore</method>
            </library>
        </observers>
        </sales_order_place_before>
     </events>
    </frontend>
</config>
10
  • I used most voted answer on this page for one item per cart magento.stackexchange.com/questions/9584/… Ashish, Can you describe in brief how to do that? – Lalji Sep 8 '16 at 7:26
  • You can use my updated answer, This is not checked code so please check it properly. – Ashish Jagnani Sep 8 '16 at 7:58
  • Ashish, Thanks for your Help, It is working so fine after small edit but It has one issue that if customer is not logged in and then customer adds product to cart without logging in and customer logs in at checkout page and can place order successfully. Do you have any idea about that? – Lalji Sep 8 '16 at 11:15
  • Ashish, Can we check before placing order instead of checking before adding to cart that customer has bought before or not? – Lalji Sep 8 '16 at 12:04
  • Yes we can, just change the event and make a new function for this event. Check the new event in my updated answer – Ashish Jagnani Sep 8 '16 at 12:21

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.