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?
Related
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
I’m trying to precomplete custom options during the process of creating or editing a product in the Magento admin : after product type choice.
I add an event trigger on the Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs _prepareLayout function. So basically when you open the edition or creation of a product, the event is triggered.
My aim is : when you start editing your new product, 3 custom options are already present in the “Custom Options” tab.
I can’t trigger it with the catalog_product_save_before event because one option is a dropdown type and have to be filled by the admin.
So i’ve coded my observer and succeeded to modify the product name with setName() function, but I can"t find how to precomplete/add custom options.
I tried with the code of the following blog : http://kamal250.wordpress.com/2012/10/22/create-custom-option-programatically-while-creating-product/#comments
But it doesn’t seems to work.
Anyone can help me with that ?
Here is my code in the observer :
$option_data = array(
'is_delete' => 0,
'is_require' => true,
'previous_group' => '',
'title' => 'Height',
'type' => 'field',
'price_type' => 'fixed',
'price' => '0',
'sort_order' => 1,
'values' => array()
);
$product->setHasOptions(1);
$product->setCanSaveCustomOptions(1);
$product->setOptions(array($option_data));
$product->setProductOptions(array($option_data));
$opt = Mage::getSingleton('catalog/product_option');
$opt->setProduct($product);
$opt->addOption($option_data);
$opt->saveOptions();
$product->setOption($opt);
Finally i have found the solution :
I modified the trigger to the action of opening the custom options tab.
And here is my code in the observer :
$product = $observer->getProduct();
$option_data = array(
'is_delete' => 0,
'is_require' => true,
'previous_group' => '',
'title' => 'Height',
'type' => 'field',
'price_type' => 'fixed',
'price' => '0',
'sort_order' => 1
);
$product->setHasOptions(1);
$option = Mage::getModel('catalog/product_option')->setProductId($product->getId())->setStoreId($product->getStoreId())->addData($option_data);
$option->save();
$product->addOption($option);
Hope it will help someone sooner or later.
See ya.
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.
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();
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.