Created
October 20, 2016 04:39
-
-
Save trabulium/e89df5cc8cbedb6fc9dd7faade953478 to your computer and use it in GitHub Desktop.
Convert a set of Magento categories to Multi-Select Attributes.
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 | |
/* Mage Australia - 2016-06-20 - | |
Finds Categories that match multiselect | |
attribute and saves the product as that style | |
*/ | |
require_once '../app/Mage.php'; | |
umask(0); | |
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); | |
$attributeOptions = getAttributeOptions('designer'); | |
print_r($attributeOptions); | |
$products = Mage::getModel('catalog/product')->getCollection(); | |
//->addAttributeToFilter('type_id', array('eq' => 'configurable')); | |
foreach ($products as $product){ | |
$product = Mage::getModel('catalog/product')->load($product->getId()); | |
#echo $product->getName(); | |
$cats = $product->getCategoryIds(); | |
$attributeIds = array(); | |
foreach ($cats as $category_id) { | |
$_cat = Mage::getModel('catalog/category')->load($category_id) ; | |
$catName = ucwords(strtolower($_cat->getName())); | |
if(in_array($catName, $attributeOptions)){ | |
$attributeKeys = array_search($catName, $attributeOptions); | |
echo $product->getName() . ' - ' . $catName . ' - ' . $attributeKeys . "\n"; | |
$attributeIds[] = $attributeKeys; | |
} | |
} | |
if(count($attributeIds)){ | |
print_r($attributeIds); | |
//$product->setData('designer', $attributeIds); | |
$product->setDesigner($attributeIds[0]); | |
$product->save(); | |
} | |
} | |
function getAttributeOptions($attribute){ | |
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($attribute)->getFirstItem(); | |
$attributeId = $attributeInfo->getAttributeId(); | |
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId); | |
$attributeOptions = $attribute ->getSource()->getAllOptions(false); | |
foreach($attributeOptions as $key=>$value) { | |
$options[$value['value']] = $value['label']; | |
} | |
return $options; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment