6

在 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]->getProduct()->getId()返回产品 ID。我想要它的类别 ID。

4

3 回答 3

6
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;
}
于 2011-01-09T14:02:02.810 回答
2

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

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

它可以帮助你

于 2013-10-03T05:54:12.673 回答
2
Mage::registry('current_product')->getCategoryId();

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

于 2012-07-03T11:11:11.257 回答