March, 2016
Archive

By In Magento

Create product attribute programmatically

Create product attribute is common operation in Magento, it can be successfully done from Magento back-end without any issues. In case that you already have loaded store with lot of attribute sets and products it can be really time consuming to add attribute to every attribute sets. If there are only few attributes sets that can be done manually, however if there are 10 or even more attribute sets operation could last quite a bit.

I found a small workaround in form of simple PHP script that can be used easily to create product attribute and assign it to all attribute sets (or only to attribute sets that you want).

Create a PHP file, we will name it ‘install-attribute.php’ and copy code from bellow into it (don’t forget PHP start/end tags):

require_once 'app/Mage.php';
Mage::app();

// Attribute basic data
$attributeName = ‘Product Custon Attribute’; // Name of the attribute
$attributeCode = ‘product_custom_attribute’; // Code of the attribute
$attributeGroup = ‘Custom Group’; // Group to add the attribute to
// Attribute configuration data
$data = array(
‘type’ => ‘varchar’, // Attribute type
‘input’ => ‘text’, // Input type
‘global’ => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // Attribute scope
‘required’ => false, // Is this attribute required?
‘user_defined’ => false,
‘searchable’ => false,
‘filterable’ => false,
‘comparable’ => false,
‘visible_on_front’ => false,
‘unique’ => false,
‘used_in_product_listing’ => true,
// Adding basic data
‘label’ => $attributeName
);

// Installer code to create attribute with new installer class (can be used in non-EAV setup)
$installer = Mage::getResourceModel(‘catalog/setup’, ‘catalog_setup’);
$installer->startSetup();
$installer->addAttribute(‘catalog_product’, $attributeCode, $data);

// Notice: Default Attribute Set have ID 4

// Add the attribute to the all attribute proper sets and proper groups:
$entity = Mage_Catalog_Model_Product::ENTITY;
$attributeSetIds = $installer->getAllAttributeSetIds($entity);
foreach($attributeSetIds as $attributeSetId)
{
$installer->addAttributeToGroup(‘catalog_product’, $attributeSetId, $attributeGroup, $attributeCode);
}

$installer->endSetup();

Comments in code explain every section clearly, so we will run through it quickly:
– first part we define all attribute parameters
– second part is installer action
– finally in third part we assign attribute to attribute sets

This is just a example and code should be modified according to your needs, also it is important to create a proper backup of data base before running script. In general nothing bad could happen but it might happen that you are not satisfied with created attribute and sometimes it is easier to restore backup than to erase attribute.

Read more

By In Magento

Magento Porto theme with ExtensionsMall v5 Color Swatch

If you plan to use Porto Theme (from SmartWave company) on your Magento store and also want to have swatches than only two modules can be really considered. One is Magento built-in swatches module and other is ExtensionsMall Color Swatch (latest version is 5.0), both work very well with theme however Emall module comes with few nice tricks. Installation of module is quite straight forward and only thing that you should pay attention is where you copy files (instead default or RWD folder, you want to copy files into smartwave/porto folder). Module works very well on catalog pages, no matter did you enabled AJAX features like ‘price slider’ or ‘infinite scroll’. With a bit knowledge of Magento it is possible to integrate swatches in all product list blocks such as ‘Featured products’, ‘New products’ and other.

If you are building a store from scratch you can save some time if you during installation of theme skip patch for Magento built in swatches, that way you will avoid any conflict with ‘ExtensionsMall’ Color Swatch module. Also if you have installed theme you can order module and ask developers to prepare modified module for Porto theme.

Porto theme - color swatch

Porto theme – color swatch

Notice from ExtensionsMall:
All customers that have previous version of Color Swatch (version 3 or 4) can upgrade module to latest version (version 5) and keep all settings, uploaded swatches and settings on product level will stay untouched after upgrade. Color Swatch 5 bring lot of new features and can’t be really compared with Magento built-in swatches module.
Extensionsmall Color Swatch module page (there is documentation in PDF format):
http://www.extensionsmall.com/magento-color-swatch.html

there is also a demo site availble:
http://demo.extensionsmall.com/index.php/color-swatch-363.html

Read more