php - Magento - 仅加载可配置产品

标签 php magento

我有以下代码:

$_productCollection = $this->getLoadedProductCollection();

foreach ($_productCollection as $_product)
{
  if ($_product->_data['type_id'] == 'configurable')
  {
    ...
  } 
}

虽然它做了它应该做的事情,但它大大减慢了页面加载时间。是否可以仅加载可配置产品并删除“可配置”检查?该商店有 12000 种产品,其中约 700 种是可配置的,其余为 child 简单产品。

我发现以下代码返回所有可配置的产品。我只需要当前类别中的产品:

$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')
                ->addAttributeToFilter('type_id', array('eq' => 'configurable'));

最佳答案

getLoadedProductCollection() 的问题在于它已经加载 - 产品数据已从数据库中检索。仅使用当前类别的产品集合也不够好,这将忽略“层”(属性过滤器)。诀窍是先从列表中删除加载的产品。

// First make a copy, otherwise the rest of the page might be affected!
$_productCollection = clone $this->getLoadedProductCollection();
// Unset the current products and filter before loading the next.
$_productCollection->clear()
                   ->addAttributeToFilter('type_id', 'configurable')
                   ->load();

print_r($_productCollection) 也有问题,您不仅输出产品,还输出数据库连接、缓存值和产品个人资源的所有详细信息资源等等……

在这种情况下,我认为您会更高兴:

print_r($_productCollection->toArray())

关于php - Magento - 仅加载可配置产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5280392/

相关文章:

php - Codeigniter:不使用 $_GET 实现搜索过滤器

php - fatal error : Call to a member function getData() on a non-object in

Magento - 模块约定 mysql4 或资源

api - 使用 magento API V2 限制结果数量

magento - 检查前端是否管理员登录

php - 如何从 eBay API 获取自定义标签字段?

PHP curl 错误 :error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure

php - 回显时间与 DB : all minutes are the same 中保存的时间不同

将数据返回到数据库 MYSQL 的 PHP 页面

php - Magento - 在 Magento 扩展中加载模型时出错?