Magento - 仅加载可配置的产品

发布于 2024-10-21 13:35:00 字数 550 浏览 4 评论 0原文

我有以下代码:

$_productCollection = $this->getLoadedProductCollection();

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

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

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

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

I have the following code:

$_productCollection = $this->getLoadedProductCollection();

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

While it does what it's supposed to do, it greatly slows down page load time. Is it possible to load only configurable products and remove the check for 'configurable'? The store has 12000 products, about 700 are configurable and the rest are child simple products.

I found the following code which returns all configurable products. I need only the products within the current category:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

洒一地阳光 2024-10-28 13:35:00

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())

The problem with getLoadedProductCollection() is it's already loaded - the products' data has already been retrieved from the database. Just using the current category's product collection isn't good enough either, that will ignore the "layers" (attribute filters). The trick is to remove the loaded products from the list first.

// 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) has it's issues too, you're not just outputting the products but also all details of the resource that is the database connection, and cached values, and the products' individual resources, and so on...

In this case I think you would be happier with:

print_r($_productCollection->toArray())
玩套路吗 2024-10-28 13:35:00

所有这些解决方案都不适合我,试试这个:

$_productCollection1 = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id','configurable'); 

foreach ($_productCollection1 as $product1) {
    echo $product1->getName();
    ...
}

它有效,但不知道它是否正确(我是 Magento 的新手)。请告诉我。

All those solutions didn't work for me, try this:

$_productCollection1 = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id','configurable'); 

foreach ($_productCollection1 as $product1) {
    echo $product1->getName();
    ...
}

It works but don't know if it's correct (I'm new to Magento). Let me know please.

静待花开 2024-10-28 13:35:00

您执行此操作的方式需要先加载所有产品,然后再解析和过滤它们。这可能更接近您正在寻找的内容:

$_productCollection = $this ->getLoadedProductCollection()
                            ->addAttributeToFilter('type_id','configurable');

The way you're doing this requires all products to be loaded before you parse through and filter them. This is probably closer to what you're looking for:

$_productCollection = $this ->getLoadedProductCollection()
                            ->addAttributeToFilter('type_id','configurable');
故人爱我别走 2024-10-28 13:35:00

尝试以下

   $collection  =  Mage::getModel('catalog/product')->getCollection();
   $collection->addAttributeToFilter('type_id','configurable');

    foreach($collection as $product)
    {

    }

加载可配置和简单的以及尝试

$collection->addAttributeToFilter('type_id', array('in' => array('configurable','simple')));

Try following

   $collection  =  Mage::getModel('catalog/product')->getCollection();
   $collection->addAttributeToFilter('type_id','configurable');

    foreach($collection as $product)
    {

    }

For loading configurable and simple as well try

$collection->addAttributeToFilter('type_id', array('in' => array('configurable','simple')));
你的背包 2024-10-28 13:35:00

以下是仅获取可配置产品的代码:

 $Config_products  =  Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToFilter('type_id','configurable');

Here is the code for getting only configurable products:

 $Config_products  =  Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToFilter('type_id','configurable');
掩饰不了的爱 2024-10-28 13:35:00

使用 \Magento\Catalog\Model\ResourceModel\Product\Collection $_productcollection,

public function getConfigProducts() {
     $configproducts = $this->_productcollection;
     $configproducts->addAttributeToSelect('*');
     $configproducts->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
     $configproducts->addAttributeToFilter('show_in_price_page', array('eq' => 1));
     $configproducts->addAttributeToFilter('type_id', array('eq' => "configurable"));
     return $configproducts;
}

Use \Magento\Catalog\Model\ResourceModel\Product\Collection $_productcollection,

public function getConfigProducts() {
     $configproducts = $this->_productcollection;
     $configproducts->addAttributeToSelect('*');
     $configproducts->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
     $configproducts->addAttributeToFilter('show_in_price_page', array('eq' => 1));
     $configproducts->addAttributeToFilter('type_id', array('eq' => "configurable"));
     return $configproducts;
}
奈何桥上唱咆哮 2024-10-28 13:35:00

如果您将简单产品的可见性更改为“单独不可见”,Magento 将不会加载它以显示在产品列表页面中。

If you change Visibility of simple product to "Not Visible Individually", Magento will not load it to display in product list page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文