0

I have imported product options in magento 2 But It throws following error, Please provide me a solution

1 exception(s): Exception #0 (Exception): Warning: Invalid argument supplied for foreach() in /var/www/html/cpap/vendor/magento/module-catalog/Block/Product/View/Options.php on line 215

1 Answer 1

0

vendor/magento/module-catalog/Block/Product/View/Options.php


/**
 * Get json representation of
 *
 * @return string
 */
public function getJsonConfig()
{
    $config = [];
    foreach ($this->getOptions() as $option) {
        /* @var $option \Magento\Catalog\Model\Product\Option */
        if ($option->hasValues()) {
            $tmpPriceValues = [];
            foreach ($option->getValues() as $valueId => $value) {
                $tmpPriceValues[$valueId] = $this->_getPriceConfiguration($value);
            }
            $priceValue = $tmpPriceValues;
        } else {
            $priceValue = $this->_getPriceConfiguration($option);
        }
        $config[$option->getId()] = $priceValue;
    }

    $configObj = new \Magento\Framework\DataObject(
        [
            'config' => $config,
        ]
    );

    //pass the return array encapsulated in an object for the other modules to be able to alter it eg: weee
    $this->_eventManager->dispatch('catalog_product_option_price_configuration_after', ['configObj' => $configObj]);

    $config=$configObj->getConfig();

    return $this->_jsonEncoder->encode($config);
}

In your case $option->getValues() returns null, this should retuns array. So quick fix is:


/**
 * Get json representation of
 *
 * @return string
 */
public function getJsonConfig()
{
    $config = [];
    foreach ($this->getOptions() as $option) {
        /* @var $option \Magento\Catalog\Model\Product\Option */
        if ($option->hasValues()) {
            $tmpPriceValues = [];
            if (is_array($option->getValues())) {
                foreach ($option->getValues() as $valueId => $value) {
                    $tmpPriceValues[$valueId] = $this->_getPriceConfiguration($value);
                }
            }
            $priceValue = $tmpPriceValues;
        } else {
            $priceValue = $this->_getPriceConfiguration($option);
        }
        $config[$option->getId()] = $priceValue;
    }

    $configObj = new \Magento\Framework\DataObject(
        [
            'config' => $config,
        ]
    );

    //pass the return array encapsulated in an object for the other modules to be able to alter it eg: weee
    $this->_eventManager->dispatch('catalog_product_option_price_configuration_after', ['configObj' => $configObj]);

    $config=$configObj->getConfig();

    return $this->_jsonEncoder->encode($config);
}

Note: Overwrite this class. Don't modify the core file

4
  • but is this a correct way or how would i find which option is null
    – Jaisa
    Commented Feb 21, 2019 at 5:33
  • You can set debug log getJsonConfig() and check.
    – Sohel Rana
    Commented Feb 21, 2019 at 6:16
  • Actually i have done what you have said, but it throws exception from other file
    – Jaisa
    Commented Feb 21, 2019 at 12:33
  • Can you suggest me, how to remove custom options, if it value is null
    – Jaisa
    Commented Feb 21, 2019 at 12:34

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.