简体   繁体   English

Magento 1 - 从产品 ID 中获取类别 ID

[英]Magento 1 - get category ID from product ID

In magento how to get the category id of each product from its product ID.在 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;
              }
        }
    }

Here $items[$i]->getProduct()->getId() returns product ID.这里$items[$i]->getProduct()->getId()返回产品 ID。 I want its category ID.我想要它的类别 ID。

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;
}
Mage::registry('current_product')->getCategoryId();

这样,就可以获取当前产品的类别 id。

suppose if you want all category ids from current product id you can get from假设如果您想要当前产品 ID 中的所有类别 ID,您可以从

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

it may help you它可能会帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM