Our Work

Adding custom fields to Magento Product/Attribute Set

Updated 1 year, 8 months and 21 day ago

We have done a SaaS project where we need to sync Products, Attribute sets, Attributes, Product Categories etc from Magento to this SaaS product and vice versa. For this article purpose let's call the Magento Objects (Products,Customers….) as subjects.Once a sync is established between this SaaS instance and Magento Instance, any new creation/deletion/updation will be synced. The changes of subjects or new subject information are stored in tables wherever it's triggered to avoid delay for those subject saves, some of the subjects are synced on the fly though. The ETL(Talend) is responsible for reading data from those sync table and syncing data from Magento instances to SaaS instance and vice versa…

To track and for other purposes we have added lot of new fields to these subjects.

To achieve this functionality we have used Magento Event-Observer Pattern.If you are not clear please read our Magento Event-Observer Pattern Article ... In this article we are going to show how to add new field and how to save data to this new fields when the subject is saved..

AttributeSets :

Created New Form Field sync Status In Attributes Form by extending the Default Attributeset formset.

$eventElem= $fieldset->addField('sync_attr_set_create', 'select', array(
'label' => Mage::helper('catalog')->__('Create/Update Attribute Set XYZ),
'name' => 'sync_attr_set_create',
'required' => true,
'id'=>'sync_attr_set_create',
'class' => 'required-entry',
'values' => $yesnoSource,
//'value' => $data->getSyncAttrSetCreate()
'value' => $mbizEavAttributeSet->getSyncStatus()
));

We are creating sync record based on above field value.  If yes we are creating sync Record by triggering Attributeset aftersave event eav_entity_attribute_set_save_commit_after.

<eav_entity_attribute_set_save_commit_after>
<observers>
<eav_entity_attribute_set_save_commit_after>
<type>singleton</type>
<class>Microbiz_Connector_Model_Observer</class>
<method>onAttributeSetSave</method>
</eav_entity_attribute_set_save_commit_after>
</observers>
</eav_entity_attribute_set_save_commit_after>

In onAttributeSetSave() method  we are checking sync status of AttributeSet is enabled or not,  if enabled we are getting all the information related to AttributeSet(Groups,Attributes and Options)  and inserting into sync tables.

Products :

Added following Product attributes  sync_status(Yes/no attribute), store_price (Price type attribute).

Using two observers catalog_product_save_before   and catalog_product_save_commit_after

In Before save event we are updating the store_price from price if it is not set for that product.

<catalog_product_save_before>
<observers>
<connector>
<type>singleton</type>
<class>KTree_Model_Product_Observer</class>
<method>onBeforeSave</method>
</connector>
</observers>
</catalog_product_save_before>

In After save event we are checking the Sync status of the product, if sync status is yes calling the background job for saving the product and returning the control to user.

<catalog_product_save_commit_after>
<observers>
<connector>
<type>singleton</type>
<class>KTree_Model_Product_Observer</class>
<method>KTreeOnProductSave</method>
</connector>
</observers>
</catalog_product_save_commit_after>

In Background job  we are checking for the product relation,  if relation is exists we are saving only updated information related to the product otherwise saving the product total information into sync tables.

Mage::helper('KTree/ERunActions')->touchUrlExt($url);

 

Related Posts