How set a default Value with EAV AddAttribute - magento

I want to set up an new attribute-set to my products in magento. This attribute should be type of selection from some options.
$installer->addAttribute('catalog_product', 'reserve', array(
'backend_label' => 'Attribute Reserve',
'type' => 'varchar',
'input' => 'select',
#'backend' => 'eav/entity_attribute_source_boolean',
'frontend' => '',
'source' => '',
#'default' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false,
'option' => array(
'value' => array(
'optionone' => array( 'O' ),
'optiontwo' => array( 'P' ),
'optionthree' => array( 'Kein Angabe' ),
)
),
));
How can I set optionthree to default value?

Had the same problem. My solution:
$installer->addAttribute('catalog_product', 'reserve', array(
'backend_label' => 'Attribute Reserve',
'type' => 'int',
'input' => 'select',
#'backend' => 'eav/entity_attribute_source_boolean',
'frontend' => '',
'source' => 'eav/entity_attribute_source_table',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'visible_in_advanced_search' => false,
'option' => array(
'value' => array(
'optionone' => array( 'O' ),
'optiontwo' => array( 'P' ),
'optionthree' => array( 'Kein Angabe' ),
)
),
));
Notice the different type (int instead of varchar) and source (eav/entity_attribute_source_table). This is the way Magento represents typical select attributes. Now you can set the default value like this:
$model = Mage::getModel('eav/entity_attribute')
->load($installer->getAttributeId('catalog_product', 'reserve'));
$model
->setDefaultValue($model->getSource()->getOptionId('Keine Angabe'))
->save();

Please use this script:-
$installer->addAttribute('catalog_product', 'reserve', array(
'backend_label' => 'Attribute Reserve',
'type' => 'varchar',
'input' => 'select',
#'backend' => 'eav/entity_attribute_source_boolean',
'frontend' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'visible_in_advanced_search' => false,
'option' => array(
'value' => array(
'optionone' => array( 'O' ),
'optiontwo' => array( 'P' ),
'optionthree' => array( 'Kein Angabe' ),
)
),
/**
* This will set the default values,
* as "array" data type is being used to set proper default value
*/
'default' => array(
'optionthree'
),
));
Hope it helps.

Navigate to Catalog >Manage Attributes to create new attribute and manage attribue stes to create new attribute set.
Please check the screenshot

Related

Custom product attribute not showing in admin catalog section magento 2.1

I have added custom product attribute in Magento 2.1 and that product is showing in attribute section but couldn't be shown in magento catalog section where we have created products
Below are the code which I am using to create attribute.
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'test_author',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Test Author',
'input' => '',
'class' => '',
'source' => '',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => 0,
'searchable' => true,
'filterable' => true,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
You can try the following code -
/** #var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'test_author',
[
'group' => 'General',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Test Author',
'input' => 'textarea',
'class' => '',
'source' => '',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => 'simple,configurable,virtual,bundle,downloadable'
]
);

Magento - How to create attribute programmatically that can be used in 'Sort by'

I need to programmatically create several attributes.
Here is a part of my upgrade script :
$dataOrder = array(
'attribute_set' => 'Main',
'group' => 'Datawarehouse',
'type' => 'int',
'input' => 'text',
'label' => 'Total order quantity',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'required' => 0,
'comparable' => 0,
'searchable' => 0,
'unique' => 0,
'user_defined' => 1,
'visible_on_front' => 1,
'visible' => 1,
'is_filterable' => 1,
'used_for_sort_by' => 1,
'used_in_product_listing' => 1,
);
My script works fine, all my attributes are created but I can't see them in the Sort by dropdown (frontend). In back office I can see my attributes, I can assign a value, all good.
But under Catalog -> Manage Attributes -> Properties -> Frontend properties : Used for Sorting in Product Listing is set to 'No'.
I thought that used_for_sort_by and used_in_product_listing would be enough but looks like it's not.
How can I set it to yes, without having to change it in the back office? Either by adding some lines in my upgrade script or by adding some code somewhere else.
EDIT I just realized that it's not only Used for Sorting in Product Listing that is not updating the right way. Everything below required isn't updating the way it should be, everything is set to 'No'.
Most likely you are calling addAttribute() method on a deprecated class. Try:
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'attribute_id', $dataOrder);
I think this is will work fine. 'used_for_sort_by' => true should work.
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'sell_counts',
[
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Sell Count',
'input' => 'text',
'class' => '',
'source' => '',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => true,
'filterable' => true,
'comparable' => true,
'visible_on_front' => true,
'used_in_product_listing' => true,
'used_for_sort_by' => true,
'unique' => false,
'apply_to' => ''
]
);

Symfony 1.4 doctrine create table

Can someone show me an example on how to use the createTable in Doctrine?
For example, I'd like to create a table 'attachment' with the following columns:
'file_path' =>string
'message_id'=>integer
Thanks
Found it :
$this->createTable('attachment', array(
'id' =>
array(
'type' => 'integer',
'length' => 8,
'autoincrement' => true,
'primary' => true,
),
'file_path' =>
array(
'type' => 'string',
'notnull' => true,
'length' => 255,
),
'message_id' =>
array(
'type' => 'integer',
'notnull' => false,
'length' => 8,
),
'created_at' =>
array(
'notnull' => true,
'type' => 'timestamp',
'length' => 25,
),
'updated_at' =>
array(
'notnull' => true,
'type' => 'timestamp',
'length' => 25,
),
), array(
'indexes' =>
array(
),
'primary' =>
array(
0 => 'id',
),
'collate' => 'utf8_general_ci',
'charset' => 'utf8',
));

Magento 1.8: While adding new product attribute non of the frontend params are set to yes

This is code that I'm using to add new product attribute with frontend settings set to yes:
<?php
$installer = Mage::getResourceModel('catalog/setup','catalog_setup');
$installer->startSetup();
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, $specCode, array(
'group' => $profileGroupName,
'sort_order' => 1,
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => $specLabel,
'note' => $specNote,
'input' => 'text',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'required' => true,
'user_defined' => true,
'default' => '',
'unique' => false,
'used_for_promo_rules' => true,
'searchable' => true,
'filterable' => true,
'comparable' => true,
'visible' => true,
'visible_on_front' => true,
'visible_in_advanced_search' => true,
'is_configurable' => false
));
...
Almost all the frontend settings are set to true but after installing them in backend I can see that this settings are set to no.
Regards,
Fixed. Code that is working bellow. Just keep to the $attr array key names for this method _prepareValues in this two classes Mage_Eav_Model_Entity_Setup, Mage_Catalog_Model_Resource_Setup. Second class inherits form the first one so if you want to add frontend setting your installer needs to be an object from the second class.
$installer = new Mage_Catalog_Model_Resource_Setup();
$installer->startSetup();
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, $tradeCode, array(
'group' => $profileGroupName,
'sort_order' => 1,
'type' => 'varchar',
'input' => 'text',
'label' => $tradeLabel,
'note' => $tradeNote,
'required' => 1,
'unique' => 0,
'user_defined' => 1,
'default' => '',
# Additional attribute data - forntend
'frontend_input_renderer' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => 1,
'searchable' => 1,
'filterable' => 1,
'comparable' => 1,
'visible_on_front' => 1,
'wysiwyg_enabled' => 0,
'is_html_allowed_on_front' => 0,
'visible_in_advanced_search' => 1,
'filterable_in_search' => 1,
'used_in_product_listing' => 1,
'used_for_sort_by' => 1,
'apply_to' => '',
'position' => '',
'is_configurable' => 0,
'used_for_promo_rules' => 0,
));
...

Create a WYSIWGY field in magento backend

I am trying to create a WYSIWGY field in category page in magento backend but it doesn't seem to work. I am writing an install script as:
'fabric_and_care' => array(
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Fabric and Care Instructions',
'input' => 'textarea',
'class' => '',
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => 0,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'wysiwyg' => true,
'group' => 'general',
),
But it only shows the textarea but not the WYSIWGY editor. Any idea where I'm doing wrong?
Try this instead. Basically it's just 'wysiwyg' => true to 'wysiwyg_enabled' => true
'fabric_and_care' => array(
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Fabric and Care Instructions',
'input' => 'textarea',
'class' => '',
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => 0,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'wysiwyg_enabled' => true,
'group' => 'general',
),

Resources