我一直在为我的问题寻找一个没有效果的解决方案。
我想列出简单和可配置的产品和二手产品的可配置。问题在于性能,因为要获得二手产品,我必须使用以下方法:
Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product)只属于一个项目。您可以想象,对于许多产品来说,有大量的SQL查询。如何在选择中添加used_products属性进行查询?
发布于 2015-02-02 17:24:43
getUsedProductCollection()是@b.enoit.be建议的一个很好的起点。
原始代码:
public function getUsedProductCollection($product = null)
{
$collection = Mage::getResourceModel('catalog/product_type_configurable_product_collection')
->setFlag('require_stock_items', true)
->setFlag('product_children', true)
->setProductFilter($this->getProduct($product));
if (!is_null($this->getStoreFilter($product))) {
$collection->addStoreFilter($this->getStoreFilter($product));
}
return $collection;
}你需要的是:
复制和调整以查找用于多个可配置产品的旧产品:
$collection = Mage::getResourceModel('catalog/product_type_configurable_product_collection')
->setFlag('require_stock_items', true)
->setFlag('product_children', true);
$collection->getSelect()->where('link_table.parent_id in ?', $productIds);
$collection->getSelect()->group('e.entity_id');$productIds必须是一个数组,它包含可配置产品的所有ID。它是否也包含简单产品的It并不重要。您可以构建一个JOIN,但是由于您无论如何都需要这些,我建议您先加载原始集合,然后加载使用过的相关产品。替代方案可能是使用UNION和JOIN的大型查询,如果没有显着的性能提高,很难理解这一点。
group('e.entity_id')确保每个产品只被选中一次,以避免由于集合中重复项而导致的异常。
发布于 2015-02-02 15:59:41
在文件app/code/core/Mage/Catalog/Model/Product/Type/Configurable.php中使用的同一个模型中还有另一个函数:
public function getUsedProductCollection($product = null)
{
$collection = Mage::getResourceModel('catalog/product_type_configurable_product_collection')
->setFlag('require_stock_items', true)
->setFlag('product_children', true)
->setProductFilter($this->getProduct($product));
if (!is_null($this->getStoreFilter($product))) {
$collection->addStoreFilter($this->getStoreFilter($product));
}
return $collection;
}因此,您可能想尝试这样做,看看它会给您带来什么:
$collection = Mage::getResourceModel('catalog/product_type_configurable_product_collection')
->setFlag('require_stock_items', true)
->setFlag('product_children', true)
->load();但是,由于Varien_Collection的工作方式,如果您在两个可配置的产品中拥有相同的简单产品,您可能最终会出现这样的错误:
Uncaught exception 'Exception' with message 'Item (Mage_Catalog_Model_Product) with the same id "some_id_here" already exist'发布于 2015-02-02 16:00:47
如果您有性能问题,最好使用直接sql查询,因为它所花费的内存更少,速度更快。像这样的东西;
$coreResource = Mage::getSingleton('core/resource');
$connect = $coreResource->getConnection('core_write');
$prid = 12535;// Product entity id
$result = $connect->query("SELECT product_id FROM catalog_product_super_link WHERE parent_id=$prid");
while ($row = $result->fetch()):
$sprid = $row['product_id'];
// Now sprid contain the simple product id what is associated with that parent
endwhile;https://stackoverflow.com/questions/28281200
复制相似问题