简体   繁体   中英

Magento getting specific category id from product

I'm currently using the following snippet to get my category ID for each product.

$categoryIds = $_product->getCategoryIds();

foreach($categoryIds as $categoryId) {
    $category = Mage::getModel('catalog/category')->load($categoryId);
    echo $category->getName();
    echo $category->getUrlPath();
 }

I have then been passing the result of that into a switch to identify the category name.

My problem is that I have just added a sale category that some products will be put into, and the result of

echo $category->getName(); 

is the sale category and not the "actual" category it is in.

Does anyone have suggestions for how to make it a) ignore the sale category, b) get next category id stored for the product?, c) make it work some other way.

Any help would be greatly appreciated!

Not really sure what is the goal, but you can do something like

if ($category->getName == 'Sales') {
    continue;
}

or array_filter $categoryIds excluding Sales category

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM