4

Magento 2 Custom option multilingual does not work. It's solved using given below link https://github.com/magento/magento2/commit/0942a9a85193b8cac9b61af5ef0f44a6be3e843f

There default is working fine. But I add custom option programmatically into multiple product is not working.

code:

$newOptions = Array
(
    [title] => Drop Down
    [is_require] => 1
    [type] => drop_down
    [sort_order] => 1
    [sku] => Drop
    [max_characters] => 
    [file_extension] => 
    [image_size_x] => 
    [image_size_y] => 
    [default_title] => Drop Down
    [store_title] => Drop Down
    [default_price] => 
    [default_price_type] => 
    [store_price] => 
    [store_price_type] => 
    [price] => 
    [price_type] => 
    [values] => Array
        (
            [0] => Array
                (
                    [sku] => d1
                    [sort_order] => 0
                    [default_title] => d
                    [store_title] => d
                    [title] => d
                    [default_price] => 1.0000
                    [default_price_type] => fixed
                    [store_price] => 1.0000
                    [store_price_type] => fixed
                    [price] => 1.0000
                    [price_type] => fixed
                )

            [1] => Array
                (
                    [sku] => d2
                    [sort_order] => 1
                    [default_title] => dd
                    [store_title] => dd
                    [title] => dd
                    [default_price] => 2.0000
                    [default_price_type] => fixed
                    [store_price] => 2.0000
                    [store_price_type] => fixed
                    [price] => 2.0000
                    [price_type] => fixed
                )

        )

)
$productsId = Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
    [13] => 14
    [14] => 15
    [15] => 16
    [16] => 17
    [17] => 18
    [18] => 19
    [19] => 20
)

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
foreach($productsId as $productId)
    {
        $skus = [];
        $option_product = $objectManager->create("Magento\Catalog\Model\Product")->load($productId);
        $custom_options = $option_product->getOptions();
        foreach ($custom_options as $custom_option) 
        {
            array_push($skus, $custom_option['sku']);
        }
        if (!in_array($newOptions['sku'], $skus)) 
        {
            $option_product->setHasOptions(1);
            $option_product->getResource();
            $option = $objectManager->create('Magento\Catalog\Model\Product\Option')
                            ->setProductId($option_product->getId())
                            ->setStoreId($option_product->getStoreId())
                            ->addData($newOptions);
            $option->save();
            $option_product->addOption($option);
            $option_product->setCanSaveCustomOptions(true);
            $option_product->save($option_product);
        }
    } 

I am getting this error.

[2017-09-21 07:24:47] main.DEBUG: Warning: Invalid argument supplied for foreach() in /var/www/html/customoption/customMulti/vendor/magento/module-catalog/Model/Product/Option/Repository.php on line 196 {"is_exception":false} []

Repository.php

protected function markRemovedValues($newValues, $originalValues)
    {
        $existingValuesIds = [];
        foreach ($newValues as $newValue) {
            if (array_key_exists('option_type_id', $newValue)) {
                $existingValuesIds[] = $newValue['option_type_id'];
            }
        }
        /** @var $originalValue \Magento\Catalog\Model\Product\Option\Value */
//line 196 
        foreach ($originalValues as $originalValue) {
            if (!in_array($originalValue->getData('option_type_id'), $existingValuesIds)) {
                $originalValue->setData('is_delete', 1);
                $newValues[] = $originalValue->getData();
            }
        }
        return $newValues;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.