Monday, July 28, 2014

Creating simple Magento product Programmatically

In the development process, you often will need some testing data you should use. Magento supplies you featuring a default Sample Data which contains some products. Thing is, they’re not regarding much use for those who have some custom attributes combined with your products, if you create your individual attribute set, or a item type. For this, you’ll need to add some products by yourself.

Sometimes, doing so inside administration is ample, but it gets painful when you require to add multiple products, or if you’re nonetheless developing SugarCRM London feature, and making changes for the fly.
You can call this via an observer, set is as a controller action, or cheat along with call it from a view file, whichever you think is the most suitable. Just pay attention, that if people observe some celebration, don’t use those who get called in every magento consultants steps. Or, if you perform, be sure to evaluate if the product while using the same SKU previously exists, by loading product’s ID by SKU before the attempt to create it. For this, you can make use of the ‘if’ check in lines 4 and 49 inside code example down below.

We’re setting this product in ‘Default Values‘ setting. You can uncomment the code on line 5 to fixed product data for any specific store. For example, you could make this happen. The code previously mentioned will set different meta values available with an USERNAME of 2.
You can find all of the values it is possible to set for the merchandise by logging this post data in product save. I’m thinking of saveAction inside the ProductController that will get called from official current administration when you’re saving a program.
Just add a final line to this file. Be sure for you to delete it when you’re done building, though. This will get you an array of data you can save. For example pertaining to setting meta_title, you’ll use any magic method setMetaTitle(‘somevalue’).
This problem can be easily solved by building products programmatically. I’ll show you a sample on how to provide a simple product by doing this. You could modify this code a bit, and make it work for other product types, or even use it as an significance script.

1 comment:

  1. Magento extension development is technical and quite tricky but if done by a professional it is the best platform for online stores.