Skip to content

Instantly share code, notes, and snippets.

@jacquesbh
Last active December 11, 2015 09:48
Show Gist options
  • Save jacquesbh/4582238 to your computer and use it in GitHub Desktop.
Save jacquesbh/4582238 to your computer and use it in GitHub Desktop.
<?php
/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */
$installer = $this;
$installer->startSetup();
// Add the attribute `baz` with some fields
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'baz', array(
'label' => 'The Baz', // The label ;)
'group' => 'General', // The group (in the admin page)
'input' => 'text', // Used for the input type
'required' => true, // Is it required?
//'source' => '', // Used for a source model
//'backend' => '', // Used for a backend model (before and after save, before and after load of the product)
//'frontend' => '', // Used for a frontend model (like the image frontend model with the getUrl method)
//'class' => '', // Used for a backend HTML class (usefull for javascript validation – with the 'validate-email' class for example)
'comparable' => false, // Comparable? (on front, via the comparable products).
//'default' => '', // The default value
'filterable' => false, // Filterable? (on front, in the category view or search)
//'apply_to' => '', // Used for product type restrictions
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, // The scope
'is_configurable' => false, // Is it a configurable attribute?
'is_html_allowed_on_front' => false, // Is HTML allowed on front?
//'note' => '', // Maybe a simple note below the input field in admin
'searchable' => false, // Is it searchable?
'sort_order' => '', // Which position on the admin page?
'type' => 'varchar', // The attribute type (varchar, text, int...)
'unique' => false, // Is it unique?
'used_for_sort_by' => false, // Is it used for the 'sort by' select on a catalog/search page?
'used_in_product_listing' => true, // In Flat?
'user_defined' => false, // Is it user defined? If false the attribute isn't removable.
'visible' => true, // Is it visible? If true the field appears in admin product page.
'visible_on_front' => true, // Visible on front?
'visible_in_advanced_search' => false, // Is it visible on advanced search?
'wysiwyg_enabled' => false, // Is Wywiwyg enable? (use 'editor' input if you put true ;))
));
<?xml version="1.0" encoding="utf-8" ?>
<config>
<!-- ... -->
<global>
<!-- ... -->
<catalog>
<product>
<type>
<foo translate="label">
<label>Custom Foo Product Type</label>
<model>catalog/product_type_simple</model>
<composite>0</composite>
<index_priority>10</index_priority>
</foo>
</type>
</product>
</catalog>
</global>
</config>
<?php
class Mage_Eav_Model_Entity_Setup extends Mage_Core_Model_Resource_Setup
{
/**
* Mage_Core_Model_Resource_Setup
*/
public function addConfigField($path, $label, array $data=array(), $default=null);
public function afterApplyAllUpdates();
public function applyDataUpdates();
public function applyUpdates();
public function callbackQueryHook(&$sql, &$bind);
public function deleteConfigData($path, $scope = null);
public function deleteTableRow($table, $idField, $id, $parentField = null, $parentId = 0);
public function endSetup();
public function getCallAfterApplyAllUpdates();
public function getConnection();
public function getFkName($priTableName, $priColumnName, $refTableName, $refColumnName);
public function getIdxName($tableName, $fields, $indexType = '');
public function getTable($tableName);
public function getTableRow($table, $idField, $id, $field=null, $parentField=null, $parentId=0);
public function run($sql);
public function setConfigData($path, $value, $scope = 'default', $scopeId = 0, $inherit=0);
public function setTable($tableName, $realTableName);
public function startSetup();
public function tableExists($table);
public function updateTable($table, $conditionExpr, $valueExpr);
public function updateTableRow($table, $idField, $id, $field, $value = null, $parentField = null, $parentId = 0);
/**
* Mage_Eav_Model_Entity_Setup
*/
public function addAttribute($entityTypeId, $code, array $attr);
public function addAttributeGroup($entityTypeId, $setId, $name, $sortOrder = null);
public function addAttributeOption($option);
public function addAttributeSet($entityTypeId, $name, $sortOrder = null);
public function addAttributeToGroup($entityType, $setId, $groupId, $attributeId, $sortOrder = null);
public function addAttributeToSet($entityTypeId, $setId, $groupId, $attributeId, $sortOrder=null);
public function addEntityType($code, array $params);
public function cleanCache();
public function createEntityTables($baseTableName, array $options = array());
public function getAllAttributeSetIds($entityTypeId = null);
public function getAttribute($entityTypeId, $id, $field = null);
public function getAttributeGroup($entityTypeId, $setId, $id, $field = null);
public function getAttributeGroupId($entityTypeId, $setId, $groupId);
public function getAttributeGroupSortOrder($entityTypeId, $setId, $sortOrder = null);
public function getAttributeId($entityTypeId, $id);
public function getAttributeSet($entityTypeId, $id, $field = null);
public function getAttributeSetId($entityTypeId, $setId);
public function getAttributeSetSortOrder($entityTypeId, $sortOrder = null);
public function getAttributeSortOrder($entityTypeId, $setId, $groupId, $sortOrder = null);
public function getAttributeTable($entityTypeId, $id);
public function getDefaultAttributeGroupId($entityType, $attributeSetId = null);
public function getDefaultAttributeSetId($entityType);
public function getEntityType($id, $field = null);
public function getEntityTypeId($entityTypeId);
public function installDefaultGroupIds();
public function installEntities($entities = null);
public function removeAttribute($entityTypeId, $code);
public function removeAttributeGroup($entityTypeId, $setId, $id);
public function removeAttributeSet($entityTypeId, $id);
public function removeEntityType($id);
public function setDefaultSetToEntityType($entityType, $attributeSet = 'Default');
public function updateAttribute($entityTypeId, $id, $field, $value = null, $sortOrder = null);
public function updateAttributeGroup($entityTypeId, $setId, $id, $field, $value = null);
public function updateAttributeSet($entityTypeId, $id, $field, $value = null);
public function updateEntityType($code, $field, $value = null);
}
<?xml version="1.0" encoding="utf-8" ?>
<config>
<!-- ... -->
<global>
<!-- ... -->
<catalog>
<product>
<type>
<foo translate="label">
<label>Custom Foo Product Type</label>
<model>jbh_catalog/product_type_foo</model>
<price_model>jbh_catalog/product_type_foo_price</price_model>
<composite>0</composite>
<index_priority>10</index_priority>
<price_indexer>jbh_catalog/product_indexer_price_foo</price_indexer>
<index_data_retreiver>jbh_catalog/catalogIndex_data_foo</index_data_retreiver>
</foo>
</type>
</product>
</catalog>
</global>
</config>
<?php
class Jbh_Catalog_Model_Resource_Product_Indexer_Price_Foo extends Mage_Catalog_Model_Resource_Product_Indexer_Price_Default
{}
<PRODUCT_TYPE_foo translate="label" module="catalog">
<label>Catalog Product View (Foo)</label>
<reference name="product.info">
<block type="catalog/product_view_type_simple" name="product.info.foo" as="product_type_data" template="catalog/product/view/type/default.phtml">
<block type="core/text_list" name="product.info.simple.extra" as="product_type_data_extra" translate="label">
<label>Product Extra Info</label>
</block>
</block>
</reference>
</PRODUCT_TYPE_foo>
<?php
class Jbh_Catalog_Model_Product_Type_Foo_Price extends Mage_Catalog_Model_Product_Type_Price
{}
<?php
class Jbh_Catalog_Model_CatalogIndex_Data_Foo extends Mage_CatalogIndex_Model_Data_Simple
{
/**
* Retreive product type code
* @return string
*/
public function getTypeCode()
{
return Jbh_Catalog_Model_Product_Type_Foo::TYPE_CODE;
}
}
<?php
/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */
$installer = $this;
// The Default attribute set
$defaultAttributeSetId = $installer->getAttributeSetId($installer->_defaultAttributeSetName);
// Add the new set
$newAttributeSetName = 'Bar';
$installer->addAttributeSet(Mage_Catalog_Model_Product::ENTITY, $newAttributeSetName);
$newAttributeSetId = $installer->getAttributeSetId(Mage_Catalog_Model_Product::ENTITY, $newAttributeSetName);
$model = Mage::getModel('eav/entity_attribute_set')->load($newAttributeSetId);
$model->initFromSkeleton($defaultAttributeSetId)->save();
// If you want a new group in your attribute set, use this :
//$newGroupName = 'Group Name';
//$installer->addAttributeGroup(Mage_Catalog_Model_Product::ENTITY, $newAttributeSetId, $newGroupName);
<?php
/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */
$installer = $this;
$installer->startSetup();
$attributes = array(
'price',
'special_price',
'special_from_date',
'special_to_date',
'minimal_price',
'cost',
'tier_price',
'weight',
'tax_class_id'
);
foreach ($attributes as $attribute) {
$applyTo = explode(',', $installer->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attribute, 'apply_to'));
if (!in_array('foo', $applyTo)) {
$applyTo[] = 'foo';
$installer->updateAttribute(Mage_Catalog_Model_Product::ENTITY, $attribute, 'apply_to', join(',', $applyTo));
}
}
$installer->endSetup();
<?php
class Jbh_Catalog_Model_Product_Type_Foo extends Mage_Catalog_Model_Product_Type_Simple
{
/**
* Product type code
* @const string
*/
const TYPE_CODE = 'foo';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment