如何获取产品组的关联产品?
我正在循环访问产品结果,如果该产品是分组产品,我想获取该组中的所有产品。我正在这样做:
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*');
foreach ($products as $product) {
if ($product->getTypeId() == 'grouped'){
// how do I now get associated products of $product?
}
}
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?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在:
你会看到他们使用这个:
由于phtml文件的类型是
Mage_Catalog_Block_Product_View_Type_Grouped
,我们可以转到:并看到
Mage_Catalog_Block_Product_View_Type_Grouped::getAssociatedProducts()
执行此操作:所以我们可以安全地假设
$this->getProduct()
返回一个产品对象,并替换它与您的$product
变量如下:如果我要完全优化您的代码,我会这样写:
In:
You'll see that they use this:
Since that phtml file is of type
Mage_Catalog_Block_Product_View_Type_Grouped
, we can go to:and see that
Mage_Catalog_Block_Product_View_Type_Grouped::getAssociatedProducts()
does this:So we can safely assume that
$this->getProduct()
returns a product object, and replace it with your$product
variable like so:If I was to optimise your code completely, I'd write it like this:
或者,如果您只想获取关联产品的 ID,您可以使用以下方法(速度更快):
Or if you want just to get ids of associated products, you can use the following method (it is much faster):
要按类型获取产品集合:
To get product collection by type: