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
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();
}
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))
-
where you all putting this code please share with code and file path Jun 6, 2016 at 12:44
-
How to get all $_product-> variables? Ex: getId, getName, getProductUrl ... Sorry for deleted sub-question!! Jun 6, 2016 at 12:53
-
If you want to get any product attribute like squ then code will be $_product->getSku(); Jun 6, 2016 at 12:55