Magento Add product via script

Magento allows several different ways to add products to the catalog, including manual editing, upload via spreadsheet and the web services Magento Core API. For developers who want to add products through PHP however, there are a few caveats to be aware of for the product to be displayed on the frontend.

The basics of adding a new product are trivial – you get a new copy of the appropriate model, fill in the details and call the save() method:

$product = Mage::getModel('catalog/product');

$product->setSku("ABC123");
$product->setName("Type 7 Widget");
$product->setDescription("This widget will give you years of trouble-free widgeting.");
$product->setShortDescription("High-end widget.");
$product->setPrice(70.50);
$product->setTypeId('simple');
$product->setAttributeSetId(9); // need to look this up
$product->setCategoryIds("20,24"); // need to look these up
$product->setWeight(1.0);
$product->setTaxClassId(2); // taxable goods
$product->setVisibility(4); // catalog, search
$product->setStatus(1); // enabled

// assign product to the default website
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));

$product->save();

The example above shows a minimal set of attributes you should be setting on a new product. If you run this code and look under Manage Products on the backend, you should see your new product. However, the real trick is making sure they appear on the frontend.

The product should now be appearing on the frontend, but it will probably be showing up as ‘Out of stock’. A product’s inventory and stock options are contained in a separate object, rather than as part of the product itself. Adding the following code somewhere between the getModel() and save() lines will allow you to set the stock data:

$stockData = $product->getStockData();
$stockData['qty'] = 10;
$stockData['is_in_stock'] = 1;
$stockData['manage_stock'] = 1;
$stockData['use_config_manage_stock'] = 0;
$product->setStockData($stockData);

This code gets the product’s stock and inventory options as an array, modifies them and then saves the array back to the object. The options should be self-explanatory – the important one is ‘is_in_stock’, which controls whether the product is in stock or not. The example above sets the product’s ‘Manage Stock’ attribute to ‘Yes’ and un-ticks the corresponding ‘Use Config Settings’ checkbox, so the product will always track stock regardless of the global config setting. This is just an example, so be sure to set values appropriately for your store.

Advertisements

15 thoughts on “Magento Add product via script

  1. Hi,
    how to add dynamic products from frontend.For that i need to create a link in header and a form will open and the product will first go to admin.If it is a correct product then it will save in database.I don’t know how to do please help me.

    • Hi Swapna

      I guess product can be added from admin section always..
      If you need to add some products from admin then you need to provide your own logic.. ie custom module with proper MVC structure..

      Are you aware of custom module development..? If no then please refer to magento website for the same

      Any more questions please feel free to contact.

    • Hi Swapna

      The link shows the functionality of editing the contents and other product information but in the previous question you asked about to add a new dynamic product from front end..

      The package described above gives you functionality of editing the existing product via admin login..

      If you want to achieve the custom product functionality then you need to move ahead with the custom module.. as the functionality asked by you is not common one.. as in general only admin or store owner should decide the product range.. and hence forth flow.. for the quantity price stock availability
      discounts special prices stock clearance sell special product etc..

      so let general user add a product from the front end.. is a bit risky. and complex for admin to track and make it available in real

      so if you clear flow in your mind.. i can help you out creating the custom module

  2. Hi Abhijeetgk

    Actually my requirement is “User should be able to add products on the website, which are displayed along with other existing products. In the product overview first the existing products should be shown (entered in back-end), then the new products .If admin will allow that product to display in frontend then only the product will display.”
    Sorry for my previous misconception.
    Please help me.

  3. Hi Abhijeetgk

    Thanks for your response.
    No,User will add the product in front end,then only admin can test the product and it will store in database.

  4. Hi Swapna

    First thing you need to confirm that whether you trying to send a mail via localhost or on live environment

    To send mail you need a SMTP port to open

    On local machine you need to contact your network administrator to open the port
    on live environment its generally open but in case of some shared hosting at some places its blocked.

    In this scenario you need to set the SMTP username and password in the magento settings to send mail via that username and password combination

    Check all these settings and if still problem persists then do let me know

  5. Hi Abhijeetgk

    In System->Configuration->System->mail setting option ,Host and port are set.
    Mail is sending if i will set both Send Email To and Email sender option in System->Configuration->Contacts option i.e i am able to send mail between same host but i want to send from any host mail to any destination host mail.
    Refer this below link and exactly i want this process that is done in Email To Friend link in product view page.

    refer link: http://www.stagefreaks.nl/
    This link is in Dutch u can translate.

  6. Hi Abhijeetgk

    I am working in cron job.I don’t have any idea about this.please help me to understand this.
    My requirement is i have to send an email about product stock through email.

    Thanks,
    Swapna

    • Hi Swapna

      first of all the Cron job is the one which you can run in the background at specific intervals say you want to send email every afternoon at 12 so you can set a task that will run automatically on server at the time

      The Crontab is concept specific to linux server for windows it may refer as scheduled tasks

      See for quick reference http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

      Now your main question “Need to send email about the product task”

      first let me know the things it it regular email like whatever the product stock is it should fire email at regular intervals ? or specific one say stock below 10 then fire the email.

      for the first one you can create a custom script that will check the product stock for each product and sends a detailed report in email and schedule that script on server cron tab at the time you need and you are done

      second one i guess there is functionality in admin to warn about the low product stock, Please check magento forums for the same

  7. Hi Abhijeetgk

    Thanks for quick reply.I want to create a custom cron job for sending mail about all product stock status and i am working in windows.Thanks for your help.

    Thanks,
    Swapna.

  8. Hi Abhijeet,

    I want to add product functionality in frontend for registered users. Front end user’s product have the ability to RAFQ (request a quote) functionality,

    for the RAFQ functionality i have used the Cart2Quote extension and it is working fine.

    But adding a product from front end – for this any plugin or extension available? or i want to customize something.

    Please help me

    Regards,
    Peeyush Jagtap

What you think ? Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s