1

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.

2 Answers 2

2

To do so, you will need a collection:

$collection = Mage::getResourceModel('catalog/product_collection')
     ->addAttributeToSelect(array('name','image','price','special_price'))
     ->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();
}
1

Add following code :

<?php
$_productCollection = Mage::getModel('catalog/product')
                        ->getCollection()
                        // add sorting to collection optional
                        ->addAttributeToSort('created_at', 'DESC')
                        ->addAttributeToSelect('*')
                        ->load();
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))
3
  • where you all putting this code please share with code and file path
    – Arunendra
    Commented Jun 6, 2016 at 12:44
  • How to get all $_product-> variables? Ex: getId, getName, getProductUrl ... Sorry for deleted sub-question!! Commented Jun 6, 2016 at 12:53
  • If you want to get any product attribute like squ then code will be $_product->getSku();
    – Arunendra
    Commented Jun 6, 2016 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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