Skip to content

Instantly share code, notes, and snippets.

@tegansnyder
Created August 11, 2014 21:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tegansnyder/2de24f96ab463f074b23 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

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