0

How to allow only one product in a cart?

I want to limit number of order by customer on our Sale Day

How can i use both Features at the same time

I have given the answer, I want only a magento developer to check if its correct?

0

config.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>
        <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>
        <sales_order_place_before>
         <observers>
           <fooman_example_order_observer>
            <type>model</type>
            <class>Fooman_Example_Model_Observer</class>
            <method>AddProductSaveBefore</method>
           </fooman_example_order_observer>
         </observers>
        </sales_order_place_before>
    </events>
</frontend>
</config>

Observer

<?php
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.');
}
}

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

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


if($quote->getItemsCount()>=1){
    Mage::throwException('You can only Buy one product at a time. Please Change your Cart Items.');
}
if (count($order)>=1) {
    Mage::throwException('You already ordered today');
}
}
} 

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.