如何获取产品组的关联产品?

发布于 2024-12-22 10:13:45 字数 365 浏览 1 评论 0原文

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

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只涨不跌 2024-12-29 10:13:45

在:

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

In:

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

You'll see that they use this:

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

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

and see that Mage_Catalog_Block_Product_View_Type_Grouped::getAssociatedProducts() does this:

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

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
    }
三生一梦 2024-12-29 10:13:45

或者,如果您只想获取关联产品的 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);
}

Or if you want just to get ids of associated products, you can use the following method (it is much faster):

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);
}
如歌彻婉言 2024-12-29 10:13:45

要按类型获取产品集合:

$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();
}

To get product collection by type:

$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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文