【问题标题】:Magento product's categoryMagento 产品类别
【发布时间】:2011-01-04 05:56:56
【问题描述】:

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

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

Bl > 头发护理 > 造型产品

Bl > Natural & Organic > 护发 > 造型产品

Bl > 我们的品牌 > Pureology > Stylers

谢谢! 里查

【问题讨论】:

    标签: mysql magento


    【解决方案1】:

    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,并为报告加载它们的名称。

    【讨论】:

    • 谢谢!但是通过应用此方法,如果任何产品具有 bl > skin > face 等类别,它会显示两个条目 1) bl> skin 和 2)bl > skin > face,您知道如何处理或解决此问题吗?
    • 如果产品属于这两个类别,则该数据结果是正确的。您能否描述一下您希望在这种情况下看到什么?
    • 它应该只显示bl > skin > face b'cz face是最终的子类别(皮肤),属于哪个产品。 (为什么还显示类别?)
    【解决方案2】:

    首先加载产品模型

    根据 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();
    }
    

    我想这就是你所需要的。

    【讨论】:

    • +1 表示努力和好方法,但我必须知道所有相关表才能获取任何类别,因为我必须通过 mysql 直接访问 magento 数据库。
    • 我不建议这样做。但我祝你好运!如果您成功发布解决方案,其他人可能会很感兴趣。当然,还有另一种方法可以通过访问可能会更快和/或更好地工作的 magento 资源模型来实现。但我现在没有时间弄清楚。
    猜你喜欢
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多