Last active
December 31, 2015 21:49
-
-
Save tegansnyder/8049039 to your computer and use it in GitHub Desktop.
Example of importing skus and changing attribute sets.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * changeAttributeSetImport.php | |
| * Allows you to change the attribute set for a list of skus | |
| * | |
| * @author Tegan Snyder <tsnyder@mmm.com> | |
| */ | |
| /* | |
| Preparing your file: | |
| There are two ways to change attribute sets for a list of skus. | |
| Option #1: | |
| Setup a CSV file with the following column headers: | |
| "sku","attribute_set_id" | |
| Option #2: | |
| Setup a CSV file with the following column headers: | |
| "sku","attribute_set_name" | |
| */ | |
| $filename = 'skus.csv'; | |
| require_once('app/Mage.php'); | |
| umask(0); | |
| Mage::app(); | |
| $store_id = 0; | |
| // load CSV to associative array | |
| $csv = array(); | |
| if(($handle = fopen($filename, "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); | |
| } | |
| $cnt = 0; | |
| // store product ids we change attribute sets on here | |
| $product_ids_changed = array(); | |
| foreach ($csv as $row) { | |
| $model = Mage::getModel('catalog/product'); | |
| if (!isset($row['sku'])) { | |
| echo '#' . $cnt . ' sku not found: ' . PHP_EOL; | |
| $cnt = $cnt + 1; | |
| continue; | |
| } | |
| $product_id = $model->getIdBySku($row['sku']); | |
| if (isset($row['attribute_set_id'])) { | |
| $attribute_set_id = $row['attribute_set_id']; | |
| } elseif (isset($row['attribute_set_name'])) { | |
| $attribute_set_name = $row['attribute_set_name']; | |
| $attribute_set_id = Mage::getModel('eav/entity_attribute_set') | |
| ->load($attribute_set_name, 'attribute_set_name') | |
| ->getAttributeSetId(); | |
| } else { | |
| echo '#' . $cnt . ' no identifier found (use attribute_set_id or attribute_set_name): ' . $row['sku'] . PHP_EOL; | |
| $cnt = $cnt + 1; | |
| continue; | |
| } | |
| if (empty($attribute_set_id)) { | |
| echo '#' . $cnt . ' empty: ' . $row['sku'] . PHP_EOL; | |
| $cnt = $cnt + 1; | |
| continue; | |
| } | |
| try { | |
| $product = Mage::getModel('catalog/product')->setStoreId($store_id)->load($product_id); | |
| $product->setAttributeSetId($attribute_set_id)->setStoreId($store_id); | |
| $product->save(); | |
| $product_ids_changed[$cnt] = $product_id; | |
| echo '#' . $cnt . ' success: ' . $row['sku'] . PHP_EOL; | |
| } catch (Exception $e) { | |
| /* | |
| If you get an error like this: | |
| Product with the 'scotch-light-duty-packaging-tape-610-clear-heat-resistant-1-2-in-x-72-yd-70006788395' url_key attribute already exists. | |
| when changing attribute sets take a look here: | |
| app/code/core/Enterprise/Catalog/Model/Product/Attribute/Backend/Urlkey.php | |
| The store id of the the query Magento runs against: | |
| SELECT `catalog_product_entity_url_key`.`entity_id`, | |
| `catalog_product_entity_url_key`.`store_id` | |
| FROM `catalog_product_entity_url_key` | |
| WHERE (value = 'scotch-light-duty-packaging-tape-610-clear-heat-resistant-1-2-in-x-72-yd-70006788395') LIMIT 1 | |
| Returns a store_id row and if the store_id row doesn't match the $store_id variable above you will get that error | |
| $row['store_id'] == $object->getStoreId() on the object | |
| */ | |
| echo '#' . $cnt . ' error: ' . $row['sku'] . PHP_EOL; | |
| echo $e->getMessage() . PHP_EOL; | |
| } | |
| $cnt = $cnt + 1; | |
| } | |
| if ($cnt == 0) { | |
| echo 'No records found in file'; | |
| } else { | |
| Mage::dispatchEvent('catalog_product_massupdate_after', array('products' => $product_ids_changed)); | |
| } |
Author
Thanks Tegan - as per our chat - just needs a slight tweak :-
The comment in line 13 should read :-
// prepare a file called skus.csv with sku, attribute_set
// attribute_set refers to the NAME of the attribute set
Author
@BFG9000 I updated it
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
run this over bash prompt: