Migrating Product Custom Options From M1 to M2, Without Using Magento Migration Tool

Problem I came across: 

While working on magento 1 to 2 migration, there were some custom options in products that could not be moved while I migrated the data, since I was not using the magento migration tool.

Custom options are basically some variation in products that are not dependant upon attributes. This however has a few limitations, one that we cannot track the inventory based on any variation and that the product with custom options cannot be used with a configurable, bundled or grouped product. Custom options are mainly used where the inventory needs are very basic. 

Moving custom options while migrating data from Magento 1 to Magento 2 can be tricky. Being in a similar situation, here is what I consider the best way. 

Solution Derived: 

Created a code to move custom options in products while migrating data from Magento 1 to Magento 2.

Magento 2 has a set CSV product import format in which if the custom options are uploaded, they will reflect in the products. The csv shall have two columns: sku and custom_options. Uploading a csv in this format gives the desired results. 

Here is the Code to get the format:

<?php

require_once ‘app/Mage.php’;

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

function getProductCustomOptions($product)

{

    $optionsString = ”;

    $opts = Mage::getSingleton(‘catalog/product_option’)->getProductOptionCollection($product);

    if (count($opts)) {

        foreach ($opts as $option) {

            foreach ($option->getValues() as $value) {

                $optionsString .= “name=” . $option->getData(‘title’) . “,type=” . $option->getData(‘type’) . “,required=” . $option->getData(‘is_require’);

                $optionsString .= “,price=” . $value->getData(‘price’) . “,price_type=” . $value->getData(‘price_type’) . “,sku=” . $value->getData(‘sku’) . “,option_title=” . $value->getData(‘title’) . “|”;

            }

        }

        $optionsString = rtrim($optionsString, ‘|’);

    }

    return $optionsString;

}

$file = fopen(‘products.csv’, ‘w’);

$fileColumns = array(‘sku’,’custom_options’);

fputcsv($file, $fileColumns);

$product = Mage::getModel(‘catalog/product’)->load($prdId);

$productData = [];

$productData[‘sku’] = $product->getSku();

$productData[‘custom_options’] = getProductCustomOptions($product);

fputcsv($file, $productData);
Hope it helps! If you face any problems while implementing, feel free to ask by leaving a comment below.

Leave a Reply