0

In Catalog/product/list i have some collection like this :

$_productCollection = $this->getLoadedProductCollection();

and foreach ($_productCollection as $_product) I want to do some condition in products with some attribute that i have added in admin in $_product->getData(); i dont find my attribute

if i do my collection i find it:

<?php $collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('name')
    ->addAttributeToFilter('status', 1)
    ->addAttributeToFilter('promo', 1)
    ->setPageSize(10);
?>
<?php foreach($collection as $col): ?>
    <?php echo $col->getSku(); ?><br/> // 123
    <?php echo $col->getPromo(); ?> //1
<?php endforeach; ?>
  • make sure you have assigned that attribute to respective attribute set – Piyush Dec 30 '16 at 15:41
  • that is to say ? i have created it in admin panel catalog/manage products i clicked in any product then in general add a new attribute – PЯINCƏ Dec 30 '16 at 15:46
1

The Solution is :

1) The bad one:

foreach ($_productCollection as $_product) {
  $loadProd = Mage::getModel('catalog/product')->load($_product->getId());
  $customAttribute = $loadProd->getPromo();
}

2) The good one:

  1. We should set the attribute to Use in Product Listing to yes in admin.
  2. After adding the attribute, we should reindexAll.
  3. Clear the browser and Magento cache.

  4. Now we will find our promo attribute in the collection.

  • This may work, but it is very inefficient. You should use Amit's suggestion. – thedash Nov 28 '17 at 15:49
  • 1
    Yes i know, look my update @thedash – PЯINCƏ Nov 28 '17 at 16:16
0

In this case,you can need some code.

  • First,enable attribute ,promo for Use in Product Listing From Admin -> Catalog -> Attributes -> Manage Attributes select attribute and

And add ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) to collection

  • Where i add the ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) ? – PЯINCƏ Dec 30 '16 at 17:16
  • in place of ` ->addAttributeToFilter('promo', 1)` – Amit Bera Dec 30 '16 at 17:27
  • you have't understand my question, i wont to do somthing in $_productCollection = $this->getLoadedProductCollection(); to have my custom attribute in this collection or in the getData() – PЯINCƏ Dec 30 '16 at 17:33

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.