Change category attribute within installation script - magento

I have my module. This module has installation script where should be add custom image field to categories.
$setup->addAttribute('catalog_category', 'additional_image', array(
'type' => 'varchar',
'backend' => 'catalog/category_attribute_backend_image',
'label' => 'Additional Image',
'input' => 'image',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => 1,
'required' => 0,
'user_defined' => 0,
'default' => '',
'position' => 6,
));
After that it must change captions other image fields (Image, Thumbnail). How I can get this system's fields and change their?

You can do this using Mage_Eav_Model_Entity_Setup::updateAttribute() method.

This is a long way off but someone else may need the information.
Mage_Eav_Model_Entity_Setup::updateAttribute()
has 5 arguments, 3 of which are necessary.
I am going to use the example of a custom customer attribute:
$entityTypeId = 'customer'
$id = 'my_custom_attribute_code'
$field = 'is_used_for_customer_segment'
$value = '1'
$sortOrder = Not Needed
So as you can see I am using the customer entity to update the attribute. I am updating my custom attribute with attribute id (code) my_custom_attribute_code. The field in this attribute that I am udpating is the is_used_for_customer_segment and setting the value to yes(1).
Here is an example of how to do this as an update.
$installer->startSetup();
$installer->updateAttribute('customer', 'my_custom_attribute_code', 'is_used_for_customer_segment', '1');
$installer->endSetup();

Related

Save custom attribute to order object at Checkout in Magento - How can I confirm this?

I've been having trouble with this for a few days, essentially I need to add a custom organisation_id attribute (that I have created via an installer script) to be added to both the customer object and order object at the end of the onepage checkout.
The installer script works fine (I can see the values within the eav_attribute and core_resources table in the database.
During the checkout phase on the final 'Order Review' section, once the Place Order button is clicked this executes the following observer:
sales_order_place_after
Once this has been executed it runs the following function within an Observer:
public function afterOrderPlaced($observer)
{
// this id below comes from a select dropdown within the checkout onepage & already saved to session
$organisation_id = Mage::getSingleton('customer/session')->getCustomerOrganisationId();
$this->_order = $observer->getEvent()->getOrder();
$this->_order->setOrganisationId($organisation_id)->save(); // e.g 25621
// Customer stuff
$this->_customer_id = $this->_order->getCustomerId();
$this->_customer = $this->_order->getCustomer();
$this->_customer->setOrganisationId($organisation_id)->save(); // e.g 25621
}
In this instance, I simply want to set the organisationId value on both the customer and order objects to '25621'.
Once I have completed the checkout process and hit the 'order confirmation' page I'd like to be able to confirm this organisation_id has indeed been correctly added to the customer and order object, can anyone confirm the easiest/best way to do this? I presume this will need to include some modification to the admin html for Sales to include this information as well.
Please note - My Magento skillset is rather limited
My Installer Script below:
$installer = new Mage_Eav_Model_Entity_Setup();
$installer->startSetup();
$installer = new Mage_Eav_Model_Entity_Setup();
$installer->startSetup();
$installer->addAttribute('customer', 'organisation_id', array(
'input' => 'select', //or select or whatever you like
'type' => 'int', //or varchar or anything you want it
'label' => 'Organisation ID',
'visible' => 1,
'required' => 0, //mandatory? then 1
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$installer->addAttribute('quote', 'organisation_id', array(
'input' => 'select', //or select or whatever you like
'type' => 'int', //or varchar or anything you want it
'label' => 'Organisation ID',
'visible' => 1,
'required' => 0, //mandatory? then 1
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$installer->addAttribute('order', 'organisation_id', array(
'input' => 'select', //or select or whatever you like
'type' => 'int', //or varchar or anything you want it
'label' => 'Organisation ID',
'visible' => 1,
'required' => 0, //mandatory? then 1
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$installer->endSetup();
You can check your custom value in your database sales_flat_order_address table and sales_flat_quote_address table .
Let me know if you have any query

Add drop down list attribute to category - set source

I added "brand" attribute (drop down list) for products.
I'm trying add new attribute (drop down list) to category with values from "brand" attribute.
What should I do to set correct source for this category attribute.
Please see my piece of code in mysql setup file:
$this->startSetup();
$this->addAttribute('catalog_category', 'brand', array(
'group' => 'General',
'type' => 'int'
'backend' => '',
'frontend_input' => '',
'frontend' => '',
'label' => 'brand',
'input' => 'select'
'class' => '',
'source' => 'mymodule/selecattributes',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'frontend_class' => '',
'required' => false,
'user_defined' => true,
'default' => '',
'position' => 100,
));
$this->endSetup();
Thanks in advance.
EDIT 1
I added class MyPackage_MyModule_Model_SelectAttributes extends extends Mage_Eav_Model_Entity_Attribute_Source_Abstract :
class MyPackage_MyModule_Model_SelectAttributes extends Mage_Eav_Model_Entity_Attribute_Source_Abstract{
public function getAllOptions()
{
$attributeCode = 'brand';
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attributeCode); //here, "color" is the attribute_code
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach ($allOptions as $instance) {
$myArray[$instance['value']] = $instance['label'];
}
return $myArray;
}
}
EDIT 2
When I open category admin page I get this error:
"Source model "mymodule/selectattributes" not found for attribute "brands""
I'm guessing that your source model is in a file named Selectattributes.php. If you are using a case-sensitive filesystem you will need to do one of two things:
Rename your class definition file to Selectattributes.php
or
Change the source_model value for your brands attribute to mymodule/selectAttributes
When Magento is attempting to instantiate the source model for your attribute, the classname (and hence the autoload include path) being calculated work as follows:
MyPackage_MyModule_Model_Selectattributes
MyPackage/MyModule/Model/Selectattributes.php
Note the issue with the filename. Note that this should be working in a non-case-sensitive filesystem.

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.

Magento - Adding a new field for shipping address during onepage checkout process

I want to add a new field "custom_house_no" for the shipping address during the onepage checkout process.
I have added the below code in my custom extension mysql file "mysql4-install-0.1.0.php"
// Customer Address
$entityTypeId = $installer->getEntityTypeId('customer_address');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttribute('customer_address', 'custom_house_no', array(
'label' => 'Custom House No',
'input' => 'text', // Input field type textbox
'type' => 'varchar', // Store varchar data type
'frontend' => '', //frontend model
'backend' => '', //backend model
'visible' => 1, //true
'required' => 0, //false
'user_defined' => 1,
'default' => '', //default value
'searchable' => 0,
'filterable' => 0,
'comparable' => 0,
'visible_on_front' => 0,
'unique' => 0,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
// 'class' => '',
// 'source' => 'catalog/category_attribute_source_page',
));
$installer->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
'custom_house_no',
'150' //last Magento's attribute position in General tab is 140
);
$attribute = Mage::getSingleton('eav/config')->getAttribute('customer_address', 'custom_house_no');
$attribute->setData('used_in_forms', array('customer_register_address', 'customer_address_edit', 'adminhtml_customer_address')); // Setting the relation between the attribute and forms in which this attribute will be used
$attribute->save();`
I have also made a class in folder MY/CustomExtension/Model/Entity/Setup.php
class MY_CustomExtension_Model_Entity_Setup extends Mage_Eav_Model_Entity_Setup {
}
I have also added the class name in the extension config file.
Link to config code : Config File Content
And in the shipping template file i have added the textbox with the name "custom_house_no".
The attribute has been added successfully and he relationship with the forms, but all the data is getting save in the database except the "custom_house_no" field.
Most likely you need to play with fieldsets. Take a look at those defined in config.xml of core Mage/Checkout module and extend them in your module config

Resources