Visibility of new attribute created by Magento upgrade script - magento

I am trying to add a custom attribute programmatically within a module upgrade script. The script runs fine and creates the new attribute (i.e. it appears in the Magento admin list under Catalog->Attributes->Manage Attributes once the script has run).
At first I was using the class Mage_Eav_Model_Entity_Setup (as recommended here and neither the 'visible' nor 'apply_to' fields were being set as intended ('visible' was always false and 'apply-to' remained as "All product types" rather than using the list supplied in the script).
Then I found this, which explained that I should use Mage_Catalog_Model_Resource_Setup instead, and that has fixed the problem with 'apply_to'.
But still I can't get the attribute's 'visible' attribute to set to true. If anyone has any ideas why the 'visible' attribute is still not being set as it should I would be very grateful to hear, thanks!
Here is my upgrade script code:
$updater = $this; // $this is class Mage_Eav_Model_Entity_Setup
$updater->startSetup();
$updater->addAttribute('catalog_product', 'my_test_attribute', array(
'label' => 'My Test Attribute',
'type' => 'int',
'input' => 'select',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'apply_to' => 'simple,configurable',
'group' => 'General',
'visible' => true,
'required' => true,
'user_defined' => true,
));
$updater->endSetup();
I am running Magento 1.7.0.1 in WAMP on Windows 7.

I have solved this now - what it needed was the "visible_on_front" attribute to be set too, rather than just the "visible". i.e. I added this line to the above script and it now works:
'visible_on_front' => true,

This attribute isn't Boolean, it's integer. So you have set up 1 = true 0 = false;

Related

How to create a new tab in products in Magento admin panel

I just started to work with Magento. Cool thing. But I think I jumped in the middle of it so fast. However, I've been asked to create a new tab called Promotions when adding a Product. It will then have an option field (named "Is Featured") and it has values of "Yes" or "No" in form of a dropdown.
I am familiar with the structure of Magento, but I can't find where I can do the changes.
I'm using Magento 1.9.2.3 Full release.
I made an image for you guys to understand what I want better.
What should I do?!
You will have to first create the attribute:
open Catalog > Attributes > Manage Attributes
click on Add New Attribute
create a new boolean attribute (chose Yes/No in "Catalog Input Type for Store Owner") and fill the other fields
Then add the attribute to the Promotions group:
open Catalog > Attributes > Manage Attribute Sets
chose the attribute set you are going to use for this product
add a new "Group" to the column on the left with the Add New button (in this case, name it "Promotions")
drag and drop the newly created group to the position you'd like it to be
drag and drop the newly created attribute from the right column to the left one, under the Promotions group
In the installer of any of your suitable extension, add an attribute to the product with a new group, and they will be displayed there.
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'is_featured',
array(
'group' => 'Promotions',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Is Featured',
'input' => 'select',
'class' => '',
'source' => 'eav/entity_attribute_source_boolean',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'sort_order' => 60
));
$installer->endSetup();
It will look like that:
https://gyazo.com/9a0e423d0c3e78e7c440b5cd79a3e547
Tweak attribute settings according to your requirements.

Magento creating custom attribute with custom validations

I need to create an attribute. Also i need to validate that attribute value. So i created a new .js file and add some functions. Then in setup file, i call the function name. But after creating the attribute that validation class wont come with the field.
$installer->addAttribute(MageTest_Module_Model_Name::ENTITY, 'test_value', array(
'input' => 'text',
'type' => 'text',
'label' => 'Test Value',
'backend' => '',
'user_defined' => false,
'visible' => 1,
'required' => 0,
'position' => 60,
'class' => 'validate-testValue',
'note' => 'This should contains 2 digits. Example 00',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
'validate-testValue' is my js function name. Can anyone help me to solve this please.
Thank You.
to use Magento form validator you have to register your method with the Validator object.
Try something like that in your custom js file
Validation.add('validate-testValue','HERE your error message if the field is not valid',function(fieldValue) {
return Validation.get('IsEmpty').test(fieldValue) || fieldValue == "testValue";
});
Ok, this example will only validate if your field value is "testValue", not very usefull.
But you can adapt it I guess :)

Magento Error when Disable module

I created a module then use upgrade script to add a multiselect attribute. the attribute is using 'source' to get it values dynamically. the code is the following:
Add Attribute:
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->startSetup();
$productEntityId = $installer->getEntityTypeId('catalog_product');
$allAttributeSetIds = $installer->getAllAttributeSetIds($productEntityId);
$installer->addAttribute('catalog_product', 'badge',array(
'label' => 'Badge',
'type' => 'varchar',
'input' => 'multiselect',
'backend' => 'eav/entity_attribute_backend_array',
'frontend' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'source' => 'module/entity_attribute_source_superbadge_config',
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false ));
$attributeId= $installer->getAttributeId($productEntityId, 'badge');
//add to General Group of all attribute sets
foreach($allAttributeSetIds as $attributeSetId) {
$installer->addAttributeToSet($productEntityId, $attributeSetId, 'General', $attributeId);
}
$installer->endSetup();
The Source is:
class Module_Model_Entity_Attribute_Source_Superbadge_Config extends Mage_Eav_Model_Entity_Attribute_Source_Boolean
{
/**
* Retrieve all attribute options
*
* #return array
*/
public function getAllOptions()
{
if (!$this->_options) {
$superbadge = array();
$badges = Mage::getModel('module/rule')->getCollection()->getSuperBadge();
foreach ($badges as $badge){
$superbadge[] = array('label' => $badge->getName(),
'value' => $badge->getId());
}
$this->_options = $superbadge;
}
return $this->_options;
}
}
The code is working fine am able to retrieve the value dynamically but the problem when the module is disable it is throwing an error directory could not found when am creating a new product in admin.
error:
Warning: include(Mage\Module\Model\Entity\Attribute\Source\Superbadge\Config.php) [function.include]: failed to open stream: No such file or directory in C:\Sites\project\development\lib\Varien\Autoload.php on line 93
Is there a way to prevent this error when the module is disable? i dont want to do uninstall as i will lost all data in my db. Thanks for any guide or help you may provide me..
The issue is because it is already save in db - eav attribute table.
One solution that i implemented is to add a button using system xml for the module. add a script to empty the source model field in the database when click the button.
click on the button everytime you need to disable the module.
the more important is to add one more button to add the source model in the database when you want to enable the module.
hope this solution will help someone who come accross this issue.
First of all did you flush the cache after you disabled the module?
Or maybe it's an compilation error? Try this out.
Try to track down where exactly the issue is coming up with a call of mageDebugBacktrace() in /lib/Varien/Autoload.php on line 93.
Let me know if some of the above worked for you!

Adding custom product attributes in Magento using setup script

I am using module setup script to add new attributes group, attribute set and attributes. I am able to create attribute set, attribute group and add products to group/set. But I am having hard time setting is_filterable, is_visible, is_visible_on_front and is_html_allowed_on_front parameters.
$installer->addAttribute('catalog_product', 'offer_type', array(
'backend' => '',
'frontend' => '',
'class' => '',
'default' => '',
'label' => 'Offer type',
'input' => 'text',
'type' => 'int',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => 1,
'required' => 1,
'searchable' => 0,
'filterable' => 1,
'unique' => 0,
'comparable' => 0,
'visible_on_front' => 1,
'is_html_allowed_on_front' => 1,
'user_defined' => 1,
));
$installer->addAttributeToSet('catalog_product', $sSetId, $groupName, 'offer_type');
I see offer_type getting added to Magento and to attribute set($sSetID) and to group ($groupname). Though when I look at attribute from magento admin UI (Catalog->attributes->Manage Attributes), I see is_filterable, is_visible, is_visible_on_front and is_html_allowed_on_front parameters set to No. I have tried various combinations but no luck. I'm using Magento CE 1.7.0.2. I am not sure what is missing in my setup script. I have reffered http://blog.chapagain.com.np/magento-adding-attribute-from-mysql-setup-file/ for this. Am I missing anything?
Thanks in advance.
Do you have properly configured your installer in your config.xml ? The standard class for magento installers is Mage_Eav_Model_Entity_Setup but when dealing with products, you'll need to use Mage_Catalog_Model_Resource_Setup instead.
Why ? look at their method _prepareValues() and you'll understand what are the authorised attributes (products have more options than the standard eav_objects, you can see that when comparing the tables eav_attribute and catalog_eav_attribute)
To point to the good installer class, take a look at the standard Mage_Catalog config.xml and adapt it for your module :
<resources>
<catalog_setup>
<setup>
<module>Mage_Catalog</module>
<class>Mage_Catalog_Model_Resource_Setup</class><!-- that line !-->
</setup>
</catalog_setup>
</resources>
ps: note that the _prepareValues() method is called only when adding an attribute... if you want to update an attribute you'll need to use the full option name ("is_visible" and not just "visible")...
Another hack would be to add these attributes afterward, but it's not very beautiful:
// adding atribute :
// [...]
//getting the new attribute with full informations
$eavConfig = Mage::getSingleton('eav/config');
$installer->cleanCache();
$attribute = $eavConfig->getAttribute('catalog_product', $attributeCode);
$attribute->addData(array(
'is_visible' => 1
));
$attribute->save()
Use 'visible_on_front' => 1, in addAttribute call.

Magento module setup/installer script

I'm trying to setup attribute-sets and attributes automatically via a setup script. The script is working and all attributes are added to the sets, no problem with that... however, when I look at the attributes the visible_on_front, the used_in_product_listing and the global are not set properly. This is what I have:
$installer->addAttribute('catalog_product', '<attribute_code>', array(
'group' => 'General',
'input' => 'date',
'type' => 'datetime',
'label' => '<some_label>',
'backend' => 'eav/entity_attribute_backend_datetime',
'is_global' => 0,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
'is_visible_on_front' => 1,
'visible_on_front' => 1,
'used_in_product_listing' => 1,
));
Anyone know how I can fix this so it works?
The trick here is to make sure that you are using the correct Setup object. The default Setup object is Mage_Eav_Model_Entity_Setup which will add your attribute into eav_attribute table but it is not aware of the extra fields in catalog_eav_attribute such as used_in_product_listing (or customer_eav_attribute and it's fields for that matter).
So, add this at the top of the install script:
$installer = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$installer->startSetup();
That should make the difference.
FYI, you can use Mage_Customer_Model_Entity_Setup to achieve the same end for customer attributes.

Resources