Ashwini
Ashwini

Reputation: 155

Magento Insert Blank Value in Product Custom Options

I'm Try to insert custom options pragmatically in product. I'v execute the below script to insert custom options

$product = Mage::getModel('catalog/product')->load(2040);

$optionData = array(
    'is_delete'         => 0,
    'is_require'        => true,
    'previous_group'    => '',
    'title'             => $product->getName(),
    'type'              => 'radio',
    'sort_order'        => 1,
    'values'            => array(
         array(
              'is_delete'     => 0,
              'title'         => 'Standard',
              'price_type'    => 'Fixed',
              'price'         => '0.0',
              'sku'           => '1',
              'option_type_id'=> -1,
         ),
         array(
                  'is_delete'     => 0,
                  'title'         => 'Large',
                  'price_type'    => 'Fixed',
                  'price'         => '50.0',
                  'sku'           => '1',
                  'option_type_id'=> -1,
         ),
         array(
                  'is_delete'     => 0,
                  'title'         => 'Extra Large',
                  'price_type'    => 'Fixed',
                  'price'         => '100.0',
                  'sku'           => '1',
                  'option_type_id'=> -1,
         )
    )
);

$product->setProductOptions(array($optionData));
$product->setCanSaveCustomOptions(1);
$product->save();

When I run the above code it insert the custom option title but custom option price inserted empty.

Below is the custom option screenshot after insert script run: enter image description here

Upvotes: 2

Views: 580

Answers (1)

Kailas
Kailas

Reputation: 3231

I've Given 5 Steps to to insert product custom option in magneto. Also check option data fields link below:

Step 1: Set Admin Store

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Step 2: Load Product

$product = Mage::getModel('catalog/product')->load($productID);

Step 3: Create Custom Option Data Array

$optionData = array(
            'title' => $product->getName(),
            'type' => 'radio',
            'is_required' => 1,
            'sort_order' => 0,
            'values' => array(
                array(
                    'title' => 'Standard',
                    'price' => $standardPrice, // $standardPrice value like 10.11 OR '10.11'
                    'price_type' => 'fixed',
                    'sku' => '',
                    'sort_order' => 0,
                ),
                array(
                    'title' => 'Premium',
                    'price' => $premiumPrice,
                    'price_type' => 'fixed',
                    'sku' => '',
                    'sort_order' => 0,
                ),
                array(
                    'title' => 'Deluxe',
                    'price' => $deluxePrice,
                    'price_type' => 'fixed',
                    'sku' => '',
                    'sort_order' => 0,
                )
            )
        );

Step 4: Set Product Option Data

$product->setProductOptions(array($optionData));

Step 5: Save Product

$product->setCanSaveCustomOptions(true);
$product->save();

Upvotes: 2

Related Questions