Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Product Search fixer for when importing products doesn't reindex the search index for them... see http://magento.stackexchange.com/questions/23648/programmatically-trigger-admin-save-action-on-product
<?php
/**
* product-search-fixer.php
* Forces a product to mimic the admin save in the Magento admin
* effectively rebuilding the catalog search index for that product
*
* @category Mmm
* @package ProductSearchFixer
* @author Tegan Snyder
*
*/
require 'app/Mage.php';
Mage::app();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$csv = array();
if( ($handle = fopen('skus.csv', "r")) !== FALSE) {
$rowCounter = 0;
while (($rowData = fgetcsv($handle, 0, ",")) !== FALSE) {
if( 0 === $rowCounter) {
$headerRecord = $rowData;
} else {
foreach( $rowData as $key => $value) {
$csv[ $rowCounter - 1][ $headerRecord[ $key] ] = $value;
}
}
$rowCounter++;
}
fclose($handle);
}
foreach ($csv as $c) {
$sku = $c['sku'];
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
echo 'Product: ' . $product->getId() . ' - ' . $product->getName() . PHP_EOL;
if (Mage::app()->isSingleStoreMode()) {
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
}
$product->save();
}
@blancovats

This comment has been minimized.

Copy link

blancovats commented Jun 5, 2015

hi there am i correct in assuming i have to have a csv file called skus.csv with a list of sku's i want this action to be run on?

@treestonemedia

This comment has been minimized.

Copy link

treestonemedia commented Oct 14, 2015

Hi,

I got this error
Call to a member function getId() on a non-object on line 34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.