1

I'm building a Magento store with several type of products. One of them has a specific payment method that is unapplicable to others. Is it possibile to check if different type of products are in the cart and show an error message? As an alternative, Is it possible to force one type of products to be bought alone?

2 Answers 2

1

Yes absolutely it is possible. You can utilize magento's event observer method to do that.

There are loads of other observer available to use in cart page, however I think this would be better one: checkout_cart_product_add_after. All event list

You will have to create a simple extension to observer your cart behaviour.

Learn extension creation here

Now in your config.xml file:

<global>
    .......
    <events>
        <checkout_cart_product_add_after>
            <observers>
                <unique_name>
                    <type>singleton</type>
                    <class>NameSpace_ModuleName_Model_Observer</class>
                    <method>checkProductType</method>
                </unique_name>
            </observers>
        </checkout_cart_product_add_after>      
    </events>
</global>

Now in your observer:

class NameSpace_ModuleName_Model_Observer
{
    public function checkProductType($observer)
    {
       $product = $observer->getEvent()->getProduct();
       $productType = $product->getTypeId();

       //if above doesn't work use this
       //$productType = $product->getResource()->getTypeId();

       $currentItem = $observer->getEvent()->getQuoteItem();
       $quote = $currentItem->getQuote();
       $quoteItems = $quote->getItems();

       if($quoteItems){
          foreach($quoteItems as $item{
             if($item->getTypeId() != $productType){
                $quote->removeItem($product->getId()); //remove just added product.
                Mage::throwException('This is the message to the customer.');
             }
          }
       }

    }   
}

I haven't tested this but should work. Good luck.

0

Goto your specific payment method class file and modify method isAvailable() if this method exist in your class or create new one.

magento call this method to check if this payment method is available for checkout or not.

return flag based on product in cart (allow if cart contain your specific product only).

Your Answer

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

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