代码之家  ›  专栏  ›  技术社区  ›  Thomas Müller

如何找出简单产品的主产品?

  •  4
  • Thomas Müller  · 技术社区  · 16 年前

    如果一个简单的产品是一个可配置产品的一部分,那么我怎样才能知道它是主产品呢?我需要这个作为产品清单。

    $_product->loadParentProductIds();
    $parentIds = $_product->getParentProductIds();
    
    3 回复  |  直到 13 年前
        1
  •  5
  •   Knowledge Craving    16 年前

    假设您拥有简单产品的产品ID。

    要获取此简单产品的所有父级可配置产品ID,请使用以下命令code:-

    <?php
    $_product = Mage::getModel('catalog/product')->load(YOUR_SIMPLE_PRODUCT_ID);
    $parentIdArray = $_product->loadParentProductIds()
                     ->getData('parent_product_ids');
    if(!empty($parentIdArray)) {
        // currently in the master configurable product
        print_r($parentIdArray); // this prints all the parent product IDs using your simple product.
    }
    ?>
    

    我想这会让你走的。

        2
  •  4
  •   bstpierre Edgar Aviles    15 年前

    $configurable_product_model = Mage::getModel(‘catalog/product_type_configurable’);
    $parentIdArray = $configurable_product_model->getParentIdsByChild($simple_product_id);
    
        3
  •  4
  •   BenMorel Manish Pradhan    12 年前

    版本1.4.2.0之后 loadParentProductIds() 和 getParentProductIds() 方法已弃用。别问我为什么。我个人有点喜欢这些方法。所以我把它们重新引入我当地的法师课程。这就是为什么:

    app/code/core/Mage/Catalog/Model/Product.php
    

    到

    app/code/local/Mage/Catalog/Model/Product.php
    

    然后改变主意 方法,在第1349行附近找到:

    public function loadParentProductIds()
    {
            return $this->_getResource()->getParentProductIds($this);
    }
    

    getParentProductId() 方法。

    所以复制:

    app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product.php
    

    到

    app/code/local/Mage/Catalog/Model/Resource/Eav/Mysql4/Product.php
    

    查找不推荐的 方法。应该在第535行附近。用1.4.2.0之前的代码覆盖它:

    public function getParentProductIds($object){
        $childId = $object->getId();
        $groupedProductsTable = $this->getTable('catalog/product_link');
        $groupedLinkTypeId = Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED;
        $configurableProductsTable = $this->getTable('catalog/product_super_link');
    
        $groupedSelect = $this->_getReadAdapter()->select()
            ->from(array('g'=>$groupedProductsTable), 'g.product_id')
            ->where("g.linked_product_id = ?", $childId)
            ->where("link_type_id = ?", $groupedLinkTypeId);
    
        $groupedIds = $this->_getReadAdapter()->fetchCol($groupedSelect);
    
        $configurableSelect = $this->_getReadAdapter()->select()
            ->from(array('c'=>$configurableProductsTable), 'c.parent_id')
            ->where("c.product_id = ?", $childId);
    
        $configurableIds = $this->_getReadAdapter()->fetchCol($configurableSelect);
        return array_merge($groupedIds, $configurableIds);
    }
    

    现在您可以再次执行以下操作:

    $_product->loadParentProductIds()->getData('parent_product_ids');
    

    希望这对你有帮助!

    推荐文章