Magento module setup/installer script - magento

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.

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

Magento - saving custom column to order object

I am making some amendments to an Magento eCommerce website - one of the options at the checkout page is for a user to select their organisation from a dropdown and save this 'organisation_id' to the Magento order object & the customer object.
Would this be best off using an observer or is there another way this should be achieved?
My Magento knowledge is limited so please feel free to point out anything obvious that I have missed out
Can anyone suggest any tips/code to get this started?
UPDATE... i found this on another Stackoverflow post
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('order', 'organisation_id', array(
'position' => 1,
'input' => 'text',
'type' => 'varchar',
'label' => 'Organisation ID',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'global' => 1,
'visible_on_front' => 1,
));
$installer->endSetup();
Would this create the database attributes for the organisation_id?

Visibility of new attribute created by Magento upgrade script

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;

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.

Change category attribute within installation script

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();

Resources