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

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

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?