2011-10-20 57 views
1

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

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

$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()引发异常。

感谢您的咨询。

回答

0

以下是我为具有相同请求的客户端编写的一种方法:可以混洗结果。

希望它有帮助。

 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; 
+0

谢谢您分享您的解决方案。这看起来像解决我的问题。太糟糕了,这个任务没有现有的方法。 – Thomas

+0

这很有帮助。如果您成为StackOverflow的常规用户,请随时返回此处+1我的答案:) –

+0

我在目录模块中将其作为新块实施。 SQL已成功创建,但foreach循环不起作用。经过一番研究,似乎没有像“load()”这样的东西,但是如果我实现它,我会得到一个错误。我错过了什么? – Thomas

0

以下是使用这些工具的最佳方法。通过这种方式,您不必依赖自定义查询,而是可以使用核心方法:

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