Magento 产品类别

发布于 2024-10-10 05:21:17 字数 232 浏览 5 评论 0原文

我必须列出产品及其类别,我只有产品的 SKU,我需要找到它属于哪个类别,所以我想知道此信息保留在哪个 magento 表中。

即:对于sku 52429,它分为3类。该报告将显示所有 3 个类别树:

BL >头发护理>定型产品

Bl>自然&有机>头发护理>定型产品

BL>我们的品牌 >纯学>造型师

谢谢! 里查

I have to list products with its category or categories, I have only products' SKU by it I need to find which category it belongs, so I want to know in which magento table this information stay.

ie: for sku 52429, it is categorized into 3 categories. the report would show all 3 category trees:

Bl > Hair Care > Styling products

Bl > Natural & Organic > Hair Care > Styling Products

Bl > Our Brands > Pureology > Stylers

Thanks!
Richa

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久隐师 2024-10-17 05:21:17

Magento 类别存储在catalog_category_entity 中(pk 为entity_id)。要查找产品和类别之间的关系,请使用catalog_category_product。它的结构很简单:

+-------------+------------+----------+
| category_id | product_id | position |
+-------------+------------+----------+
|           3 |          5 |        1 |
|           3 |          6 |        1 |
|           3 |          7 |        1 |
+-------------+------------+----------+

因此,要获取产品的所有类别:

select cc.* from catalog_category_entity cc
   join catalog_category_product cp on cc.entity_id = cp.category_id
   where cp.product_id = {{your product id}};

编辑以注意您要查找的信息(以显示类别树)位于类别表本身中。列摘录(部分省略):

+-----------+-----------+-------+----------+-------+----------------+
| entity_id | parent_id | path  | position | level | children_count |
+-----------+-----------+-------+----------+-------+----------------+
|         1 |         0 | 1     |        0 |     0 |             65 |
|         2 |         1 | 1/2   |        1 |     1 |             64 |
|         3 |         2 | 1/2/3 |        1 |     2 |              9 |
|         4 |         2 | 1/2/4 |        2 |     2 |             18 |
|         5 |         2 | 1/2/5 |        3 |     2 |              9 |
+-----------+-----------+-------+----------+-------+----------------+

您可以在 path 列上使用 split 来获取路径中所有类别的类别 ID,并为报告加载它们的名称。

Magento categories are stored in catalog_category_entity (pk is entity_id). To find the relationship between a product and a category, use catalog_category_product. Its structure is simple:

+-------------+------------+----------+
| category_id | product_id | position |
+-------------+------------+----------+
|           3 |          5 |        1 |
|           3 |          6 |        1 |
|           3 |          7 |        1 |
+-------------+------------+----------+

So, to get all categories for a product:

select cc.* from catalog_category_entity cc
   join catalog_category_product cp on cc.entity_id = cp.category_id
   where cp.product_id = {{your product id}};

EDIT to note that the info you are looking for (to display category trees) is in the category table itself. An excerpt of the columns (some omitted):

+-----------+-----------+-------+----------+-------+----------------+
| entity_id | parent_id | path  | position | level | children_count |
+-----------+-----------+-------+----------+-------+----------------+
|         1 |         0 | 1     |        0 |     0 |             65 |
|         2 |         1 | 1/2   |        1 |     1 |             64 |
|         3 |         2 | 1/2/3 |        1 |     2 |              9 |
|         4 |         2 | 1/2/4 |        2 |     2 |             18 |
|         5 |         2 | 1/2/5 |        3 |     2 |              9 |
+-----------+-----------+-------+----------+-------+----------------+

You can use split on that path column to get the category IDs of all the categories in the path, and load their names for the report.

十秒萌定你 2024-10-17 05:21:17

加载产品模型

首先按 ID

$product = Mage::getModel('catalog/product')->load($id);

或按属性 (SKU)

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', '52429');

现在您可以加载类别 ID

$categoryIds = $product->getCategoryIds();

然后获取完整的类别对象

foreach($categoryIds as $categoryId) { 
  $categories[] = Mage::getModel(’catalog/category’) 
    ->setStoreId(Mage::app()->getStore()->getId()) 
    ->load($categoryId);
}

现在获取每个类别的父级

foreach($categories as $category) {
  $category->getParentCategory();
}

我认为这就是您所需要的。

First load up the product model

Either by ID

$product = Mage::getModel('catalog/product')->load($id);

or by attribute (SKU)

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', '52429');

Now you can load the category IDs

$categoryIds = $product->getCategoryIds();

Then to get the full category objects

foreach($categoryIds as $categoryId) { 
  $categories[] = Mage::getModel(’catalog/category’) 
    ->setStoreId(Mage::app()->getStore()->getId()) 
    ->load($categoryId);
}

Now to get the parent of each category

foreach($categories as $category) {
  $category->getParentCategory();
}

This is all you need i think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文