4

Hi I am developing a script which makes simple products for magento store. I have added the products through the script but now I want to add custom options to the added simple product. How would I do it through my custom script? Here is how I am adding the product:

$product = Mage::getModel('catalog/product');
$product
        ->setStoreId($store_id) //you can set data in store scope
        ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
        ->setAttributeSetId(4) //ID of a attribute set named 'default'
        ->setTypeId('simple') //product type
        ->setCreatedAt(strtotime('now')) //product creation time
->setWeight(1.0000)
        ->setStatus(1) //product status (1 - enabled, 2 - disabled)
        ->setTaxClassId(0) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
        ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
->setHasOptions(true);
    $product->save();

After this code I want to add the custom options to this added product. How can I do it? Any help would be greatly appreciated. Thankyou.

1
  • Have you tried my suggestion?
    – laketuna
    Mar 19, 2015 at 13:37

2 Answers 2

3

Before $product->save(), you can add some code to save. Here's some good guides on how it works and what you need to write. HERE and HERE

Basically...

$options = array(...); // Look at guides how to create this array
$product->setCanSaveCustomOptions(true);
$product->getOptionInstance()->addOption($option);
$product->setHasOptions(true);
2
1

Magento provides SOAP API for this. You should use magento SOAP API for add product which is secure and standard method of Magento. For add custom option you can use following API:

http://www.magentocommerce.com/api/soap/catalog/catalogProductCustomOption/product_custom_option.add.html

It has example with explanation of API uses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.