【问题标题】:Magento - get bundled products where a simple product belongs toMagento - 获取简单产品所属的捆绑产品
【发布时间】:2011-10-20 17:38:31
【问题描述】:

我想在一个简单的产品页面上显示所有捆绑包,因此需要检索信息。我搜索并尝试了很多。这篇文章听起来很有希望,但要么不起作用,要么可能不适合我的问题: Magento - get a list of bundled product ids from a product id

我找到了分组产品的解决方案,但这不能在这里应用。

$grouped_product_model = Mage::getModel('bundle/product_selection');
$groupedParentId = $grouped_product_model->getParentIdsByChild($product->getId()); 

我发现表 catalog_product_bundle_selection 是搜索的正确位置,但我想知道是否有一种干净的方法和现有功能可以通过 product_id 搜索此表,而不仅仅是破解它。

我在 Mage_Bundle 中没有找到解决方案。

我错过了什么?

从 vrnet 得到急救后,我写了一个新的块类,所以我可以更新布局

class Thomaier_Catalog_Block_Product_View_BundledSelect extends Mage_Catalog_Block_Product_View 
{

    protected $_simpleProducts = array( '3' ); // just an example

    public function getBundles() {

        $bundleIds = array();

        $bundlesCollectionModel = Mage::getResourceModel('bundle/selection_collection');
        $bundlesCollection = $bundlesCollectionModel->getSelect()
             ->where('`selection`.`product_id` in (' . join(',', (array)$this->_simpleProducts) . ')');

        foreach ($bundlesCollection as $bundleItem) {
            $bundleIds[] = $bundleItem->getParentProductId();
        }

        ...
    }
}

我跳过了一些部分。正如我在评论中提到的,当我在 phpmyadmin 中尝试 SQL 查询时,它运行良好,但是 $bundleItem 没有创建并且 ->load() 抛出异常。

感谢您的建议。

【问题讨论】:

    标签: magento magento-1.4


    【解决方案1】:

    下面是我为具有相同请求的客户编写的一个方法,但有一个额外的:能够随机播放结果。

    希望对你有帮助。

         protected $_simpleProducts = array(); // Array with IDs of simple products you want bundles from.
    
         protected $_shuffle = false;
    
        public function getBundles() {
            $bundleIds = array();
    
            /*Rather than using a collection model 
    and make operations with getSelect,
    a more elegant way is to extend
    Mage_Bundle_Model_Mysql4_Selection_Collection
    with a method that would be something like
    setProductIdsFilter($productIds)*/
    
            $bundlesCollectionModel = Mage::getResourceModel('bundle/selection_collection');
            $bundlesCollection = $bundleCollectionModel->getSelect()
                                             ->where('`selection`.`product_id` in (' . join(',', (array)$this->_simpleProducts) . ')');
            foreach ($bundlesCollection as $bundleItem) {
                $bundleIds[] = $bundleItem->getParentProductId();
            }
            if (count($bundleIds)) {
                $allowBundles = Mage::getResourceModel('catalog/product_collection')
                                ->addIdFilter($bundleIds)
                                ->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
                if ($this->_shuffle) {
                    $allowBundles->getSelect()->order('rand()');
                }
                if ($allowBundles->count()) {
                    return $allowBundles;
                }
            }
            return;
    

    【讨论】:

    • 感谢您分享您的解决方案。这看起来像解决我的问题。太糟糕了,这个任务没有现成的方法。
    • 很高兴这对您有所帮助。如果您成为 StackOverflow 的普通用户,请随时回到这里并为我的回答 +1 :)
    • 我将它作为一个新块实现到目录模块中。 SQL 已成功创建,但 foreach 循环不起作用。经过一些研究,似乎缺少像“load()”这样的东西,但是如果我实现它,我会收到一个错误。我错过了什么?
    • 如果你在一个集合上循环,load() 方法不是强制性的。 load() 方法将由“foreach”上的底层 Varien 方法自动触发。你能用你的块代码更新你原来的问题吗?
    【解决方案2】:

    以下是使用这些的最佳方法。这样您就不必依赖自定义查询,而是可以使用核心方法:

    $bundlesCollection = Mage::getResourceModel('bundle/selection')
                                 ->getParentIdsByChild($simple_product_ids_array_or_int);
            foreach ($bundlesCollection as $bundleProdId) {
                  //do anything you want with the bundleProdId array elements
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 2013-06-16
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多