Created
December 22, 2015 23:10
-
-
Save willboudle/2ed3205b7000b13be902 to your computer and use it in GitHub Desktop.
Create Magento Attributes using API and CSV file
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 | |
| // Details | |
| $SETTINGS = array( | |
| "apiUser" => "your_api_username", | |
| "apiKey" => "your_api_password", | |
| "apiURL" => "http://magentohost/api/soap/?w…..", | |
| "default_attr_set_id" => 4, // should always be 4 | |
| "api_ignore_errors" => false, | |
| ); | |
| $csv_file = "path/to/your/csv_file.csv"; | |
| if( is_file( $csv_file ) && is_readable( $csv_file ) ) { | |
| // Create the client | |
| $client = new SoapClient($SETTINGS[‘apiURL’]); | |
| // Login | |
| try { | |
| $session = $client->login($SETTINGS[‘apiUser’], $SETTINGS[‘apiKey’]); | |
| } catch (Exception $e) { | |
| // Something went wrong, either your login details or the API URL, but let’s print them anyway | |
| echo "Something went wrong :("; | |
| print_r($e); | |
| exit; | |
| } | |
| // Now make the attributes from the CSV file; | |
| create_atts_from_csv($csv_file); | |
| echo "complete!"; | |
| } | |
| /* | |
| create_atts_from_csv($filename) | |
| create attributes in format: | |
| Internal Name, Display Name | |
| */ | |
| function create_atts_from_csv($filename) | |
| { | |
| $csv = $filename; | |
| $CSV_ATTS = array(); | |
| $fp = fopen($csv, ‘r’) or die("can’t open file"); | |
| $first = true; | |
| while ($csv_line = fgetcsv($fp, 1024)) { | |
| if (!$first) | |
| $CSV_ATTS[] = $csv_line; | |
| else | |
| $first = FALSE; | |
| } | |
| fclose($fp) or die("can’t close file"); | |
| foreach ($CSV_ATTS as $attribute) | |
| { | |
| create_attribute($attribute[0], $attribute[1]); | |
| } | |
| return; | |
| } | |
| /* | |
| create_attribute($atts) | |
| http://www.magentocommerce.com/api/soap/catalog/catalogProductAttribute/product_attribute.create.html | |
| Note: is_html_allowed_on_front is only allowed with text fields | |
| */ | |
| function create_attribute($att_name, $att_label, $storeid=0, $type="text", $scope="global", $default_value="", $ignore_errors=true){ | |
| global $SETTINGS; | |
| global $client; | |
| global $session; | |
| $new_att_id = null; | |
| // Create Array | |
| $data = array( | |
| "attribute_code" => generate_attribute_code($att_name), | |
| "frontend_input" => $type, | |
| "scope" => $scope, | |
| "default_value" => $default_value, | |
| "is_unique" => 0, | |
| "is_required" => 0, | |
| "is_configurable" => 0, | |
| "is_searchable" => 0, | |
| "is_visible_in_advanced_search" => 0, | |
| "is_comparable" => 0, | |
| "is_used_for_promo_rules" => 0, | |
| "is_visible_on_front" => 1, | |
| "used_in_product_listing" => 1, | |
| "additional_fields" => array( | |
| "is_html_allowed_on_front" => 1 | |
| ), | |
| "frontend_label" => array( | |
| array( | |
| "store_id" => $storeid, | |
| "label" => $att_label | |
| ) | |
| ) | |
| ); | |
| //print_r($data); | |
| // Note that if you have the extension SEO Layered Navigation installed, set the comment to "" as they have a bug in their code | |
| // $data[‘comment’] = ""; | |
| try { | |
| $new_att_id = $client->call($session, ‘product_attribute.create’, array($data)); | |
| } | |
| // product_attribute.list | |
| catch(SoapFault $e){ | |
| if( $SETTINGS[‘api_ignore_errors’] != true) | |
| print_r($e); | |
| } | |
| //print_r($new_att_id); | |
| // Now we need to add the attribute to the default group | |
| if($new_att_id) | |
| { | |
| // Now add that to the | |
| try { | |
| $result = $client->call( | |
| $session, | |
| "product_attribute_set.attributeAdd", | |
| array( | |
| (int) $new_att_id, | |
| $SETTINGS[‘default_attr_set_id’] | |
| ) | |
| ); | |
| } | |
| // product_attribute.list | |
| catch(SoapFault $e){ | |
| if($SETTINGS[‘api_ignore_errors’] != true) | |
| print_r($e); | |
| } | |
| } // /if new_att_id valid | |
| } | |
| /* | |
| used for attributes to make internal magento code | |
| */ | |
| function generate_attribute_code($att_name, $att_prefix="att_", $maxLength=100) | |
| { | |
| $result = str_replace("(","_", $att_name); | |
| $result = str_replace(")","_", $result); | |
| $result = strtolower($result); | |
| $result = preg_replace("/[^a-z0-9\s-]/", "", $result); | |
| $result = trim(preg_replace("/[\s-]+/", " ", $result)); | |
| $result = trim(substr($result, 0, $maxLength)); | |
| $result = preg_replace("/\s/", "_", $result); | |
| $result = $att_prefix . $result; | |
| // there is a limit of 30 chars to attribute names | |
| if (strlen($result) > 30) | |
| $result = substr($result, 0, 30); | |
| return $result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment