【问题标题】:Magento 1 - get category ID from product IDMagento 1 - 从产品 ID 获取类别 ID
【发布时间】:2011-05-23 17:59:49
【问题描述】:

在 magento 中如何从产品 ID 中获取每个产品的类别 ID。

   $items    = $request->getAllItems();
    $c           = count($items); 

    for ($i = 0; $i < $c; $i++) {
        if ($items[$i]->getProduct() instanceof Mage_Catalog_Model_Product) {

            if ($items[$i]->getProduct()->getId()) {
               $this->_dhlAllowed    = false;
              }
        }
    }

此处$items[$i]-&gt;getProduct()-&gt;getId() 返回产品 ID。我想要它的类别 ID。

【问题讨论】:

  • $items[$i]->getProduct()->getCategoryIds() ;这将返回一个服务器中的类别 ID,但不返回另一台服务器中的类别 ID。有什么想法吗?
  • 您是否尝试过重新索引服务器上的平面类别表?这种奇怪的事情通常与过时(或损坏)的索引有关。

标签: magento entity-attribute-value magento-1.4


【解决方案1】:
public function getProductCategory() {
    /* @var $product Mage_Catalog_Model_Product */
    $product = Mage::registry('current_product');
    if ($product->getId()) {
        $categoryIds = $product->getCategoryIds();
        if (is_array($categoryIds) and count($categoryIds) >= 1) {
            return Mage::getModel('catalog/category')->load($categoryIds[0]);
        };
    }
    return false;
}

【讨论】:

  • count($categoryIds) 应该是&gt;= 1
【解决方案2】:

假设如果您想要当前产品 ID 中的所有类别 ID,您可以从

Mage::registry('current_product')->getCategoryIds();

也许对你有帮助

【讨论】:

    【解决方案3】:
    Mage::registry('current_product')->getCategoryId();
    

    这样就可以得到当前产品的分类id。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      相关资源
      最近更新 更多