Magento 1-如何获得产品组的关联产品? [英] Magento 1 - how do I get associated products of product Group?

查看:42
本文介绍了Magento 1-如何获得产品组的关联产品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历产品结果,如果该产品是分组产品,则希望获得该组中的所有产品.我正在这样做:

I am looping through products results, and if the product is a grouped product, I want to get all products in that group. I'm doing this:

$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/app/design/frontend/base/default/template/catalog/product/view/type/grouped.phtml

您会看到他们使用了此

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

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

Since that phtml file is of type Mage_Catalog_Block_Product_View_Type_Grouped, we can go to:

/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-> getProduct()返回一个产品对象,并用您的 $ product 变量替换它,如下所示:

So we can safely assume that $this->getProduct() returns a product object, and replace it with your $product variable like so:

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

如果我要完全优化您的代码,我会这样写:

If I was to optimise your code completely, I'd write it like this:

<?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
    }

这篇关于Magento 1-如何获得产品组的关联产品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆