Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.05 KB | None | 0 0
  1. <?php
  2. define('MAGENTO', realpath(dirname(__FILE__)));
  3. require_once MAGENTO . '/app/Mage.php';
  4. Mage::app();
  5. $entity_type_id = Mage::getModel('catalog/product')->getResource()->getTypeId();
  6.  
  7. prepareCollection($entity_type_id);
  8.  
  9. function prepareCollection($ent_type_id){
  10. $resource = Mage::getSingleton('core/resource');
  11. $connection = $resource->getConnection('core_read');
  12. $select_attribs = $connection->select()
  13. ->from(array('ea'=>$resource->getTableName('eav/attribute')))
  14. ->join(array('c_ea'=>$resource->getTableName('catalog/eav_attribute')), 'ea.attribute_id = c_ea.attribute_id');
  15. // ->join(array('e_ao'=>$resource->getTableName('eav/attribute_option'), array('option_id')), 'c_ea.attribute_id = e_ao.attribute_id')
  16. // ->join(array('e_aov'=>$resource->getTableName('eav/attribute_option_value'), array('value')), 'e_ao.option_id = e_aov.option_id and store_id = 0')
  17. $select_prod_attribs = $select_attribs->where('ea.entity_type_id = '.$ent_type_id)
  18. ->order('ea.attribute_id ASC');
  19.  
  20. $product_attributes = $connection->fetchAll($select_prod_attribs);
  21.  
  22. $select_attrib_option = $select_attribs
  23. ->join(array('e_ao'=>$resource->getTableName('eav/attribute_option'), array('option_id')), 'c_ea.attribute_id = e_ao.attribute_id')
  24. ->join(array('e_aov'=>$resource->getTableName('eav/attribute_option_value'), array('value')), 'e_ao.option_id = e_aov.option_id and store_id = 0')
  25. ->order('e_ao.attribute_id ASC');
  26.  
  27. $product_attribute_options = $connection->fetchAll($select_attrib_option);
  28.  
  29. $attributesCollection = mergeCollections($product_attributes, $product_attribute_options);
  30. prepareCsv($attributesCollection);
  31.  
  32. }
  33.  
  34. function mergeCollections($product_attributes, $product_attribute_options){
  35.  
  36. foreach($product_attributes as $key => $_prodAttrib){
  37. $values = array();
  38. $attribId = $_prodAttrib['attribute_id'];
  39. foreach($product_attribute_options as $pao){
  40. if($pao['attribute_id'] == $attribId){
  41. $values[] = $pao['value'];
  42. }
  43. }
  44. if(count($values) > 0){
  45. $values = implode(";", $values);
  46. $product_attributes[$key]['_options'] = $values;
  47. }
  48. else{
  49. $product_attributes[$key]['_options'] = "";
  50. }
  51. /*
  52. temp
  53. */
  54. $product_attributes[$key]['attribute_code'] = $product_attributes[$key]['attribute_code'];
  55. }
  56.  
  57. return $product_attributes;
  58.  
  59. }
  60.  
  61. function prepareCsv($attributesCollection, $filename = "importAttrib.csv", $delimiter = '|', $enclosure = '"'){
  62.  
  63. $f = fopen('php://memory', 'w');
  64. $first = true;
  65. foreach ($attributesCollection as $line) {
  66. if($first){
  67. $titles = array();
  68. foreach($line as $field => $val){
  69. $titles[] = $field;
  70. }
  71. fputcsv($f, $titles, $delimiter, $enclosure);
  72. $first = false;
  73. }
  74. fputcsv($f, $line, $delimiter, $enclosure);
  75. }
  76. fseek($f, 0);
  77. header('Content-Type: application/csv');
  78. header('Content-Disposition: attachement; filename="'.$filename.'"');
  79. fpassthru($f);
  80. }
  81.  
  82. <?php
  83. define('MAGENTO', realpath(dirname(__FILE__)));
  84. require_once MAGENTO . '/../app/Mage.php';
  85. Mage::app();
  86. // $fileName = MAGENTO . '/var/import/importAttrib.csv';
  87. $fileName = 'importAttrib.csv';
  88. // getCsv($fileName);
  89. getAttributeCsv($fileName);
  90.  
  91. function getAttributeCsv($fileName){
  92. // $csv = array_map("str_getcsv", file($fileName,FILE_SKIP_EMPTY_LINES));
  93. $file = fopen($fileName,"r");
  94. while(!feof($file)){
  95. $csv[] = fgetcsv($file, 0, '|');
  96. }
  97. $keys = array_shift($csv);
  98. foreach ($csv as $i=>$row) {
  99. $csv[$i] = array_combine($keys, $row);
  100. }
  101. foreach($csv as $row){
  102. $labelText = $row['frontend_label'];
  103. $attributeCode = $row['attribute_code'];
  104. if($row['_options'] != "")
  105. $options = explode(";", $row['_options']); // add this to createAttribute parameters and call "addAttributeValue" function.
  106. else
  107. $options = -1;
  108. if($row['apply_to'] != "")
  109. $productTypes = explode(",", $row['apply_to']);
  110. else
  111. $productTypes = -1;
  112. unset($row['frontend_label'], $row['attribute_code'], $row['_options'], $row['apply_to'], $row['attribute_id'], $row['entity_type_id'], $row['search_weight']);
  113. createAttribute($labelText, $attributeCode, $row, $productTypes, -1, $options);
  114. }
  115. }
  116.  
  117.  
  118. /**
  119. * Create an attribute.
  120. *
  121. * For reference, see Mage_Adminhtml_Catalog_Product_AttributeController::saveAction().
  122. *
  123. * @return int|false
  124. */
  125. function createAttribute($labelText, $attributeCode, $values = -1, $productTypes = -1, $setInfo = -1, $options = -1)
  126. {
  127.  
  128. $labelText = trim($labelText);
  129. $attributeCode = trim($attributeCode);
  130.  
  131. if($labelText == '' || $attributeCode == '')
  132. {
  133. echo "Can't import the attribute with an empty label or code. LABEL= [$labelText] CODE= [$attributeCode]"."<br/>";
  134. return false;
  135. }
  136.  
  137. if($values === -1)
  138. $values = array();
  139.  
  140. if($productTypes === -1)
  141. $productTypes = array();
  142.  
  143. if($setInfo !== -1 && (isset($setInfo['SetID']) == false || isset($setInfo['GroupID']) == false))
  144. {
  145. echo "Please provide both the set-ID and the group-ID of the attribute-set if you'd like to subscribe to one."."<br/>";
  146. return false;
  147. }
  148.  
  149. echo "Creating attribute [$labelText] with code [$attributeCode]."."<br/>";
  150.  
  151. //>>>> Build the data structure that will define the attribute. See
  152. // Mage_Adminhtml_Catalog_Product_AttributeController::saveAction().
  153.  
  154. $data = array(
  155. 'is_global' => '0',
  156. 'frontend_input' => 'text',
  157. 'default_value_text' => '',
  158. 'default_value_yesno' => '0',
  159. 'default_value_date' => '',
  160. 'default_value_textarea' => '',
  161. 'is_unique' => '0',
  162. 'is_required' => '0',
  163. 'frontend_class' => '',
  164. 'is_searchable' => '1',
  165. 'is_visible_in_advanced_search' => '1',
  166. 'is_comparable' => '1',
  167. 'is_used_for_promo_rules' => '0',
  168. 'is_html_allowed_on_front' => '1',
  169. 'is_visible_on_front' => '0',
  170. 'used_in_product_listing' => '0',
  171. 'used_for_sort_by' => '0',
  172. 'is_configurable' => '0',
  173. 'is_filterable' => '0',
  174. 'is_filterable_in_search' => '0',
  175. 'backend_type' => 'varchar',
  176. 'default_value' => '',
  177. 'is_user_defined' => '0',
  178. 'is_visible' => '1',
  179. 'is_used_for_price_rules' => '0',
  180. 'position' => '0',
  181. 'is_wysiwyg_enabled' => '0',
  182. 'backend_model' => '',
  183. 'attribute_model' => '',
  184. 'backend_table' => '',
  185. 'frontend_model' => '',
  186. 'source_model' => '',
  187. 'note' => '',
  188. 'frontend_input_renderer' => '',
  189. );
  190.  
  191. // Now, overlay the incoming values on to the defaults.
  192. foreach($values as $key => $newValue)
  193. if(isset($data[$key]) == false)
  194. {
  195. echo "Attribute feature [$key] is not valid."."<br/>";
  196. return false;
  197. }
  198.  
  199. else
  200. $data[$key] = $newValue;
  201.  
  202. // Valid product types: simple, grouped, configurable, virtual, bundle, downloadable, giftcard
  203. $data['apply_to'] = $productTypes;
  204. $data['attribute_code'] = $attributeCode;
  205. $data['frontend_label'] = array(
  206. 0 => $labelText,
  207. 1 => '',
  208. 3 => '',
  209. 2 => '',
  210. 4 => '',
  211. );
  212.  
  213. //<<<<
  214.  
  215. //>>>> Build the model.
  216.  
  217. $model = Mage::getModel('catalog/resource_eav_attribute');
  218.  
  219. $model->addData($data);
  220.  
  221. if($setInfo !== -1)
  222. {
  223. $model->setAttributeSetId($setInfo['SetID']);
  224. $model->setAttributeGroupId($setInfo['GroupID']);
  225. }
  226.  
  227. $entityTypeID = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
  228. $model->setEntityTypeId($entityTypeID);
  229.  
  230. $model->setIsUserDefined(1);
  231.  
  232. //<<<<
  233.  
  234. // Save.
  235.  
  236. try
  237. {
  238. $model->save();
  239. }
  240. catch(Exception $ex)
  241. {
  242. echo "Attribute [$labelText] could not be saved: " . $ex->getMessage()."<br/>";
  243. return false;
  244. }
  245.  
  246. if(is_array($options)){
  247. foreach($options as $_opt){
  248. addAttributeValue($attributeCode, $_opt);
  249. }
  250. }
  251.  
  252. $id = $model->getId();
  253.  
  254. echo "Attribute [$labelText] has been saved as ID ($id).<br/>";
  255.  
  256. // return $id;
  257. }
  258.  
  259. function addAttributeValue($arg_attribute, $arg_value)
  260. {
  261. $attribute_model = Mage::getModel('eav/entity_attribute');
  262.  
  263. $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
  264. $attribute = $attribute_model->load($attribute_code);
  265.  
  266. if(!attributeValueExists($arg_attribute, $arg_value))
  267. {
  268. $value['option'] = array($arg_value,$arg_value);
  269. $result = array('value' => $value);
  270. $attribute->setData('option',$result);
  271. $attribute->save();
  272. }
  273.  
  274. $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
  275. $attribute_table = $attribute_options_model->setAttribute($attribute);
  276. $options = $attribute_options_model->getAllOptions(false);
  277.  
  278. foreach($options as $option)
  279. {
  280. if ($option['label'] == $arg_value)
  281. {
  282. return $option['value'];
  283. }
  284. }
  285. return false;
  286. }
  287. function attributeValueExists($arg_attribute, $arg_value)
  288. {
  289. $attribute_model = Mage::getModel('eav/entity_attribute');
  290. $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
  291.  
  292. $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
  293. $attribute = $attribute_model->load($attribute_code);
  294.  
  295. $attribute_table = $attribute_options_model->setAttribute($attribute);
  296. $options = $attribute_options_model->getAllOptions(false);
  297.  
  298. foreach($options as $option)
  299. {
  300. if ($option['label'] == $arg_value)
  301. {
  302. return $option['value'];
  303. }
  304. }
  305.  
  306. return false;
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement