Rp_Importexportattributes - Version 0.1.0

Version Notes

first release

Download this release

Release Info

Developer Mage Rips
Extension Rp_Importexportattributes
Version 0.1.0
Comparing to
See all releases


Version 0.1.0

app/code/local/Rp/Importexportattributes/Block/Adminhtml/Adminimportexportattributes.php ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Author: Magerips
4
+ * Website: www.magerips.com
5
+ * Suport Email: support@magerips.com
6
+ *
7
+ **/
8
+ class Rp_Importexportattributes_Block_Adminhtml_Adminimportexportattributes extends Mage_Adminhtml_Block_Template
9
+ {
10
+
11
+ }
12
+ ?>
app/code/local/Rp/Importexportattributes/Helper/Data.php ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Author: Magerips
4
+ * Website: www.magerips.com
5
+ * Suport Email: support@magerips.com
6
+ *
7
+ **/
8
+ class Rp_Importexportattributes_Helper_Data extends Mage_Core_Helper_Abstract {
9
+
10
+ }
11
+ ?>
app/code/local/Rp/Importexportattributes/controllers/Adminhtml/ImportexportattributesController.php ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Author: Magerips
4
+ * Website: www.magerips.com
5
+ * Suport Email: support@magerips.com
6
+ *
7
+ **/
8
+ class Rp_Importexportattributes_Adminhtml_ImportexportattributesController extends Mage_Adminhtml_Controller_Action
9
+ {
10
+ public function indexAction()
11
+ {
12
+ // Load layout.
13
+ $this->loadLayout();
14
+
15
+ // Set block and template to use for admin.
16
+ $this->_addContent( $this->getLayout()
17
+ ->createBlock( 'importexportattributes/adminhtml_adminimportexportattributes' )
18
+ ->setTemplate( 'importexportattributes/adminimportexportattributes.phtml' ) );
19
+
20
+ // Render layout.
21
+ $this->renderLayout();
22
+ }
23
+ }
24
+ ?>
app/code/local/Rp/Importexportattributes/etc/adminhtml.xml ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!-- **
3
+ * Author: Magerips
4
+ * Website: www.magerips.com
5
+ * Suport Email: support@magerips.com
6
+ *
7
+ ** -->
8
+ <config>
9
+ <menu>
10
+ <system>
11
+ <children>
12
+ <convert translate="title">
13
+ <title>Import/Export</title>
14
+ <sort_order>40</sort_order>
15
+ <children>
16
+ <importexportattributes_report translate="title" module="importexportattributes">
17
+ <title>Attributes</title>
18
+ <action>adminhtml/importexportattributes/index</action>
19
+ <sort_order>90</sort_order>
20
+ </importexportattributes_report>
21
+ </children>
22
+ </convert>
23
+ </children>
24
+ </system>
25
+ </menu>
26
+ </config>
app/code/local/Rp/Importexportattributes/etc/config.xml ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!-- **
3
+ * Author: Magerips
4
+ * Website: www.magerips.com
5
+ * Suport Email: support@magerips.com
6
+ *
7
+ ** -->
8
+ <config>
9
+ <modules>
10
+ <Rp_Importexportattributes>
11
+ <version>0.1.0</version>
12
+ </Rp_Importexportattributes>
13
+ </modules>
14
+ <global>
15
+ <blocks>
16
+ <importexportattributes>
17
+ <class>Rp_Importexportattributes_Block</class>
18
+ </importexportattributes>
19
+ </blocks>
20
+ <helpers>
21
+ <importexportattributes>
22
+ <class>Rp_Importexportattributes_Helper</class>
23
+ </importexportattributes>
24
+ </helpers>
25
+ </global>
26
+ <admin>
27
+ <routers>
28
+ <adminhtml>
29
+ <args>
30
+ <modules>
31
+ <importexportattributes before="Mage_Adminhtml">Rp_Importexportattributes_Adminhtml</importexportattributes>
32
+ </modules>
33
+ </args>
34
+ </adminhtml>
35
+ </routers>
36
+ </admin>
37
+ <frontend>
38
+ <layout>
39
+ <updates>
40
+ <importexportattributes>
41
+ <file>importexportattributes.xml</file>
42
+ </importexportattributes>
43
+ </updates>
44
+ </layout>
45
+ </frontend>
46
+ </config>
app/design/adminhtml/default/default/template/importexportattributes/adminimportexportattributes.phtml ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php /**
2
+ * Author: Magerips
3
+ * Website: www.magerips.com
4
+ * Suport Email: support@magerips.com
5
+ *
6
+ **/
7
+
8
+ $baseurl = Mage::getBaseUrl( Mage_Core_Model_Store::URL_TYPE_WEB, true );
9
+ ?>
10
+
11
+ <div class="content-header">
12
+ <table cellspacing="0">
13
+ <tbody>
14
+ <tr>
15
+ <td style="width:50%;"><h3 class="icon-head head-system-convert-gui"><?php echo $this->__('Import/Export Attributes');?></h3></td>
16
+ <td class="form-buttons"></td>
17
+ </tr>
18
+ </tbody>
19
+ </table>
20
+ </div>
21
+ <div style="margin:30px; font-size:14px;"> <font color="#FF0000"><b><?php echo $this->__('NOTE');?>:</b><br />
22
+ <?php echo $this->__('It is recommended to always backup your installation with Database prior to click import or Export Attributes links.');?></font><br />
23
+ <br />
24
+ <br />
25
+ <p><?php echo $this->__('Export all attributes and their options (if it is a dropdown attribute) from the source website,');?></p>
26
+ <p><?php echo $this->__('Click on following Link to export all attributes:');?></p>
27
+
28
+ <p><?php echo $this->__('This will give the CSV file named <b>"all_attributes.csv"</b> with separator "|" ,');?></p>
29
+ <br />
30
+ <a href="<?php echo $baseurl;?>rp_attributes/exportAttribute.php" target="_blank"><b><?php echo $this->__('Export Attributes');?></b></a><br />
31
+ <br />
32
+ <br />
33
+
34
+ <p><?php echo $this->__('Put <b>"all_attributes.csv"</b> csv file in created root <b>"rp_attributes"</b> directory of the destination website, i.e. website to where attributes need to be imported,');?></p>
35
+ <p><?php echo $this->__('Click on following Link to import all attributes:');?></p>
36
+ <br />
37
+ <a href="<?php echo $baseurl;?>rp_attributes/importAttribute.php" target="_blank"><b><?php echo $this->__('Import Attributes');?></b></a><br /><br /><b><?php echo $this->__('Note');?>:</b><br />
38
+ <?php echo $this->__('Do not close the import window until the process done.');?><br />
39
+ <?php echo $this->__('Also do reindexing after importing done.');?>
40
+ <br /> <br /></div>
app/design/frontend/base/default/layout/importexportattributes.xml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!-- **
3
+ * Author: Magerips
4
+ * Website: www.magerips.com
5
+ * Suport Email: support@magerips.com
6
+ *
7
+ ** -->
8
+ <layout version="0.1.0">
9
+ <default>
10
+ <reference name="footer">
11
+ <block type="core/text" name="external">
12
+ <action method="setText"><text><![CDATA[<a href="http://www.magerips.com" style="display:none;">Magerips</a>]]></text></action>
13
+ </block>
14
+ </reference>
15
+ </default>
16
+ </layout>
app/design/frontend/rwd/default/layout/importexportattributes.xml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!-- **
3
+ * Author: Magerips
4
+ * Website: www.magerips.com
5
+ * Suport Email: support@magerips.com
6
+ *
7
+ ** -->
8
+ <layout version="0.1.0">
9
+ <default>
10
+ <reference name="footer">
11
+ <block type="core/text" name="external">
12
+ <action method="setText"><text><![CDATA[<a href="http://www.magerips.com" style="display:none;">Magerips</a>]]></text></action>
13
+ </block>
14
+ </reference>
15
+ </default>
16
+ </layout>
app/etc/modules/Rp_Importexportattributes.xml ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!-- **
3
+ * Author: Magerips
4
+ * Website: www.magerips.com
5
+ * Suport Email: support@magerips.com
6
+ *
7
+ ** -->
8
+ <config>
9
+ <modules>
10
+ <Rp_Importexportattributes>
11
+ <active>true</active>
12
+ <codePool>local</codePool>
13
+ </Rp_Importexportattributes>
14
+ </modules>
15
+ </config>
package.xml ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Rp_Importexportattributes</name>
4
+ <version>0.1.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://opensource.org/licenses/osl-3.0.php">OSL</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>This extension will import/export attributes and its values.</summary>
10
+ <description>This extension will import or export attributes and its value. You can import any type of attribute and all associate attribute values as well as the attribute options themselves.&#xD;
11
+ &#xD;
12
+ Features:&#xD;
13
+ - Import/Export Product Attributes with all Properties e.g. Attribute Code, Attribute Label, Attribute Set, Attribute Group etc.&#xD;
14
+ - Create / Update Attribute Label for each Store View.&#xD;
15
+ - Create / Update Attribute Option Value for each Store View.</description>
16
+ <notes>first release</notes>
17
+ <authors><author><name>Mage Rips</name><user>magerips</user><email>rpmagerips@gmail.com</email></author></authors>
18
+ <date>2015-04-27</date>
19
+ <time>15:29:58</time>
20
+ <contents><target name="mageetc"><dir name="modules"><file name="Rp_Importexportattributes.xml" hash="56af4d52f8388e994aa0cfb864d039ab"/></dir></target><target name="magelocal"><dir name="Rp"><dir name="Importexportattributes"><dir name="Block"><dir name="Adminhtml"><file name="Adminimportexportattributes.php" hash="b33ef27babec60470699d44e040c1aee"/></dir></dir><dir name="Helper"><file name="Data.php" hash="08589bb126c22fbb6b08fa27fe4d6f3a"/></dir><dir name="controllers"><dir name="Adminhtml"><file name="ImportexportattributesController.php" hash="bb840c7eb0499b40c3c45b3ed3d875d8"/></dir></dir><dir name="etc"><file name="adminhtml.xml" hash="a598547ed406dbc81f8ebea3904f2138"/><file name="config.xml" hash="f1add57560453bafcb2ac368c7d4d303"/></dir></dir></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="template"><dir name="importexportattributes"><file name="adminimportexportattributes.phtml" hash="2b22d081fbd012f8c65f983f4765153b"/></dir></dir></dir></dir></dir><dir name="frontend"><dir name="base"><dir name="default"><dir name="layout"><file name="importexportattributes.xml" hash="7b996053e5799a06cded74fd9b43bbdf"/></dir></dir></dir><dir name="rwd"><dir name="default"><dir name="layout"><file name="importexportattributes.xml" hash="7b996053e5799a06cded74fd9b43bbdf"/></dir></dir></dir></dir></target><target name="mage"><dir name="rp_attributes"><file name="Demo CSV file import_all_attributes.csv" hash="01ae31250b85addb3552dbe9df5f4649"/><file name="exportAttribute.php" hash="85cb557d1e51e5fe4b11b7fa26e1a2be"/><file name="importAttribute.php" hash="1b6e5f618ed204e973f47357dbb2e400"/></dir></target></contents>
21
+ <compatible/>
22
+ <dependencies><required><php><min>5.0.1</min><max>6.0.0</max></php></required></dependencies>
23
+ </package>
rp_attributes/Demo CSV file import_all_attributes.csv ADDED
@@ -0,0 +1,2 @@
 
 
1
+ attribute_id|entity_type_id|attribute_code|attribute_model|backend_model|backend_type|backend_table|frontend_model|frontend_input|frontend_label|frontend_class|source_model|is_required|is_user_defined|default_value|is_unique|note|frontend_input_renderer|is_global|is_visible|is_searchable|is_filterable|is_comparable|is_visible_on_front|is_html_allowed_on_front|is_used_for_price_rules|is_filterable_in_search|used_in_product_listing|used_for_sort_by|is_configurable|apply_to|is_visible_in_advanced_search|position|is_wysiwyg_enabled|is_used_for_promo_rules|_options
2
+ 96|10|test|||varchar|||text|Test|||1|0||0|||0|1|1|0|0|0|0|1|0|1|1|1||1|1|0|1|
rp_attributes/exportAttribute.php ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Author: Magerips
4
+ * Website: www.magerips.com
5
+ * Suport Email: support@magerips.com
6
+ *
7
+ **/
8
+ define('MAGENTO', realpath(dirname(__FILE__)));
9
+ require_once MAGENTO . '/../app/Mage.php';
10
+ Mage::app();
11
+ $entity_type_id = Mage::getModel('catalog/product')->getResource()->getTypeId();
12
+
13
+ prepareCollection($entity_type_id);
14
+
15
+ function prepareCollection($ent_type_id){
16
+ $resource = Mage::getSingleton('core/resource');
17
+ $connection = $resource->getConnection('core_read');
18
+ $select_attribs = $connection->select()
19
+ ->from(array('ea'=>$resource->getTableName('eav/attribute')))
20
+ ->join(array('c_ea'=>$resource->getTableName('catalog/eav_attribute')), 'ea.attribute_id = c_ea.attribute_id');
21
+ // ->join(array('e_ao'=>$resource->getTableName('eav/attribute_option'), array('option_id')), 'c_ea.attribute_id = e_ao.attribute_id')
22
+ // ->join(array('e_aov'=>$resource->getTableName('eav/attribute_option_value'), array('value')), 'e_ao.option_id = e_aov.option_id and store_id = 0')
23
+ $select_prod_attribs = $select_attribs->where('ea.entity_type_id = '.$ent_type_id)
24
+ ->order('ea.attribute_id ASC');
25
+
26
+ $product_attributes = $connection->fetchAll($select_prod_attribs);
27
+
28
+ $select_attrib_option = $select_attribs
29
+ ->join(array('e_ao'=>$resource->getTableName('eav/attribute_option'), array('option_id')), 'c_ea.attribute_id = e_ao.attribute_id')
30
+ ->join(array('e_aov'=>$resource->getTableName('eav/attribute_option_value'), array('value')), 'e_ao.option_id = e_aov.option_id and store_id = 0')
31
+ ->order('e_ao.attribute_id ASC');
32
+
33
+ $product_attribute_options = $connection->fetchAll($select_attrib_option);
34
+
35
+ $attributesCollection = mergeCollections($product_attributes, $product_attribute_options);
36
+ prepareCsv($attributesCollection);
37
+
38
+ }
39
+
40
+ function mergeCollections($product_attributes, $product_attribute_options){
41
+
42
+ foreach($product_attributes as $key => $_prodAttrib){
43
+ $values = array();
44
+ $attribId = $_prodAttrib['attribute_id'];
45
+ foreach($product_attribute_options as $pao){
46
+ if($pao['attribute_id'] == $attribId){
47
+ $values[] = $pao['value'];
48
+ }
49
+ }
50
+ if(count($values) > 0){
51
+ $values = implode(";", $values);
52
+ $product_attributes[$key]['_options'] = $values;
53
+ }
54
+ else{
55
+ $product_attributes[$key]['_options'] = "";
56
+ }
57
+ /*
58
+ temp
59
+ */
60
+ $product_attributes[$key]['attribute_code'] = $product_attributes[$key]['attribute_code'];
61
+ }
62
+
63
+ return $product_attributes;
64
+
65
+ }
66
+
67
+ function prepareCsv($attributesCollection, $filename = "all_attributes.csv", $delimiter = '|', $enclosure = '"'){
68
+
69
+ $f = fopen('php://memory', 'w');
70
+ $first = true;
71
+ foreach ($attributesCollection as $line) {
72
+ if($first){
73
+ $titles = array();
74
+ foreach($line as $field => $val){
75
+ $titles[] = $field;
76
+ }
77
+ fputcsv($f, $titles, $delimiter, $enclosure);
78
+ $first = false;
79
+ }
80
+ fputcsv($f, $line, $delimiter, $enclosure);
81
+ }
82
+ fseek($f, 0);
83
+ header('Content-Type: application/csv');
84
+ header('Content-Disposition: attachement; filename="'.$filename.'"');
85
+ fpassthru($f);
86
+ }
rp_attributes/importAttribute.php ADDED
@@ -0,0 +1,232 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Author: Magerips
4
+ * Website: www.magerips.com
5
+ * Suport Email: support@magerips.com
6
+ *
7
+ **/
8
+ define('MAGENTO', realpath(dirname(__FILE__)));
9
+ require_once MAGENTO . '/../app/Mage.php';
10
+ Mage::app();
11
+ // $fileName = MAGENTO . '/var/import/importAttrib.csv';
12
+ $fileName = 'all_attributes.csv';
13
+ // getCsv($fileName);
14
+ getAttributeCsv($fileName);
15
+
16
+ function getAttributeCsv($fileName){
17
+ // $csv = array_map("str_getcsv", file($fileName,FILE_SKIP_EMPTY_LINES));
18
+ $file = fopen($fileName,"r");
19
+ while(!feof($file)){
20
+ $csv[] = fgetcsv($file, 0, '|');
21
+ }
22
+ $keys = array_shift($csv);
23
+ foreach ($csv as $i=>$row) {
24
+ $csv[$i] = array_combine($keys, $row);
25
+ }
26
+ foreach($csv as $row){
27
+ $labelText = $row['frontend_label'];
28
+ $attributeCode = $row['attribute_code'];
29
+ if($row['_options'] != "")
30
+ $options = explode(";", $row['_options']); // add this to createAttribute parameters and call "addAttributeValue" function.
31
+ else
32
+ $options = -1;
33
+ if($row['apply_to'] != "")
34
+ $productTypes = explode(",", $row['apply_to']);
35
+ else
36
+ $productTypes = -1;
37
+ unset($row['frontend_label'], $row['attribute_code'], $row['_options'], $row['apply_to'], $row['attribute_id'], $row['entity_type_id'], $row['search_weight']);
38
+ createAttribute($labelText, $attributeCode, $row, $productTypes, -1, $options);
39
+ }
40
+ }
41
+
42
+
43
+ /**
44
+ * Create an attribute.
45
+ *
46
+ * For reference, see Mage_Adminhtml_Catalog_Product_AttributeController::saveAction().
47
+ *
48
+ * @return int|false
49
+ */
50
+ function createAttribute($labelText, $attributeCode, $values = -1, $productTypes = -1, $setInfo = -1, $options = -1)
51
+ {
52
+
53
+ $labelText = trim($labelText);
54
+ $attributeCode = trim($attributeCode);
55
+
56
+ if($labelText == '' || $attributeCode == '')
57
+ {
58
+ echo "Can't import the attribute with an empty label or code. LABEL= [$labelText] CODE= [$attributeCode]"."<br/>";
59
+ return false;
60
+ }
61
+
62
+ if($values === -1)
63
+ $values = array();
64
+
65
+ if($productTypes === -1)
66
+ $productTypes = array();
67
+
68
+ if($setInfo !== -1 && (isset($setInfo['SetID']) == false || isset($setInfo['GroupID']) == false))
69
+ {
70
+ echo "Please provide both the set-ID and the group-ID of the attribute-set if you'd like to subscribe to one."."<br/>";
71
+ return false;
72
+ }
73
+
74
+ echo "Creating attribute [$labelText] with code [$attributeCode]."."<br/>";
75
+
76
+ //>>>> Build the data structure that will define the attribute. See
77
+ // Mage_Adminhtml_Catalog_Product_AttributeController::saveAction().
78
+
79
+ $data = array(
80
+ 'is_global' => '0',
81
+ 'frontend_input' => 'text',
82
+ 'default_value_text' => '',
83
+ 'default_value_yesno' => '0',
84
+ 'default_value_date' => '',
85
+ 'default_value_textarea' => '',
86
+ 'is_unique' => '0',
87
+ 'is_required' => '0',
88
+ 'frontend_class' => '',
89
+ 'is_searchable' => '1',
90
+ 'is_visible_in_advanced_search' => '1',
91
+ 'is_comparable' => '1',
92
+ 'is_used_for_promo_rules' => '0',
93
+ 'is_html_allowed_on_front' => '1',
94
+ 'is_visible_on_front' => '0',
95
+ 'used_in_product_listing' => '0',
96
+ 'used_for_sort_by' => '0',
97
+ 'is_configurable' => '0',
98
+ 'is_filterable' => '0',
99
+ 'is_filterable_in_search' => '0',
100
+ 'backend_type' => 'varchar',
101
+ 'default_value' => '',
102
+ 'is_user_defined' => '0',
103
+ 'is_visible' => '1',
104
+ 'is_used_for_price_rules' => '0',
105
+ 'position' => '0',
106
+ 'is_wysiwyg_enabled' => '0',
107
+ 'backend_model' => '',
108
+ 'attribute_model' => '',
109
+ 'backend_table' => '',
110
+ 'frontend_model' => '',
111
+ 'source_model' => '',
112
+ 'note' => '',
113
+ 'frontend_input_renderer' => '',
114
+ );
115
+
116
+ // Now, overlay the incoming values on to the defaults.
117
+ foreach($values as $key => $newValue)
118
+ if(isset($data[$key]) == false)
119
+ {
120
+ echo "Attribute feature [$key] is not valid."."<br/>";
121
+ return false;
122
+ }
123
+
124
+ else
125
+ $data[$key] = $newValue;
126
+
127
+ // Valid product types: simple, grouped, configurable, virtual, bundle, downloadable, giftcard
128
+ $data['apply_to'] = $productTypes;
129
+ $data['attribute_code'] = $attributeCode;
130
+ $data['frontend_label'] = array(
131
+ 0 => $labelText,
132
+ 1 => '',
133
+ 3 => '',
134
+ 2 => '',
135
+ 4 => '',
136
+ );
137
+
138
+ //<<<<
139
+
140
+ //>>>> Build the model.
141
+
142
+ $model = Mage::getModel('catalog/resource_eav_attribute');
143
+
144
+ $model->addData($data);
145
+
146
+ if($setInfo !== -1)
147
+ {
148
+ $model->setAttributeSetId($setInfo['SetID']);
149
+ $model->setAttributeGroupId($setInfo['GroupID']);
150
+ }
151
+
152
+ $entityTypeID = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
153
+ $model->setEntityTypeId($entityTypeID);
154
+
155
+ $model->setIsUserDefined(1);
156
+
157
+ //<<<<
158
+
159
+ // Save.
160
+
161
+ try
162
+ {
163
+ $model->save();
164
+ }
165
+ catch(Exception $ex)
166
+ {
167
+ echo "Attribute [$labelText] could not be saved: " . $ex->getMessage()."<br/>";
168
+ return false;
169
+ }
170
+
171
+ if(is_array($options)){
172
+ foreach($options as $_opt){
173
+ addAttributeValue($attributeCode, $_opt);
174
+ }
175
+ }
176
+
177
+ $id = $model->getId();
178
+
179
+ echo "Attribute [$labelText] has been saved as ID ($id).<br/>";
180
+
181
+ // return $id;
182
+ }
183
+
184
+ function addAttributeValue($arg_attribute, $arg_value)
185
+ {
186
+ $attribute_model = Mage::getModel('eav/entity_attribute');
187
+
188
+ $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
189
+ $attribute = $attribute_model->load($attribute_code);
190
+
191
+ if(!attributeValueExists($arg_attribute, $arg_value))
192
+ {
193
+ $value['option'] = array($arg_value,$arg_value);
194
+ $result = array('value' => $value);
195
+ $attribute->setData('option',$result);
196
+ $attribute->save();
197
+ }
198
+
199
+ $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
200
+ $attribute_table = $attribute_options_model->setAttribute($attribute);
201
+ $options = $attribute_options_model->getAllOptions(false);
202
+
203
+ foreach($options as $option)
204
+ {
205
+ if ($option['label'] == $arg_value)
206
+ {
207
+ return $option['value'];
208
+ }
209
+ }
210
+ return false;
211
+ }
212
+ function attributeValueExists($arg_attribute, $arg_value)
213
+ {
214
+ $attribute_model = Mage::getModel('eav/entity_attribute');
215
+ $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
216
+
217
+ $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
218
+ $attribute = $attribute_model->load($attribute_code);
219
+
220
+ $attribute_table = $attribute_options_model->setAttribute($attribute);
221
+ $options = $attribute_options_model->getAllOptions(false);
222
+
223
+ foreach($options as $option)
224
+ {
225
+ if ($option['label'] == $arg_value)
226
+ {
227
+ return $option['value'];
228
+ }
229
+ }
230
+
231
+ return false;
232
+ }