Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: d16d6cc134
...
compare: 2c425eaaf5
  • 1 commit
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 83 additions and 6 deletions.
  1. +9 −0 README.textile
  2. +20 −6 attr-add.php
  3. +54 −0 lib/attribute_functions.php
View
9 README.textile
@@ -20,6 +20,15 @@ bc. ./attr-list.php -u
On a blank Magento installation this will list only 3 attributes: @color@, @cost@, and @manufacturer@.
+h3. Create Normal Data Attribute
+
+bc. ./attr-add.php --code length --label 'Panjang (cm)' --type data --datatype decimal --normal
+./attr-add.php --code shop_id --label 'Shop ID' --type data --datatype varchar --normal
+./attr-add.php --code local_sku --label 'Local SKU' --type data --datatype varchar --normal
+./attr-add.php --code local_price --label 'Local Price' --type data --datatype decimal --normal
+
+Note: @--normal@ switch is optional (it's the default).
+
h3. Create Normal Select Attribute
bc. ./attr-add.php --code material --label Bahan --type select --normal --opts Katun,Sutra,Rayon,Spandex
View
26 attr-add.php
@@ -3,18 +3,32 @@
require_once 'lib/init.php';
require_once 'lib/attribute_functions.php';
-$args = getopt('', array('code:', 'label:', 'opts:', 'normal', 'configurable', 'type:'));
+$args = getopt('', array('code:', 'label:', 'opts:', 'datatype:', 'normal', 'configurable', 'type:'));
if (empty($args) || empty($args['code']) || empty($args['label'])) {
- echo "Create an attribute\n";
- echo "Normal select attribute: attr-add.php --code CODE --label LABEL --type select [--normal] --opts [OPT,OPT,...]\n";
- echo "Configurable select attribute: attr-add.php --code CODE --label LABEL --type select --configurable --opts [OPT,OPT,...]\n";
+ echo "1. Create a data attribute\n";
+ echo " Normal data attribute: attr-add.php --code CODE --label LABEL --type data --datatype DATATYPE [--normal]\n";
+ echo " Configurable data attribute: attr-add.php --code CODE --label LABEL --type data --datatype DATATYPE [--configurable]\n";
+ echo " Data types: varchar, int, decimal, datetime, text\n";
+ echo "2. Create a select attribute\n";
+ echo " Normal select attribute: attr-add.php --code CODE --label LABEL --type select [--normal] --opts [OPT,OPT,...]\n";
+ echo " Configurable select attribute: attr-add.php --code CODE --label LABEL --type select --configurable --opts [OPT,OPT,...]\n";
exit(1);
}
$code = $args['code'];
$label = $args['label'];
$type = $args['type'];
+$datatype = isset($args['datatype']) ? $args['datatype'] : null;
$configurable = isset($args['configurable']);
-$opts = isset($args['opts']) ? split(',', $args['opts']) : array();
+$options = isset($args['opts']) ? split(',', $args['opts']) : array();
-createSelectAttribute($code, $label, $configurable, $opts);
+switch ($type) {
+ case 'data':
+ createDataAttribute($code, $label, $datatype, $configurable);
+ break;
+ case 'select':
+ createSelectAttribute($code, $label, $configurable, $options);
+ break;
+ default:
+ throw new Exception("Unknown attribute type: $type");
+}
View
54 lib/attribute_functions.php
@@ -1,12 +1,66 @@
<?php
/**
+ * Create data attribute, can be normal or configurable.
+ *
+ * @param string $code
+ * @param string $label
+ * @param string $datatype varchar, int, decimal, datetime, text
+ * @param boolean $configurable If false then normal attribute.
+ * @return int Attribute ID
+ */
+function createDataAttribute($code, $label, $datatype, $configurable) {
+ echo "Create ". ($configurable ? 'configurable' : 'normal') ." data attribute $code datatype: $datatype label: $label\n";
+
+ $frontendInputs = array(
+ 'varchar' => 'text',
+ 'int' => 'text',
+ 'decimal' => 'text',
+ 'datetime' => 'date',
+ 'text' => 'textarea');
+
+ $attr = Mage::getModel('catalog/resource_eav_attribute');
+ $data = array(
+ 'attribute_code' => $code,
+ 'backend_type' => $datatype,
+ 'frontend_input' => $frontendInputs[ $datatype ],
+ 'frontend_label' => $label,
+ 'is_required' => 0,
+ 'is_user_defined' => 1,
+ 'default_value' => '',
+ 'is_unique' => 0,
+ 'is_global' => 1,
+ 'is_visible' => 1,
+ 'is_searchable' => 1,
+ 'is_filterable' => 1,
+ 'is_comparable' => 1,
+ 'is_visible_on_front' => 1,
+ 'is_html_allowed_on_front' => 1,
+ 'is_used_for_price_rules' => 1,
+ 'is_filterable_in_search' => 1,
+ 'used_in_product_listing' => 1,
+ 'used_for_sort_by' => 1,
+ 'is_configurable' => $configurable ? 1 : 0,
+ 'is_visible_in_advanced_search' => 1,
+ 'is_used_for_promo_rules' => 1);
+ $productEntityTypeId = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
+ $attr->setEntityTypeId($productEntityTypeId);
+
+ $attr->addData($data);
+ $attr->save();
+
+ echo "Created ". ($configurable ? 'configurable' : 'normal') ." $datatype attribute $code #{$attr->getId()}.\n";
+ return $attr->getId();
+}
+
+/**
* Create select attribute with options, can be normal or configurable.
*
* @param string $code
* @param string $label
* @param boolean $configurable If false then normal attribute.
* @param array $options Array of string options.
+ * @return int Attribute ID
*/
function createSelectAttribute($code, $label, $configurable, $options) {
echo "Create ". ($configurable ? 'configurable' : 'normal') ." attribute $code label: $label opts: ". join(', ', $options) ."\n";

No commit comments for this range

Something went wrong with that request. Please try again.