Add image product attribute

This is how to get a new image attribute to a given attribute set inside of a setup script (add image attribute for products):

<?php
/**
 *
 * Date:          5.12.2017
 * Time:          0:58:0
 * Last modified: 0:57:28
 * Class / File:  mysql4-upgrade-0.5.2-0.5.3.php
 *
 *                Installs additional image attributes for customization
 *                preview on product detail page
 */

$oInstaller = $this;
/* @var $oInstaller Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */

$oInstaller->startSetup();

$iProductEntityTypeId = $oInstaller->getEntityTypeId('catalog_product');

$aEffectedProductTypes = [
    Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE
];

$aImageAttributeAttributes = [
    'group'                         => 'Images',
    'type'                          => 'varchar',
    'input'                         => 'media_image',
    'global'                        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'                       => true,
    'required'                      => false,
    'default'                       => null,
    'user_defined'                  => false,
    'apply_to'                      => implode(',', $aEffectedProductTypes),
    'visible_on_front'              => true,
    'used_in_product_listing'       => true,
    'is_comparable'                 => 0,
    'is_searchable'                 => 0,
    'is_visible_in_advanced_search' => 0,
    'is_used_for_promo_rules'       => 0,
    'used_for_sort_by'              => 0
];


//make it repeatable
$aAttributes = [
    'customization_example_1' => 'Customization Example 1',
    'customization_example_2' => 'Customization Example 2',
    'customization_example_3' => 'Customization Example 3'
];
$aAttributeSets = [
    'Fountain pen',
    'Rollerball pen',
    'Ballpoint pen',
    'Mechanical pencil',
    'Multisystem pen'
];

$iSort = 480; // start value for sorting

foreach ($aAttributes as $sAttributeCode => $sAttributeLabel) {
    $oInstaller->removeAttribute(
        $iProductEntityTypeId,
        $sAttributeCode
    );
    $aImageAttributeAttributes['label'] = $sAttributeLabel;
    $aImageAttributeAttributes['sort_order'] = $iSort;
    
    $oInstaller->addAttribute(
        $iProductEntityTypeId,
        $sAttributeCode,
        $aImageAttributeAttributes
    );
    foreach ($aAttributeSets as $sAttributeSet) {
        $oInstaller->addAttributeToSet(
            $iProductEntityTypeId,
            $sAttributeSet,
            $sAttributeGroup,
            $sAttributeCode,
            $iSort
        );
    }
    $iSort = $iSort + 10;
}

$oInstaller->endSetup();

 

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.