Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

creating configurable product with the API
 
shani1351
Sr. Member
 
Total Posts:  156
Joined:  2010-01-24
 

Hello,
I’m writing a script that is creating two simple products and than should create a configurable product that links the two simple products (all with Magneto API).

The Configurable Attribute is “Housing”
(Attribute Code : housing
Input Type : drop-down
scoop: global
Use To Create Configurable Product: yes
ID = 525

It has two options:
1. Admin - With Housing
Default Store View - Bulb & Housing
Position - 1
ID = 6

2. Admin - Without Housing
Default Store View - Lamp only
Position - 2
ID = 5
)

My code to create simple product is:
$product = Mage::getModel(’catalog/product’);

$product->setWebsiteIds(array(’0’)); // your website id might be different
$product->setAttributeSetId(4);
$product->setSku($arg_data[’sku’]);
$product->setTypeId(’simple’);
$product->setName($arg_data[’name’]);
$product->setDescription(’description’);
$product->setShortDescription($arg_data[’short_description’]);
$product->setPrice($arg_data[’price’]);
$product->setWeight(0);
$product->setStatus(1); //0-Disabled; 1-Enabled
$product->setTaxClassId(0);
$product->setCategoryIds(’2, ‘ . $arg_data[’category’]);
$product->setVisibility(1); // 1-Nowhere; 2-Catalog; 3-Search; 4-Catalog/Search
$product->setHousing($arg_data[’housing’]);
// $product->setStekName($arg_data[’name’]);
// $product->setVendorId($arg_data[’vendor_id’]);
// $product->setImage($arg_data[’image_path’]);
// $product->addImage($arg_data[’image_path’]);
// add size
//$new_size = $this->getRkSize($arg_data[’sku’]);
//$size_val = $this->attributeValueExists(’size’, $new_size);

//$new_color = $this->getRkColor($arg_data[’sku’]);
//$color_val = $this->attributeValueExists(’color’, $new_color);

//set stock data
if (!$stockData = $product->getStockData()) {
$stockData = array();
}
// IF QTY > 0
if($arg_data[’qty_in_stock’] > 0){$status = 1;}else{$status = 0;}

$stockData[’qty’] = $arg_data[’qty_in_stock’];
$stockData[’is_in_stock’] = $status;
$stockData[’manage_stock’] = 0;

try
{
if(!$this->existsInMagento($arg_data[’sku’]))
{
$product->setStockData($stockData);
$product->save();
}
} catch (Exception $e) {
//do nothing becuase the item already exists
}

$entity_id = getEntityID_bySKU($db_magento, $sku_with_housing);

$url_key = $prodModel->formatUrlKey($sku_with_housing);
$url_path = $url_key.’.html’;

// Call function from script
updateProductEntityVarchar($db_magento, $entity_id, ‘url_key’, $sku_with_housing);
updateProductEntityVarchar($db_magento, $entity_id, ‘url_path’, $sku_with_housing);
updateManufacturer($db_magento, $entity_id, $brand);

The simple products are being created ok.
The problem is with the configurable product creation. The code is:
$Configurable_Data= array(
‘sku’ => (str_replace ($quote, “`”, $sku)),
‘name’ => (str_replace ($quote, “`”, $sku)),
‘price’ => (str_replace ($quote, “`”, $price_with_housing)),
‘category’ => $category,
‘short_description’ => (str_replace ($quote, “`”, $sku)),
‘qty_in_stock’ => (str_replace ($quote, “`”, $qty))
);

$Cildrens_Data = array(’0’ => array(’sku’ => $sku_with_housing), ‘1’ => array(’sku’ => $sku_lamp));

(continues on next post)

 
Magento Community Magento Community
Magento Community
Magento Community
 
shani1351
Sr. Member
 
Total Posts:  156
Joined:  2010-01-24
 

createConfigurableProduct($Configurable_Data, $Cildrens_Data);

public function createConfigurableProduct($data, $childrens)
{
$product = Mage::getModel(’catalog/product’);
$product->setTypeId(’configurable’);
$product->setTaxClassId(0); //none
$product->setWebsiteIds(array(’0’)); // store id
$product->setAttributeSetId(4);
$product->setSku($data[’sku’]);
$product->setName($data[’name’]);
$product->setDescription("Description");
$product->setInDepth("Description");
$product->setPrice($data[’price’]);
$product->setShortDescription("Short Description");
$product->setWeight(0);
$product->setStatus(1); //enabled
$product->setVisibility(4); //catalog and search
$product->setMetaDescription("Meta Description");
$product->setMetaTitle($data[’sku’]);
$product->setMetaKeywords($data[’sku’]);
$product->setCategoryIds(’2, ‘ . $data[’category’]);

$childrens_data = array($childrens[0][’sku’]=>
array(’0’=>array(’attribute_id’=>’525’,’label’=>’With Housing’,’value_index’=>’6’,’is_percent’=>0,’pricing_value’=>’’)),
$childrens[1][’sku’]=>array(’0’=>
array(’attribute_id’=>’535’,’label’=>’Without Housing’,’value_index’=>’5’,’is_percent’=>0,’pricing_value’=>’’))
);

$product->setConfigurableProductsData($childrens_data);

$Attributes_data = array(’0’=>array(’id’=>NULL,’label’=>’Housing’,’position’=> NULL,
‘values’=>array(’0’=>
array(’value_index’=>6,’label’=>’With Housing’,’is_percent’=>0,
‘pricing_value’=>’0’,’attribute_id’=>’525’),
‘1’=>
array(’value_index’=>5,’label’=>’Without Housing’,
‘is_percent’=>0,’pricing_value’=>’0’,’attribute_id’=>’525’)
),
‘attribute_id’=>525,’attribute_code’=>’housing’,’frontend_label’=>’Housing’,
‘html_id’=>’config_super_product__attribute_0’)
);

$product->setConfigurableAttributesData($Attributes_data);
$product->setCanSaveConfigurableAttributes(1);

try{
$product->save();
// $productId = $product->getId();
//echo $product->getId() . “, $price, $itemNum added\n”;
//echo “<tr>”;
// echo “<td>$sku</td><td>$product->getId()</td><td>1</td><td>$manufacturer</td><td>$price_with_housing</td><td>$qty</td>”;
echo “</tr>\n”;
}
catch (Exception $e){
//echo “$price, $itemNum not added\n”;
//echo “exception:$e”;
}
}

The script doesn’t raise any errors from Magento API (or from the code on the script). But the configurable product doesn’t get created.

Hep please! I don’t understand what is the problem.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top