Skip to content

Instantly share code, notes, and snippets.

@ameliaikeda
Last active December 14, 2015 20:19
Show Gist options
  • Save ameliaikeda/5143426 to your computer and use it in GitHub Desktop.
Save ameliaikeda/5143426 to your computer and use it in GitHub Desktop.
<?php
/*
** mage_update:
** Relies on Magento's Varien Object __call() magic method to save to database attribute fields.
** Lack of error-handling™ because this is a batch script and Magento is in Developer Mode.
** Note that this is called in one hell of a loop (~3.4 million iterations), so keep it relatively speedy.
**
** lower_case_with_underscores becomes CamelCase
** UPPERCASE becomes Capitalized, yadda yadda.
**
** @param (string) $new: new SKU Code Input
** @param (int) $id: Product ID (entity_id)
** @return void
*/
function mage_update($new, $id) {
$model = Mage::Model('catalog/product')->load($id);
$path = $model->getImage();
$path_small = $model->getSmallImage();
$thumbnail = $model->getThumbnail();
$old = $model->getSku();
$ext = end(explode(".", $path));
if ($path == $ext) throw new Exception("Invalid File (Renaming): $path ($new)"); // TODO: check all paths
/* Warning: Make *sure* the Magento cache is enabled while renaming when running this on live.
* 'round these parts; we don't need no stinkin' maintenance time. */
$model->setImage("/ws/$sku.$ext"); // hardcoded for now. TODO: fix
$model->setSmallImage("/ws/{$sku}_small.$ext");
$model->setThumbnail("/ws/thumb_$sku.$ext");
$model->setSku($new)->setOldSku($old);
$dir = "media/catalog/product"; // hardcoding, ho! TODO: fix
// TODO: check file_exists().
// TODO: Fix naive assumption that all files are different here (they're not always)
rename($dir.$path, "$dir/$sku.$ext");
rename($dir.$path_small, "$dir/{$sku}_small.$ext");
rename($dir.$thumbnail, "$dir/thumb_$sku.$ext");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment