1

I have a magento installation that has a layered navigation to which attributes show and allow you to filter by.

I have an issue with the attributes showing on the filter, they wont show unless I go into each product and re-save them after I have imported the attribute data via CSV.

I have re-indexed the data and flushed all cache but still cant get them to show.

I need to find a way to force all products to re-save programatically.

Does anyone know of a script I can run via SSH to do this at all? I have tried a few I have found online, but they dont seem to execute correctly.

Any help would be greatly appreciated.

Thanks in advance,

user2045
  • 831
  • 3
  • 16
  • 29
Andy Simpson
  • 147
  • 1
  • 3
  • 6
  • have you tried reindex from console/terminal? $~ php shell/indexer.php -reindexall is command for that on unix systems with ability to run php scripts from terminal – pankijs Mar 28 '15 at 01:00

3 Answers3

1

Untested but something like this:

<?php
require 'app/Mage.php';


Mage::app();

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

foreach ($collection as $product) {
    echo 'saving product '.$product->getData('sku').PHP_EOL;
    $product->save();
    echo 'saved'.PHP_EOL;
}
Andrew Kett
  • 3,488
  • 1
  • 20
  • 33
  • how many products do you have? – Andrew Kett Feb 16 '15 at 11:22
  • 750 products. I have just managed to sort this though. I have updated products via the Admin panel. Batch updated attributes, changed all to Taxable Goods and updated. Now working, thanks for your help though, Andrew. – Andy Simpson Feb 16 '15 at 11:51
1

This is what resource iterators are made for:

$productCollection = Mage::getModel('catalog/product')->getCollection();
Mage::getSingleton('core/resource_iterator')->walk($productCollection->getSelect(), array(array($this, 'productCallback')));

// callback method
public function productCallback($args)
{
    $product = Mage::getModel('catalog/product'); 
    $product->getResource()->saveAttribute($product, 'your_changed_attribute1');
    $product->getResource()->saveAttribute($product, 'your_changed_attribute2');
}

Note: Since you might only have an issue with some of your attributes, there's no need to do call the 'full' ->save() functions on your products and this is a quite heavy operation. Just select the attributes which you need to save as shown above.

Credits & links for you to look at:

Best Way To Iterate Through Product Collection

Is it possible to iterate over Magento collections with pagination natively?

Anna Völkl
  • 17,341
  • 5
  • 60
  • 141
  • I like this–it gives you the ORM for building your query and then a very high-speed way to process and update the data – STW Oct 22 '15 at 04:42
0

You can use this :

<?php
require 'app/Mage.php';

Mage::app();

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->setPageSize(100);
for ($i = 0; $i < $products->getLastPageNumber(); $i++) {
    $products
        ->setCurPage($i)
        ->save();
}
Aurélien
  • 322
  • 2
  • 7