1

In catalog/category/product in

foreach ($_productCollection as $_product)

I want to getMy attribute to do some condition but it doesnt work

<?php

if ($_product->getPromo()) {
    echo 'Ok';
}
?>

instead to do my own collection with the ->AddAttributeToFilter('Promo', 1) to avoid another foreach

  • You need to filter the default category product colletion with your custom attribute "promo", right? – Dhiren Vasoya Dec 30 '16 at 12:56
  • No, i want to add an attribute to collection – PЯINCƏ Dec 30 '16 at 15:15
4

First of all check that are you getting your attribute in product collection data using print_r($_product->getData()).

If it is not there in product data check your attribute is properly created or not.

Else, sometimes it happens that you need to load the product model again with product id in for loop. Like ..

foreach ($_productCollection as $_product) {
  $loadProd = Mage::getModel('catalog/product')->load($_product->getId());
  $promo = $loadProd->getPromo(); //or your attribute name
}
1

Else, sometimes it happens that you need to load the product model again with product id in for loop. Like ..

No. Never ever you need to load the product model again! This is a first good step to max. page load times ...

addAttributeToFilter('Promo', 1) will apply i filter on your collection, but it will not add the attribute to select statement. To add custom attributes, try this

$_productCollection->addAttributeToSelect('promo')
  • How you found this question @sv3n ? I completely forgot it, You are going to browse all my questions lol :) I agree with you it is not a good practice to do a load in a foreach but I have not used this solution. The solution that i used is to set my attribute used in product list to yes and the problem is solved – PЯINCƏ Aug 8 '17 at 20:35
  • @Prince yep :P , because you asked some good questions :) But this answer ... erm no, this should not be the only answer if someone else finds this :) This is badest practice in my opinion. – sv3n Aug 8 '17 at 20:44
  • Yes sure it's not at all the best practice. I dont used it – PЯINCƏ Aug 8 '17 at 20:47
  • You still don"t have Linkedin ? – PЯINCƏ Aug 8 '17 at 22:18
  • Nope :) Maybe tomorrow ;) – sv3n Aug 8 '17 at 22:20

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.