Skip to content

Instantly share code, notes, and snippets.

@jonathonbyrdziak
Last active December 18, 2015 10:19
Show Gist options
  • Save jonathonbyrdziak/5767674 to your computer and use it in GitHub Desktop.
Save jonathonbyrdziak/5767674 to your computer and use it in GitHub Desktop.

Removes the magento attribute values from the database

This is only part of the statement, you'll need to first delete the values, then delete the options associated with those values. The statement to delete the options is below this statement.

DELETE value FROM eav_attribute_option_value AS `value` 
INNER JOIN eav_attribute_option AS `options` 
ON options.option_id = `value`.option_id 
WHERE `options`.attribute_id = '139'

Delete attribute options

DELETE options FROM eav_attribute_option AS `options` 
WHERE `options`.attribute_id = '139'

Method to create new product attributes

Drop this method into your setup class, then you can quickly and easily create new product attributes during upgrades.

private function createAttribute($code, $label, $attribute_type = 'text', $product_type = '', $visible = true)
{
  if (strpos($product_type,',')!==false) {
    $product_type = explode(',',$product_type);
  } else {
    $product_type = array($product_type);
  }

  $_attribute_data = array(
    'attribute_code' => $code,
    'is_global' => '1',
    // Valid types: boolean, text, textarea
    'frontend_input' => $attribute_type, 
    'default_value_text' => '',
    'default_value_yesno' => '0',
    'default_value_date' => '',
    'default_value_textarea' => '',
    'is_unique' => '0',
    'is_required' => '0',
    // Valid product types: simple, grouped, configurable, virtual, bundle, downloadable, giftcard
    'apply_to' => $product_type,
    'is_configurable' => '0',
    'is_searchable' => '0',
    'is_visible_in_advanced_search' => '0',
    'is_comparable' => '0',
    'is_used_for_price_rules' => '0',
    'is_wysiwyg_enabled' => '0',
    'is_html_allowed_on_front' => '1',
    'is_visible_on_front' => '0',
    'used_in_product_listing' => '0',
    'used_for_sort_by' => '0',
    'frontend_label' => $label
  );
  $model = Mage::getModel('catalog/resource_eav_attribute');
  if (!isset($_attribute_data['is_configurable'])) {
    $_attribute_data['is_configurable'] = 0;
  }
  if (!isset($_attribute_data['is_filterable'])) {
    $_attribute_data['is_filterable'] = 0;
  }
  if (!isset($_attribute_data['is_filterable_in_search'])) {
    $_attribute_data['is_filterable_in_search'] = 0;
  }
  if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
    $_attribute_data['backend_type'] = $model->getBackendTypeByInput($_attribute_data['frontend_input']);
  }
  $defaultValueField = $model->getDefaultValueByInput($_attribute_data['frontend_input']);
  if ($defaultValueField) {
    $_attribute_data['default_value'] = $this->getRequest()->getParam($defaultValueField);
  }
  $model->addData($_attribute_data);
  $model->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId());
  $model->setIsUserDefined($visible);
  try {
    $model->save();
  } catch (Exception $e) { echo '<p>Sorry, error occured while trying to save the attribute. Error: '.$e->getMessage().'</p>'; }
  
  return $this;
}

Here is how to use this method, once you've dropped it into a class.

//My "grouped product only" attribute
$this->createAttribute(strtolower("ShirtType"), "ShirtType", "select", "grouped");
//My "simple product only" attribute
$this->createAttribute(strtolower("Swatch"), "Swatch", "text", "simple");
//My "joint" attribute, the one that I had both on simple and on grouped products
$this->createAttribute(strtolower("InventoryMsg"), "InventoryMsg", "text", "");
$this->createAttribute(strtolower("Complete"), "Complete", "boolean", "");

Get Attribute ID from Code

This is normally used if you want to check to see if the attribute actually exists in your database.

public function getAttributeId($entity, $code)
{
  $eavAttribute = new Mage_Eav_Model_Mysql4_Entity_Attribute();
  return $eavAttribute->getIdByCode($entity, $code);
}

Here's the code I normally drop into my installer script to create a new attribute.

if (!$installer->getAttributeId(Mage_Catalog_Model_Product::ENTITY, 'bb_compatibilities')) {
  $installer->createAttribute('bb_compatibilities', 'Compatibilities');
}

Check if product has attribute

It's one thing to create a new attribute, it's a whole nother thing to associate it with a product. Here's how to check to see if a product contains a particular attribute.

$product->offsetExists('bb_compatibilities')

Add Attribute to all Attribute Sets

I couldn't get this to work, maybe you can. Let me know if you do.

$attrID = $this->getAttributeId(Mage_Catalog_Model_Product::ENTITY, $code);
$model_attr = Mage::getModel('eav/entity_setup','core_setup');
foreach ($model->getAllAttributeSetIds(Mage_Catalog_Model_Product::ENTITY) as $setId)
{
  $groupId = $model_attr->getAttributeGroup(Mage_Catalog_Model_Product::ENTITY,$setId,'General');
  $model_attr->addAttributeToSet(Mage_Catalog_Model_Product::ENTITY, $setId, $groupId, $attrID);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment