【问题标题】:How do I get associated products of product Group?如何获取产品组的关联产品?
【发布时间】:2012-01-24 16:06:49
【问题描述】:

我正在遍历产品结果,如果产品是分组产品,我想获取该组中的所有产品。我正在这样做:

$products = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->addAttributeToSelect('*');
foreach ($products as $product) {
    if ($product->getTypeId() == 'grouped'){
        // how do I now get associated products of $product?
    }
}

【问题讨论】:

    标签: magento


    【解决方案1】:

    在:

    /magento/app/design/frontend/base/default/template/catalog/product/view/type/grouped.phtml
    

    你会看到他们使用这个:

    <?php 
        $_associatedProducts = $this->getAssociatedProducts();
    

    由于该 phtml 文件的类型为Mage_Catalog_Block_Product_View_Type_Grouped,我们可以转到:

    /magento/app/code/core/Mage/Catalog/Block/Product/View/Type/Grouped.php
    

    看到Mage_Catalog_Block_Product_View_Type_Grouped::getAssociatedProducts() 这样做了:

    <?php
        $this->getProduct()->getTypeInstance()->getAssociatedProducts($this->getProduct());
    

    所以我们可以放心地假设 $this-&gt;getProduct() 返回一个产品对象,并将其替换为您的 $product 变量,如下所示:

    <?php
        if ($product->getTypeId() == 'grouped'){
            // how do I now get associated products of $product?
            $associatedProducts = $product->getTypeInstance()->getAssociatedProducts($product);
        }
    

    如果我要彻底优化你的代码,我会这样写:

    <?php
        $products = Mage::getModel('catalog/product')
            ->getCollection()
            ->addAttributeToFilter('type_id', array('eq' => 'grouped'));
        foreach ($products as $product) {
            $associatedProducts = $product->getTypeInstance()->getAssociatedProducts($product);
            // Do something with $associatedProducts
        }
    

    【讨论】:

    • 谢谢,我可能会让这个工作......我想迭代所有产品,如果它被分组迭代它的关联产品 - 我仍然想要所有单个产品,而不仅仅是分组的关联产品产品。您的“优化”代码不会包含常规的单一产品,对吗?
    • 正确,它只会得到一组产品的集合。您要使用的是 $product->isGrouped()。因此,对于所有产品的集合,请使用: foreach ($products as $product) { if ($product->isGrouped()) { /* 获取相关产品集合 */ } }
    【解决方案2】:

    或者,如果您只想获取关联产品的 id,您可以使用以下方法(速度更快):

    public function getAssociatedProductIds($groupedProductId)
    {
        $coreResource = Mage::getSingleton('core/resource');
        $conn = $coreResource->getConnection('core_read');
        $select = $conn->select()
            ->from($coreResource->getTableName('catalog/product_relation'), array('child_id'))
            ->where('parent_id = ?', $groupedProductId);
    
        return $conn->fetchCol($select);
    }
    

    【讨论】:

      【解决方案3】:

      按类型获取产品集合:

      $product = Mage::getModel('catalog/product')
         ->getCollection()
         ->addAttributeToFilter('type_id', array('eq' => 'grouped'))
         ->load();
      
      $parentProductId = array();
      
      foreach($product as $simpleProducts) {
        $simpleProducts->loadParentProductIds();
        array_push($parentProductId,$simpleProducts->getParentProductIds();
      }
      

      【讨论】:

      • 致命错误:调用 /var/www/vhosts/mysite.com/httpdocs/app/code/core/Mage/Catalog/Model/Product.php 中未定义的方法 Mage_Catalog_Model_Resource_Eav_Mysql4_Product::getParentProductIds()在线 1355
      • 如果您按分组过滤,则 $product 集合不包含 simpleproducts
      猜你喜欢
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-03
      相关资源
      最近更新 更多