Pergunta

I install the attribute with the following script:

$installer = $this;
$installer->startSetup();

$installer->removeAttribute('catalog_product', 'customizableonly');
$installer->addAttribute('catalog_product', 'customizableonly', array(
        'group'                     => 'General',
        'input'                     => 'select',
        'type'                      => 'int',
        'label'                     => 'Customizable Only',
        'source'                    => 'eav/entity_attribute_source_boolean',
        'global'                    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible'                   => 1,
        'required'                  => 0,
        'visible_on_front'          => 0,
        'is_html_allowed_on_front'  => 0,
        'is_configurable'           => 0,
        'searchable'                => 0,
        'filterable'                => 0,
        'comparable'                => 0,
        'unique'                    => false,
        'user_defined'              => false,
        'default'           => 0,
        'is_user_defined'           => false,
        'used_in_product_listing'   => true
));

$this->endSetup();

Also tried with $installer = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');

And then I am using the value of the attribute in some other code. But I always get null. I found out that, the attribute does not get set a default value. When I open a product - the dropdown shows No, but when I get its value in code it's null. If I just click the dropdown, simply set No and save the product - everything works.

How to overcome this?

Foi útil?

Solução

Try to set default value as string

'default' => '0'

or empty

'default' => ''

Update

The default values are added when you add new product for old ones it not affects.

Try to fix that in Product management with mass action

Inside manage products, there is a action called “Update Attributes”. Select all the products that you want to update and then select Update Attributes and add all the new information in.

Outras dicas

You should set value for all existing entity manually:

$productIds = Mage::getResourceModel('catalog/product_collection')
    ->getAllIds();

// Now create an array of attribute_code => values
$attributeData = array("my_attribute_code" =>"my_attribute_value");

// Set the store to affect. I used admin to change all default values
$storeId = 0; 

// Now update the attribute for the given products.
Mage::getSingleton('catalog/product_action')
    ->updateAttributes($productIds, $attributeData, $storeId);

source: https://stackoverflow.com/questions/4906497/default-attribute-value-for-all-product-in-magento. See Asrar Malik's answer.

I had the problem that with the code snippets above a select-attribute was created instead of a yes/no attribute. To fix this I had to use

'input'             => 'boolean'

instead of:

'input'             => 'select'

I was not able to add a default value 0 to a yes/no attribute too.

Therefore I used an event to add the default value 0

<frontend>
    <events>
        <customer_save_before>
            <observers>
                <xx_save_observer>
                    <type>singleton</type>
                    <class>xx/observer</class>
                    <method>customerSaveBefore</method>
                </xx_save_observer>
            </observers>
        </customer_save_before>
    </events>
</frontend>

Method:

public function customerSaveBefore(Varien_Event_Observer $observer)
{
    try {
        $customer = $observer->getCustomer();
        if (!$customer->getYourCustomAttribute()) {
            $customer->setYourCustomAttribute(0);
        }
    } catch ( Exception $e ) {
        Mage::log( "customer_save_before observer failed: ".$e->getMessage());
    }
}

For adding yes/no custom attribute to magento create module as shown below.

http://www.pearlbells.co.uk/how-to-add-custom-attribute-dropdown-to-category-section-magento/

    <?php
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'featured_product', array(
    'group'         => 'General Information',
    'input'         => 'select',
    'type'          => 'text',
    'label'         => 'Featured Product',
    'backend'       => '',
    'visible'       => true,
    'required'      => false,
    'visible_on_front' => true,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'source' => 'eav/entity_attribute_source_boolean',
));

$this->endSetup();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top