0

I have created a attribute fresh arrival with two values yes or no in dropdown and I am creating a block which is reading from a phtml file. How can I show all products which have fresh arrival set as yes in phtml file.

1

Try this. (Assume your attribute code is fresh_arrival)

$collection = Mage::getModel('catalog/product')->getCollection();

$collection->addAttributeToSelect('*')
           ->addAttributeToFilter(fresh_arrival, 1);
0

Depending on how many products use the fresh arrival attribute, selecting them via SQL may be a better idea:

$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$connection_write = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql = 'SELECT * FROM catalog_product_entity';
$catalog = $connection->fetchAll($sql);
foreach ($catalog as $product){
    $sql = 'SELECT * FROM catalog_product_entity_int WHERE value = ? AND entity_id = ? AND attribute_id = ?';
    $fresh_arrivals = $connection->fetchAll($sql, array(1, $product['entity_id'], attribute_id));
    foreach ($fresh_arrivals as $fresh_arrival){
        /*Make your changes here
         * 
         * i.e
         * 
         * $sql = 'UPDATE table_name SET attribute_value = ? WHERE entity_id = ? AND attribute_id = ?'
         * $connection_write->query($sql, array(attribute_value, $product['entity_id'], attribute_id));
         * 
         * */
    }
}

Note that you can figure out what your attribute ID is by checking the URL when modifying the attribute via Catalog->Attributes->Manage Attributes

  • Even if this might work. you shouldn't do raw queries and you shouldn't call load inside a loop. Big performance killer. – Marius Feb 4 '14 at 7:33
  • Personally I wouldn't go the route of ever loading within the query, just didn't know which changes he was interested in making. Raw SQL across the whole catalog would be a great performance boost over using Magento's orm assuming his catalog is over 4000 products? Or am I mistaken. – Moose Feb 4 '14 at 7:46
  • Sure..direct query is fasted but the fact that you are loading all the fresh arrivals cancels the boost from the direct query. It makes it even worse. – Marius Feb 4 '14 at 7:51
  • Understood. I've edited the answer to indicate how to make these changes via SQL. Ty Marius. – Moose Feb 4 '14 at 7:56
0

try this

$collection = Mage::getModel('catalog/product')->getCollection()
                  ->addAttributeToFilter('fresh_arrival', array('eq'=>1));

foreach($collection as $product){
    //do something
}

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.