3

I want to get the name of current category name of a product after add to cart for this I'm using this $category = Mage::registry('current_category') but it shows null on observer.

so which observer I should call to get this value after add to cart the product.

Please help me thank you.

  • What event are you observing? Mage::registry('current_category') is only available on catalog_category_view – Douglas Radburn Feb 26 '18 at 9:00
  • thank you.so how could i get this value when i add to cart product help i am new learner – Shubhajay Das Feb 26 '18 at 9:03
  • Okay, a few questions. 1) What do you want to do with the category name? And 2) what happens if a product is in multiple categories? – Douglas Radburn Feb 26 '18 at 9:06
  • yes it may be comes from multiple categories .i want to store the current category name in custom table from which current category its viewed and ordered. – Shubhajay Das Feb 26 '18 at 9:23
2

You should use following way to get product's categories in observer.

Declare event observers in config.xml

<events>
    <checkout_cart_product_add_after>
        <observers>
            <custommodule>
                <class>custommodule/observer</class>
                <method>cartProductAddAfter</method>
            </custommodule>
        </observers>
    </checkout_cart_product_add_after>
    <checkout_cart_product_update_after>
        <observers>
            <custommodule>
                <class>custommodule/observer</class>
                <method>cartProductUpdateAfter</method>
            </custommodule>
        </observers>
    </checkout_cart_product_update_after>
</events>

Develop the Observers handlers

class Vendor_Custommodule_Model_Observer 
{
    /* If you'd like to do the same while updating the shopping cart*/
    public function cartProductUpdateAfter($observer)
    {
        $this->cartProductAddAfter($observer);
    }

    public function cartProductAddAfter($observer)
    {
        $product_id = $observer->getEvent()->getProduct()->getId();

        //Get the category ids
        $product = Mage::getModel('catalog/product')->load($product_id);
        $cats = $product->getCategoryIds();

        //One product can associate with more then one categories. 
        foreach ($cats as $category_id) {
            $_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
            echo $_cat->getName();             
        }
    }
}

Another way

public function cartProductAddAfter($observer)
    {
        $_product = $observer->getEvent()->getProduct();
        $catIds = $_product->getCategoryIds();
        $catCollection = Mage::getResourceModel('catalog/category_collection')
                         ->addAttributeToSelect('*')
                         ->addAttributeToFilter('entity_id', $catIds)
                         ->addIsActiveFilter();

        foreach($catCollection as $cat){
            print_r($cat->getData());
            //echo $cat->getName();
            //echo $cat->getUrl();
        }
    }
  • Please do not use load() in a loop :) – sv3n Feb 26 '18 at 9:56
  • can you tell me why should I not use load() and how to get category data without load() ? – Aasim Goriya Feb 26 '18 at 10:00
  • As it is in multiple category so it get all the categories names.but i want just the current category name from which it add to cart.thank you. – Shubhajay Das Feb 26 '18 at 10:01
  • how can you identify which one is current of your product have multiple categories assigned – Aasim Goriya Feb 26 '18 at 10:04
  • 1
    @sv3n check my updated ans without load() ;) – Aasim Goriya Feb 26 '18 at 10:08
1

You should not use $category = Mage::registry('current_category') while adding a product to cart.

You can do the following.

//Get the category ids
$product = Mage::getModel('catalog/product')->load($product_id);
$cats = $product->getCategoryIds();

One product can associate with more then one categories.

foreach ($cats as $category_id) {
    $_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
        echo $_cat->getName();             
    }
  • wherever you want category id – Magento Learner Feb 26 '18 at 10:20
  • i just want current category name it shows all the categories wherever it is set that all name.thank you – Shubhajay Das Feb 26 '18 at 10:28
  • this is the magento's default functionality. A product can have one or more categories. it is you which category you want to display. – Magento Learner Feb 26 '18 at 11:18
-1

in config.xml singleton Customization_Holdcategory_Model_Observer categorypath

<checkout_cart_product_add_after>
    <observers>
        <holdcategoryi>
            <type>singleton</type>
            <class>Customization_Holdcategory_Model_Observer</class>
            <method>cartProductAddAfter</method>
        </holdcategoryi>
    </observers>
</checkout_cart_product_add_after>
<checkout_cart_product_update_after>
    <observers>
        <holdcategoryii>
            <type>singleton</type>
            <class>Customization_Holdcategory_Model_Observer</class>
            <method>cartProductUpdateAfter</method>
        </holdcategoryii>
    </observers>
</checkout_cart_product_update_after>

in observer.php

<?php

class Customization_Holdcategory_Model_Observer { protected $_myDropdownValue;

public function categorypath(Varien_Event_Observer $observer)
{
    $categoryId = '';
    if ($category = Mage::registry('current_category')) {
        Mage::getSingleton('core/session')->setcurrentid($category->getId());
        echo $category->getId();
    }
}
/* If you'd like to do the same while updating the shopping cart*/
public function cartProductUpdateAfter($observer)
{
    $this->cartProductAddAfter($observer);
}

public function cartProductAddAfter($observer)
{
    $currentcatid = Mage::getSingleton('core/session')->getcurrentidl();
    $_product = $observer->getEvent()->getProduct();
    $catIds = $_product->getCategoryIds();
    $catCollection = Mage::getResourceModel('catalog/category_collection')
                     ->addAttributeToSelect('*')
                     ->addAttributeToFilter('entity_id', $catIds)
                     ->addIsActiveFilter();

    foreach($catCollection as $cat){
        if($currentcatid == $cat->getId())
        echo "<br>".$cat->getId();
    }die;
}

}

?>

  • How to get exact category using ID in observer? – zus Aug 3 at 5:06

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.