0

I need to add a condition for my category in the observer, how can I call category?

  • @ShafeelSha ver : 1.9.2.3 – zus Aug 3 at 5:18
  • Please check updated answer – Shafeel Sha Aug 3 at 5:23
1

You can try Something like this

$product = Mage::registry('current_product'); 

$prodID = $product->getId();

$_product = Mage::getModel('catalog/product')->load($prodID);

$categoryIds = $_product->getCategoryIds();

if($categoryIds[0]==4){
  // Do something
}else{
return;
}
  • I need to give condition for my specific category, like ID : 4, how can i call id 4 category in observer? – zus Aug 3 at 5:09
  • check my updaed answer – Waqar Ali Aug 3 at 5:11
  • How can i check condition like, if cart contains only category Id =4 products – zus Aug 17 at 5:58
1

Get category and subcategory:

<?php 
$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
if (count($_categories) > 0){
    foreach($_categories as $_category){
        $_category = Mage::getModel('catalog/category')->load($_category->getId());
        $_subcategories = $_category->getChildrenCategories();
        if (count($_subcategories) > 0){
            echo $_category->getName();
            echo $_category->getId();      
            foreach($_subcategories as $_subcategory){
                 echo $_subcategory->getName();
                 echo $_subcategory->getId();
            }
        }
    }
}
?>

To get only one category: (put category Id as we want)

<?php
$category = Mage::getModel('catalog/category')->load(4);
$subcategories = $category->getChildrenCategories();
if (count($subcategories) > 0){
    echo $category->getName();
    foreach($subcategories as $subcategory){
        echo $subcategory->getName();
    }
}
?>

for more please visit

  • How can i use in the observer, my condition: if the selected category of products available in the cart no other products add to cart, and message like restricted products available in the cart you need to place a different order. – zus Aug 3 at 5:40
  • How to add more than one category id in Mage::getModel('catalog/category')->load(4); @Shafeel Sha – zus Aug 3 at 11:57
  • please use first method , you will get category collection – Shafeel Sha Aug 3 at 12:05
  • How can i assign my selected category id's – zus Aug 3 at 12:24
  • foreach($_categories as $_category){ echo $_category->getId(); } – Shafeel Sha Aug 3 at 12:37

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.