cancel
Showing results for 
Search instead for 
Did you mean: 

How to check Cart contains only selected category of products?

How to check Cart contains only selected category of products?

My observer condition based on Cart products, how to check Cart contains only selected category of products?

What event i need to use?

4 REPLIES 4

Re: How to check Cart contains only selected category of products?

Hi @Aveeva

Not sure what is your exact requirement. There can be many ways to do this.

1) Do not show the category specific products to customers which you do not want to allow in the cart.
2) If you are showing the products to customers, you may prevent them to add those products to cart.

You can use an observer to validate whether the product belongs to a category or not.

Use following in your module's config.xml

<events>
    <checkout_cart_product_add_after>
        <observers>
            <check_product_add_after>
                <class>namespace_modulename/observer</class>
                <method>checkCategory</method>
            </check_product_add_after>
        </observers>
    </checkout_cart_product_add_after>
</events>

In you Observer you may perform the validation

class Namespace_Module_Model_Observer
{
    public function checkCategory(Varien_Event_Observer $observer)
    {
            $event = $observer->getEvent();
            $product = $event->getProduct();
            // now write the logic to find out the category
            // If product not from allowed category
             Mage::throwException(Mage::helper('core')->__('Can not be added to cart'));
    }
}

 

---
Problem Solved Click Accept as Solution!:Magento Community India Forum

Re: How to check Cart contains only selected category of products?

@Mukesh Tiwari  For my observer condition, how to check if cart contains only selected category of products, for eg, the cart have 2 products and same category of products. How to check this condition? 

Re: How to check Cart contains only selected category of products?

Hi @Aveeva
Please try following code.

 

$cart = Mage::getModel('checkout/cart')->getQuote();
$allowedCategories = array(1,2,..);
foreach ($cart->getAllItems() as $item) {
    $productId = $item->getProduct()->getName();
    $product = Mage::getModel('catalog/product')->load($productId);
    $categories = $product->getCategoryIds();
    foreach ($categories as $category_id) {
    $cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
      // Write you logic here to compare 
    }
}

 

 

---
Problem Solved Click Accept as Solution!:Magento Community India Forum

Re: How to check Cart contains only selected category of products?

I am new to PHP, can i get help.

 

My Work:

 

I am using Extra Fees EXtension working good and i added a condition like if the shipping method is flat-rate then only will add Extra fees, everything working good.

 

code : https://paste.ofcode.org/zQrXdkpd3HaJuFMANPC9ex  line 54 - 88

 

Here How can i add condition like, if cart contains only category id 5 products, no need to add extra fees, how can i use your code.

 

FYI - Category id 5 products is tricky to packing as a single pack so we have to do two packings that's what adding extra charges. If the cart contains only category 5 products no need to create two packing right, here how can i add the above condition[if cart contains only category id 5 products, no need to add extra fees ]