我需要找到一种方法来编程找到所有包含特定简单产品的捆绑产品。我希望有一个简单的解决方案这个问题,因为我无法自己弄清楚一个问题。

有帮助吗?

解决方案

以下内容应该使您入门。这不是执行此操作的最性能的方法 - 我们正在加载一系列捆绑的产品,获取儿童ID,然后检查这些ID是否有我们想要的东西。但是,它应该适合外壳脚本需求,一些前端商店的需求,并应通过检查核心方法如何获取数据(即哪种资源模型,导致表格)来以更具性能的方式获取此信息。 。

在Magento CE 1.7.0.1中测试

function findBundledProductsWithThisChildProduct($id_to_find)
{
    //grab all bundled products
    $bundles = array();
    $products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addFieldToFilter('type_id','bundle');

    //loop over bundled products
    foreach($products as $product)
    {

        //get child product IDs
        $children_ids_by_option = $product
        ->getTypeInstance($product)
        ->getChildrenIds($product->getId(),false); //second boolean "true" will return only
                                                   //required child products instead of all          

        //flatten arrays (which are grouped by option)  
        $ids = array();
        foreach($children_ids_by_option as $array)
        {
            $ids = array_merge($ids, $array);
        }

        //perform test and add to return value
        if(in_array($id_to_find, $ids))
        {
            $bundles[] = $product;
        }
    }

    return $bundles;
}

//usage
$products = findBundledProductsWithThisChildProduct($simple_product_id);
foreach($products as $product)
{
    var_dump($product->getData());
}

其他提示

我知道事实对您有用后,这也是如此,但是对将来的搜索者有用。

Janw的基本答案是正确的,但是更熟练的方法是:

$parentIds = Mage::getResourceSingleton('bundle/selection')->getParentIdsByChild($product->getId());

然后,您可以在这些ID上加载产品收集过滤。

我看到了艾伦·风暴的答案。但是,如果我已经有100个捆绑产品怎么办。然后,您需要循环循环所有这些:(

我想出了一种查询此问题的方法:

<?php
function get_bundle_product_by_simple_id($id) {
    $read= Mage::getSingleton('core/resource')->getConnection('core_read');

    // fetch read database connection that is used in Mage_Core module
    $read = Mage::getSingleton('core/resource')->getConnection('core_read');

    $query = sprintf("SELECT `parent_product_id` FROM `catalog_product_bundle_selection` WHERE `product_id` = '%s'",
            htmlspecialchars( stripslashes( $id ) )
        );
    $value=$read->query( $query );
    $row = $value->fetch();

    if( is_null($row) ) {
        return $row;
    }
    return $row['parent_product_id'];
}

我是Magento的新手,所以可能有更好的方法。或根本不这样做的原因。主要是逃脱/剥离 $id

许可以下: CC-BY-SA归因
scroll top