How to get Categories of products In Observer?Restrict products to purchase using Observer not triggerHow to stop process or flow of a event observerHow to call Observer method in controller in Magento?Magento 2 : Event Observer For Payment Method SelectionHow to get product category data from observerCall phtml File in ObserverHow to get the current category name in an observer in magento 1how to trigger observer by rest apiObserver for import productsMagento2: How can call observer function in controller file?Magento 2 - How to call sales_order_save_after Observer manually for testing?

Weird resistor with dots around it

How to gracefully leave a company you helped start?

Attacking the Hydra

What is the most difficult concept to grasp in Calculus 1?

How to prevent criminal gangs from making/buying guns?

Word for an event that will likely never happen again

How to not forget things?

Why aren’t there water shutoff valves for each room?

How would armour (and combat) change if the fighter didn't need to actually wear it?

Doesn't the speed of light limit imply the same electron can be annihilated twice?

Why not demand President's/candidate's financial records instead of tax returns?

What are the odds of rolling specific ability score totals in D&D?

Why won't the Republicans use a superdelegate system like the DNC in their nomination process?

Go to last file in vim

Cycle of actions and voice signals on a multipitch climb

Running code generated in realtime in JavaScript with eval()

Should I leave building the database for the end?

Human with super efficient metabolism

Will using a resistor in series with a LED to control its voltage increase the total energy expenditure?

Do you "gain" 1st level?

Is Binary Integer Linear Programming solvable in polynomial time?

Finding the shaded region

Why did IBM make the PC BIOS source code public?

How would you translate this? バタコチーズライス



How to get Categories of products In Observer?


Restrict products to purchase using Observer not triggerHow to stop process or flow of a event observerHow to call Observer method in controller in Magento?Magento 2 : Event Observer For Payment Method SelectionHow to get product category data from observerCall phtml File in ObserverHow to get the current category name in an observer in magento 1how to trigger observer by rest apiObserver for import productsMagento2: How can call observer function in controller file?Magento 2 - How to call sales_order_save_after Observer manually for testing?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















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










share|improve this question
























  • @ShafeelSha ver : 1.9.2.3

    – zus
    Aug 3 at 5:18











  • Please check updated answer

    – Shafeel Sha
    Aug 3 at 5:23


















0















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










share|improve this question
























  • @ShafeelSha ver : 1.9.2.3

    – zus
    Aug 3 at 5:18











  • Please check updated answer

    – Shafeel Sha
    Aug 3 at 5:23














0












0








0








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










share|improve this question














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







magento-1.9 event-observer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 3 at 4:59









zuszus

571 silver badge20 bronze badges




571 silver badge20 bronze badges















  • @ShafeelSha ver : 1.9.2.3

    – zus
    Aug 3 at 5:18











  • Please check updated answer

    – Shafeel Sha
    Aug 3 at 5:23


















  • @ShafeelSha ver : 1.9.2.3

    – zus
    Aug 3 at 5:18











  • Please check updated answer

    – Shafeel Sha
    Aug 3 at 5:23

















@ShafeelSha ver : 1.9.2.3

– zus
Aug 3 at 5:18





@ShafeelSha ver : 1.9.2.3

– zus
Aug 3 at 5:18













Please check updated answer

– Shafeel Sha
Aug 3 at 5:23






Please check updated answer

– Shafeel Sha
Aug 3 at 5:23











2 Answers
2






active

oldest

votes


















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;






share|improve this answer



























  • 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


















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






share|improve this answer

























  • 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








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f284284%2fhow-to-get-categories-of-products-in-observer%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









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;






share|improve this answer



























  • 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















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;






share|improve this answer



























  • 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













1












1








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;






share|improve this answer















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;







share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 3 at 5:11

























answered Aug 3 at 5:08









Waqar AliWaqar Ali

61616 bronze badges




61616 bronze badges















  • 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

















  • 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
















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





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





check my updaed answer

– Waqar Ali
Aug 3 at 5:11













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






share|improve this answer

























  • 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















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






share|improve this answer

























  • 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













1












1








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






share|improve this answer














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







share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 3 at 5:19









Shafeel ShaShafeel Sha

47315 bronze badges




47315 bronze badges















  • 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

















  • 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
















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 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





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





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






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





foreach($_categories as $_category) echo $_category->getId();

– Shafeel Sha
Aug 3 at 12:37

















draft saved

draft discarded
















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f284284%2fhow-to-get-categories-of-products-in-observer%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form