Magento category ID from product ID

徘徊边缘 提交于 2019-12-03 18:04:15

问题


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


回答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;
}



回答2:


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

this way, category id of a current product can be get.




回答3:


suppose if you want all category ids from current product id you can get from

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

it may help you



来源:https://stackoverflow.com/questions/4413565/magento-category-id-from-product-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!