我如何获得关于我当前所在商店的产品所属类别的ID [英] How do I get the category ids that a product is in with respect to the store that I'm currently on

查看:59
本文介绍了我如何获得关于我当前所在商店的产品所属类别的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在产品页面上并拥有产品对象,但是当我尝试使用以下方法获取类别ID时:

I'm on a product page and have the product object but when I try to get the category ids using:

$_product->getCategoryIds();

或:

$_product->getResource()->getAttribute('category_ids')->getFrontend()->getValue($_product); 

它为我提供了所有类别ID,而我只想要我所在的商店的ID.

it gets me all the category ids and I just want the ones for the store I'm on.

这是一个多商店环境,因此是我的问题. 其他一切似乎都还可以,类别列表也可以正常工作. 这是我唯一的问题. 有人可以帮忙吗?

It's a multistore environment so hence my problem. Everything else seems ok and the category list works fine. This is my only problem. Can anyone help?

推荐答案

与Alans答案非常相似,循环可能更少:

Pretty similar to Alans answer, maybe a bit less looping:

$rootCategory = Mage::getModel('catalog/category')
    ->load(Mage::app()->getStore()->getRootCategoryId());

$sameStoreCategories = Mage::getResourceModel('catalog/category_collection')
    ->addIdFilter($product->getCategoryIds())
    ->addFieldToFilter('path', array('like' => $rootCategory->getPath() . '/%'))
    ->getItems();

var_dump(array_keys($sameStoreCategories));

这将始终有效.丑陋的是,您仍然需要加载类别.

This will always work. The ugly thing is that you still need to load the categories.

如果启用了平面类别表,则可以使用以下变体:

Here is a variation you can use if the flat category tables are enabled:

$sameStoreCategories = Mage::getResourceModel('catalog/category_flat_collection')
    ->addIdFilter($product->getCategoryIds())
    ->getItems();

var_dump(array_keys($sameStoreCategories));

为什么起作用?因为平面表是按商店建立索引的,并且每个平面表仅包含与该商店组根类别相关联的类别实体记录.

Why does it work? Because the flat tables are indexed by store, and each flat table only contains the category entity records that are associated with that store groups root category.

因此,即使您按与该产品关联的所有类别ID进行过滤,该集合也将仅包含当前商店中存在的类别.

So even though you are filtering by all category IDs associated with the product, the collection will only contain the categories present in the current store.

这篇关于我如何获得关于我当前所在商店的产品所属类别的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆