Magento获取商品所在的所有分类

$product = Mage::getModel('catalog/product')->load($product_id);

$category_ids = $product->getCategoryIds();

$product->getCategoryIds();

  • 先判断数据里面是否有category_ids这个属性;
  • 如果不存在的话在看属性的锁机制,如果属性被锁则打开锁
  • 然后通过资源模型查询数据表
  • 如果锁打开了,最后在关上锁
/**
     * Retrieve assigned category Ids
     *
     * @return array
     */
public function getCategoryIds()
{
    if (! $this->hasData('category_ids')) {
        $wasLocked = false;
        if ($this->isLockedAttribute('category_ids')) {
            $wasLocked = true;
            $this->unlockAttribute('category_ids');
        }
        $ids = $this->_getResource()->getCategoryIds($this);
        $this->setData('category_ids', $ids);
        if ($wasLocked) {
            $this->lockAttribute('category_ids');
        }
    }

    return (array) $this->_getData('category_ids');
}

$this->isLockedAttribute();

/**
     * Retrieve locked attributes
     *
     * @return boolean
     */
public function isLockedAttribute($attributeCode)
{
    return isset($this->_lockedAttributes[$attributeCode]);
}

$this->unlockAttribute();

public function unlockAttribute($attributeCode)
{
    if ($this->isLockedAttribute($attributeCode)) {
        unset($this->_lockedAttributes[$attributeCode]);
    }

    return $this;
}

$ids = t h i s − > g e t R e s o u r c e ( ) − > g e t C a t e g o r y I d s ( this->_getResource()->getCategoryIds( this>getResource()>getCategoryIds(this);

/**
     * Retrieve product category identifiers
     *
     * @param Mage_Catalog_Model_Product $product
     * @return array
     */
public function getCategoryIds($product)
{
    $adapter = $this->_getReadAdapter();

    $select = $adapter->select()
        ->from($this->_productCategoryTable, 'category_id')
        ->where('product_id = ?', (int)$product->getId());

    return $adapter->fetchCol($select);
}

$this->lockAttribute();

/**
     * Lock attribute
     *
     * @param string $attributeCode
     * @return Mage_Catalog_Model_Abstract
     */
public function lockAttribute($attributeCode)
{
    $this->_lockedAttributes[$attributeCode] = true;
    return $this;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值