2

I need to alter the display of products in catalog/product/list.phtml based on whether they have a Custom Design assigned, in Magento 1.8.

I've edited custom_design in Catalog > Attributes > Manage Attributes and set "Use in Product Listing" to "Yes", reindexed, cleared cache.

But when I use:

// in catalog/product/list.phtml

$_productCollection = $this->getLoadedProductCollection();

foreach ($_productCollection as $_product) {
  echo $_product->getCustomDesign();
  //or
  echo $_product->getData('custom_design');
}

I get no output for that attribute.

I've also tried:

// in catalog/product/list.phtml

$_productCollection = $this->getLoadedProductCollection();

$_productCollection->addAttributeToSelect('custom_design')->load();

foreach ($_productCollection as $_product) {
  echo $_product->getCustomDesign();
  //or
  echo $_product->getData('custom_design');
}

But still nothing.

If I use $_product->getCustomDesign(); in catalog/product/view.phtml, I get the output I expect, so I know it's the right attribute & the product has the value.

Anybody know if I'm missing anything? I've gone through the blocks/model stack and made sure that custom_design is being added to the list of attributes to load, as I'd expect, but if I var_dump($_product->getData()), it doesn't appear.

Thanks!

  • Do you use flat tables? Rebuild the flat index then and check if the column custom_design has been added to the table catalog_product_flat_index_[store-id] – simonthesorcerer Nov 12 '15 at 17:05
  • Flat tables is enabled, but catalog_product_flat_1 (there's no catalog_product_flat_index_<id>) doesn't have 'custom_design'. Is there a way to rebuild other than just hitting 'reindex' on the Index Management page? – a_harvie Nov 13 '15 at 2:53
  • yes, i meant this table; added another answer – simonthesorcerer Nov 13 '15 at 13:47
1

You have to load the product if you want to access custom attributes. Something like

$_productCollection = $this->getLoadedProductCollection()
->addAttributeToSelect('custom_design');

foreach ($_productCollection as $_product) {
   $product = Mage::getModel('catalog/product')->load($_product->getId())
   echo $product ->getCustomDesign();
   //or
   echo $product ->getData('custom_design');
}

should work

  • Thanks. I thought Use in Product Listing was enough (this isn't a custom attribute - "Custom Design" is a core attribute), but it does work with loading the product. I don't seem to have to use addAttributeToSelect - it does seem like that's being taken care of by the Use in Product Listing flag. – a_harvie Nov 13 '15 at 2:45
1

You were right that you need to set the attribute to "Used in product listings" => "Yes". After that, you need to rebuild the flat index, and the table catalog_product_flat_[store-id] should have a new column called custom_design. You can rebuild the index in the backend (which tends to run forever or crash on big catalogs), you can use a custom PHP-script, you can use the built-in shell command in MAGENTO_ROOT/shell/indexer.php or a tool like n98-magerun.

The answer by Anja solves the problem by simply calling load() on the product. This is a typical "It's-not-correct-but-it-works"-solution, here's why:

Taken from this very insightful article by Vinai Kopp:

If there is a single product in the collection, this code block will cause 4 queries to be executed. If there are 100 products in the collection, there would be 202 queries being executed (2 for the collection and 2 for each product: 2 + 100*2).

IMO the only position where load() is allowed to be in a loop is an export script where performance doesn't matter. As you stated in your question, you want to use this code in a product list in the frontend for real visitors, and once there are more than a few of them, this will lead to serious performance issues.

  • Thanks, that makes sense, and I did want to avoid the load() in the loop. I've double-checked that the attribute ("Edit Product Attribute "Custom Design") has "Used in product listings" => "Yes", and I just ran the indexer in the shell: php shell/indexer.php reindexall, and the output includes: Product Flat Data index was rebuilt successfully and Product Attributes index was rebuilt successfully. There's no custom_design column though - Pastebin output from SHOW TABLES FROM catalog_product_flat_1 – a_harvie Nov 14 '15 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.