I need to get all products with a specific attribute example: specialpiece = Yes with product name, image, price, special_price in a loop to show this in my home page.


To do so, you will need a collection:

$collection = Mage::getResourceModel('catalog/product_collection')
     ->addAttributeToFilter('specialpiece', 1);

Then you can loop through them to get their info:

foreach ($collection as $product) {
    $name = $product->getName();
    $price = $product->getPrice();
    $specialPrice = $product->getSpecialPrice();
    $image = $product->getImage();

Add following code :

$_productCollection = Mage::getModel('catalog/product')
                        // add sorting to collection optional
                        ->addAttributeToSort('created_at', 'DESC')
foreach ($_productCollection as $_product){
   echo $_product->getId().'</br>';
   echo $_product->getName().'</br>';
   echo $_product->getProductUrl().'</br>';
   echo $_product->getPrice().'</br>';

For specific attribute add this line :

->addAttributeToFilter('specialpiece ', array("eq" =>1))
  • where you all putting this code please share with code and file path
    – Arunendra
    Jun 6 '16 at 12:44
  • How to get all $_product-> variables? Ex: getId, getName, getProductUrl ... Sorry for deleted sub-question!! Jun 6 '16 at 12:53
  • If you want to get any product attribute like squ then code will be $_product->getSku();
    – Arunendra
    Jun 6 '16 at 12:55

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.