1

I'm migrating data from another platform to magento.

I'm looping over each product and inserting the data i've generated.

$product = Mage::getModel('catalog/product')->load($p['magento_id']);
if(!$product->getOptionsReadonly()) {
    $product->setProductOptions($pvars);
    $product->setCanSaveCustomOptions(true);
    $product->save();
}

what seems to be happening is that its adding the data, but also data from the previous inserts also, so i have multiple custom options called "Door Colour". the $pvar data is as it should be (see below).

$pvars contains:

Array
(
    [83] => Array
        (
            [title] => Door Colour
            [type] => drop_down
            [is_require] => 1
            [sort_order] => 0
            [values] => Array
                (
                    [0] => Array
                        (
                            [title] => Grey
                            [price] => 0.00
                            [price_type] => fixed
                            [sku] => 83-83-grey
                            [sort_order] => 1
                        )

                    [1] => Array
                        (
                            [title] => Dark Blue
                            [price] => 0.00
                            [price_type] => fixed
                            [sku] => 83-84-dark-blue
                            [sort_order] => 1
                        )

                    [2] => Array
                        (
                            [title] => Red
                            [price] => 0.00
                            [price_type] => fixed
                            [sku] => 83-85-red
                            [sort_order] => 1
                        )

                    [3] => Array
                        (
                            [title] => Maroon
                            [price] => 0.00
                            [price_type] => fixed
                            [sku] => 83-86-maroon
                            [sort_order] => 1
                        )

                    [4] => Array
                        (
                            [title] => Pastel Blue
                            [price] => 0.00
                            [price_type] => fixed
                            [sku] => 83-87-pastel-blue
                            [sort_order] => 1
                        )

                    [5] => Array
                        (
                            [title] => Yellow
                            [price] => 0.00
                            [price_type] => fixed
                            [sku] => 83-88-yellow
                            [sort_order] => 1
                        )

                    [6] => Array
                        (
                            [title] => Green
                            [price] => 0.00
                            [price_type] => fixed
                            [sku] => 83-89-green
                            [sort_order] => 1
                        )

                )

        )

    [435] => Array
        (
            [title] => 11 Door Lock Option
            [type] => drop_down
            [is_require] => 1
            [sort_order] => 0
            [values] => Array
                (
                    [0] => Array
                        (
                            [title] => Cam Locks (Free)
                            [price] => 0.00
                            [price_type] => fixed
                            [sku] => 435-90-cam-locks--free-
                            [sort_order] => 1
                        )

                    [1] => Array
                        (
                            [title] => Padlock Fitting (Free)
                            [price] => 0.00
                            [price_type] => fixed
                            [sku] => 435-91-padlock-fitting--free-
                            [sort_order] => 1
                        )

                    [2] => Array
                        (
                            [title] => 4 Digit Combination Locks
                            [price] => 253.00
                            [price_type] => fixed
                            [sku] => 435-92-4-digit-combination-locks
                            [sort_order] => 1
                        )

                    [3] => Array
                        (
                            [title] => £1 Coin Return Locks
                            [price] => 302.50
                            [price_type] => fixed
                            [sku] => 435-93---1-coin-return-locks
                            [sort_order] => 1
                        )

                    [4] => Array
                        (
                            [title] => Electronic Combination Locks
                            [price] => 495.00
                            [price_type] => fixed
                            [sku] => 435-94-electronic-combination-locks
                            [sort_order] => 1
                        )

                )

        )

    [761] => Array
        (
            [title] => Size (mm)
            [type] => drop_down
            [is_require] => 1
            [sort_order] => 0
            [values] => Array
                (
                    [0] => Array
                        (
                            [title] => 1800h x 1500w x 450d (11ARCH)
                            [price] => 0.00
                            [price_type] => fixed
                            [sku] => 761-95-1800h-x-1500w-x-450d--11arch-
                            [sort_order] => 1
                        )

                )

        )

)

1 Answer 1

1

worked it out, for anyone interested:

$product = Mage::getModel('catalog/product')->load($p['magento_id']);
foreach($pvars as $optionData)
{
  $product->setHasOptions(1);
  $option = Mage::getModel('catalog/product_option')
          ->setProductId($p['magento_id'])
          ->setStoreId(1)
          ->addData($optionData);
  $option->save();
  $product->addOption($option); 

  Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  $product->save();  
}
2
  • Actually it is a bug as the same happens with the internal API, glad you found a solution.
    – user2935
    Commented Dec 4, 2013 at 10:51
  • @Trev thanks for sharing your solution - feel free to accept your own answer as the answer to mark this question as solved Commented Mar 4, 2014 at 20:27

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.