Create New Product on Form Submission

You can easily utilize WebForms API to create new product after the form has been submitted.

Lets create new extension called SubmitProduct.

SubmitProduct file structure:

It contains 4 files in total.

app
├── code
│ └── community
│ └── VladimirPopov
│ └── SubmitProduct
│ ├── etc
│ │ ├── config.xml
│ │ └── system.xml
│ └── Model
│ └── Observer.php
└── etc
└── modules
└── VladimirPopov_SubmitProduct.xml

app / etc / modules / VladimirPopov_SubmitProduct.xml

This file registeres and enables extension in Magento

<?xml version="1.0"?>
<config>
<modules>
<VladimirPopov_SubmitProduct>
<active>true</active>
<codePool>community</codePool>
</VladimirPopov_SubmitProduct>
</modules>
</config>

app / code / community / VladimirPopov / SubmitProduct

This folder contains main extension files.

etc / config.xml

In this file we are registering model for our extension and hook it to WebForms event "webforms_result_submit".

<?xml version="1.0"?>
<config>
<modules>
<VladimirPopov_SubmitProduct>
<version>1.0.0</version>
</VladimirPopov_SubmitProduct>
</modules>
<adminhtml>
<acl>
<resources>
<all>
<title>Allow Everything</title>
</all>
</resources>
</acl>
</adminhtml>
<global>
<models>
<submitproduct>
<class>VladimirPopov_SubmitProduct_Model</class>
</submitproduct>
</models>
<events>
<webforms_result_submit>
<observers>
<submitproduct_observer>
<type>singleton</type>
<class>VladimirPopov_SubmitProduct_Model_Observer</class>
<method>submitProduct</method>
</submitproduct_observer>
</observers>
</webforms_result_submit>
</events>
</global>
</config>

etc / system.xml

This file holds configuration options for our extension. SubmitProduct has turn on/off switch.

<?xml version="1.0"?>
<config>
<sections>
<webforms>
<groups>
<submitproduct translate="label comment">
<comment></comment>
<label>Submit products through web-forms</label>
<frontend_type>text</frontend_type>
<sort_order>1000</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<enable translate="label comment">
<comment>
<![CDATA[Enable products submission through web-forms]]>
</comment>
<label>Enable products submission</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</enable>
</fields>
</submitproduct>
</groups>
</webforms>
</sections>
</config>

You`ll notice new block on Web-forms > Forms Settings page.

Model / Observer.php

Here we define model that has method of create new product when the form is submitted.

<?php
class VladimirPopov_SubmitProduct_Model_Observer{
protected $result;
protected $form;
public function submitProduct($observer){
// skip if the script is not enabled
if(!Mage::getStoreConfig('webforms/submitproduct/enable')) return;
$webform = $observer->getWebform();
// list of allowed forms
$allowed_forms = array('submitproduct','submitproduct2');
// skip any non-allowed forms
if(!in_array($webform->getCode(),$allowed_forms)) return;
$this->form = $webform;
$this->result = Mage::getModel('webforms/results')->load($observer->getResult()->getId());
// list of categories ID numbers for new product
$categories = array(3);
// list of websites ID numbers for new product
$websites = array(1);
// tax class
$tax_class = 0;
// attribute set ID number
$attribute_set = 4;
// product type
$type = 'simple';
// create product
$product = Mage::getModel('catalog/product')->setStoreId(0);
// set product type
$product->setTypeId($type);
// set attribute set
$product->setAttributeSetId($attribute_set);
// set categories
$product->setCategoryIds($categories);
// set websites
$product->setWebsiteIDs($websites);
// set tax class
$product->setTaxClassId($tax_class);
// generate unique SKU
$product->setSku('SUBMIT_PRODUCT_'.Mage::helper('webforms')->randomAlphaNum());
// set status disabled
$product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
// set visibility
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
// set name
$product->setName($this->getValueByCode('name'));
// set description
$product->setDescription($this->getValueByCode('description'));
// set short description
$product->setShortDescription($this->getValueByCode('short_description'));
// set weight
$product->setWeight($this->getValueByCode('weight'));
// set price
$product->setPrice($this->getValueByCode('price'));
// set stock
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => $this->getValueByCode('quantity')
));
// set images
$image1 = $this->result->getFileFullPath($this->getFieldIdByCode('image1'), $this->getValueByCode('image1'));
$product->addImageToMediaGallery($image1, array('image','small_image','thumbnail'));
// set additional optional image
if($this->getValueByCode('image2')){
$image2 = $this->result->getFileFullPath($this->getFieldIdByCode('image2'), $this->getValueByCode('image2'));
$product->addImageToMediaGallery($image2);
}
$product->save();
}
// get field value by field code parameter
protected function getValueByCode($code){
if(empty($this->result) || empty($this->form)) return false;
foreach($this->form->getFieldsToFieldsets() as $fieldset){
foreach($fieldset['fields'] as $field){
if($field->getCode() == $code){
return $this->result->getData('field_'.$field->getId());
}
}
}
}
// get field id by field code parameter
protected function getFieldIdByCode($code){
if(empty($this->result) || empty($this->form)) return false;
foreach($this->form->getFieldsToFieldsets() as $fieldset){
foreach($fieldset['fields'] as $field){
if($field->getCode() == $code){
return $field->getId();
}
}
}
}
}

We are using specific field codes to identify which field should work as a product name, description, short description and images. Please have a look at code comments for details.

You can download sources here.