Advertisement
Guest User

Untitled

a guest
May 31st, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. $attributeModel = Mage::getModel('eav/entity_attribute')->getCollection()->addFieldToFilter('frontend_label', 'Color');
  2. $attributeCode = $attributeModel->getData('attribute_code') [0]['attribute_code'];
  3.  
  4. $attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);
  5.  
  6. $data = [
  7. 'option' => [
  8. 'value' => [
  9. 0 => [
  10. 0 => 'Red', // Admin
  11. 1 => 'default', // Default store view
  12. // position ???
  13. // is_default ???
  14. ],
  15. 1 => [ 0 => 'Blue', 1 => 'default', 2 => 0 ],
  16. 2 => [ 0 => 'Green', 1 => 'default', 2 => 0 ],
  17. ],
  18. ],
  19. ];
  20.  
  21. $attributeModel->addData($data)->save();
  22.  
  23. print_r($attributeModel->getData());
  24. die;
  25.  
  26. <global>
  27. ....
  28. ....
  29. <resources>
  30. <yourmodule_yournamespace_setup>
  31. <setup>
  32. <module>YourModule_YourNamespace</module>
  33. <class>YourModule_YourNamespace_Model_Resource_Setup</class>
  34. </setup>
  35. </yourmodule_yournamespace_setup>
  36. </resources>
  37. </global>
  38.  
  39. class YourModule_YourNamespace_Model_Resource_Setup extends Mage_Catalog_Model_Resource_Setup
  40. {
  41. /**
  42. * Create Product attributes for select list
  43. *
  44. * @param string $attribute_code
  45. * @param array $optionsArray
  46. */
  47. public function addAttributeOptions($attribute_code, array $optionsArray)
  48. {
  49. $tableOptions = $this->getTable('eav_attribute_option');
  50. $tableOptionValues = $this->getTable('eav_attribute_option_value');
  51. $attributeId = (int) $this->getAttribute('catalog_product', $attribute_code, 'attribute_id');
  52. foreach ($optionsArray as $sortOrder => $label) {
  53. // add option
  54. $data = array(
  55. 'attribute_id' => $attributeId,
  56. 'sort_order' => $sortOrder,
  57. );
  58. $this->getConnection()->insert($tableOptions, $data);
  59.  
  60. // add option label
  61. $optionId = (int) $this->getConnection()->lastInsertId($tableOptions, 'option_id');
  62. $data = array(
  63. 'option_id' => $optionId,
  64. 'store_id' => 0,
  65. 'value' => $label,
  66. );
  67. $this->getConnection()->insert($tableOptionValues, $data);
  68. }
  69. }
  70.  
  71. }
  72.  
  73. $data = array('value1','value2','value3');
  74. $installer->addAttributeOptions('<attribute_code>', $data);
  75.  
  76. <?php
  77. error_reporting(E_ALL);
  78. require_once 'app/Mage.php';
  79. umask(0);
  80. echo "<pre>";
  81. /* not Mage::run(); */
  82. Mage::app();
  83. try
  84. {
  85. $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
  86. $installer->startSetup();
  87.  
  88. $color = array('Red','Green','Blue','Pink','Yellow','orange');
  89. $iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
  90. $aOption = array();
  91. $aOption['attribute_id'] = $installer->getAttributeId($iProductEntityTypeId, 'color');
  92.  
  93. for($iCount=0;$iCount<sizeof($color);$iCount++){
  94. $aOption['value']['option'.$iCount][0] = $color[$iCount];
  95. }
  96. $installer->addAttributeOption($aOption);
  97.  
  98. $installer->endSetup();
  99. ?>
  100.  
  101. function ambassUpdate($ambassName, $attr_id){
  102. $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
  103. $installer->startSetup();
  104.  
  105. $aOption = array();
  106. $aOption['attribute_id'] = $attr_id;
  107.  
  108. for($iCount=1;$iCount<sizeof($ambassName);$iCount++){
  109. //for($iCount=1;$iCount<4;$iCount++){
  110. $aOption['value']['option'.$iCount][0] = $ambassName[$iCount];
  111. $aOption['order']['option'.$iCount] = $iCount + 10;
  112. }
  113.  
  114. $installer->addAttributeOption($aOption);
  115. $installer->endSetup();
  116. return true;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement