How to make custom attribute comparable(script) - magento

Code in my Setup.php
'compare_field_freegift' =>
array (
'group' => 'Gift Product',
'label' => 'Gift Inside',
'type' => 'int',
'note' => 'Comparing product field(Yes/No)',
'input' => 'boolean',
'global'=> Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => 1,
'required' => 1,
'user_defined' => 0,
'searchable' => 0,
'filterable' => 0,
'comparable' => 1,//Comparable on Front-end
'apply_to' => 'simple,configurable,downloadable',
'visible_on_front' => 1,
'visible_in_advanced_search' => 0,
'unique' => 0
)
All things are working fine except making field 'comparable'->True.
I tried with 'true','1' and 1 also but it is not coming.

Related

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' => ''
]
);

Does Magento supports attribute with type date, not datetime

I have problem with this format and I don't know does magento support it.
In code I have:
$this->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'release_date', array(
'input' => 'date',
'type' => 'datetime',
'label' => 'Release date',
'backend' => 'eav/entity_attribute_backend_datetime',
'visible' => true,
'required' => false,
'user_defined' => true,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
));
How to change type to date? And how to make it dependable from Magento locale?
Here solution....
$data = array(
"attribute_code" => "date_attribute_3",
"frontend_input" => "date",
"default_value" => "06/19/2014",
"is_unique" => 0,
"is_global"=>"1",
"is_required" => 0,
"apply_to" => array("simple"),
"is_configurable" => 0,
"is_searchable" => 0,
"is_visible_in_advanced_search" => 0,
"is_comparable" => 0,
//"is_global" =>1,
"is_used_for_promo_rules" => 0,
"is_visible_on_front" => 0,
"used_in_product_listing" => 0,
"additional_fields" => array(),
"frontend_label" => array(array("store_id" => "0", "label" => "my date"))
);
$Attribute=Mage::getModel('catalog/product_attribute_api')->create($data);

Magento : set value from config to default attribute value

on the one hand i have field called min pricecf in my module configuration
on the other hand i have an attribute called min_price that i've created unsing install script
this is the attribute :
$setup->addAttribute('catalog_product', 'min_price', array(
'group' => 'Prices',
'input' => 'text',
'type' => 'decimal',
'label' => 'Min Price',
'backend' => '',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'searchable' => 1,
'filterable' => 0,
'default' => 'Here ????',
'comparable' => 1,
'visible_on_front' => 1,
'visible_in_advanced_search' => 0,
'is_html_allowed_on_front' => 0,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
what i wanna do is get the value from min pricecf to min_price and that value should be the default.

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,
));
...

Magento - attribute "input-type" checkbox

I have seen a similar question but I did not find an answer there :
How can I get my custom backend Magento Customer Checkbox Attribute to save my selection?
$installer->addAttribute('catalog_product', 'tip', array(
'group' => 'ISM',
'input' => 'checkbox',
'type' => 'int',
'label' => 'TIP',
'visible' => 1,
'source' => 'eav/entity_attribute_source_boolean',
'required' => 0,
'user_defined' => 1,
'searchable' => 0,
'filterable' => 0,
'comparable' => 0,
'visible_on_front' => 0,
'visible_in_advanced_search' => 0,
'is_html_allowed_on_front' => 0,
'global' => 1,
'used_in_product_listing' => 0,
'is_html_allowed_on_front' => 1
));
I have created an attribute, but it doesn't save the value when I edit-save or create-save the product.
Use 'input' as int because you always store the value of the checkbox '0' or '1'. And for source you are already using core options boolean that is 0 or 1.

Resources