0

I want to extend the Magento\ConfigurableProduct\Helper\Product\Options\Loader as per this github issue https://github.com/magento/magento2/issues/21663#issuecomment-477966963

My di.xml file is

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <preference for="Magento\ConfigurableProduct\Helper\Product\Options\Loader" type="Vendor\Mymodule\Helper\Loader" />
</config>

My Loader.php is in Helper folder in my module. The code is below

  <?php
    
    namespace Vendor\Mymodule\Helper;
    
    use \Magento\Catalog\Api\Data\ProductInterface;
    use \Magento\ConfigurableProduct\Api\Data\OptionInterface;
    use \Magento\ConfigurableProduct\Model\Product\Type\Configurable;
    use \Magento\ConfigurableProduct\Api\Data\OptionValueInterfaceFactory;
    use \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
    
    /**
     * Class Loader
     */
    class Loader
    {
        /**
         * @var OptionValueInterfaceFactory
         */
        private $optionValueFactory;
    
        /**
         * @var JoinProcessorInterface
         */
        private $extensionAttributesJoinProcessor;
    
        /**
         * ReadHandler constructor
         *
         * @param OptionValueInterfaceFactory $optionValueFactory
         * @param JoinProcessorInterface $extensionAttributesJoinProcessor
         */
        public function __construct(
            OptionValueInterfaceFactory $optionValueFactory,
            JoinProcessorInterface $extensionAttributesJoinProcessor
        ) {
            $this->optionValueFactory = $optionValueFactory;
            $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
        }
    
        /**
         * @param ProductInterface $product
         * @return OptionInterface[]
         */
        public function load(ProductInterface $product)
        {
            $options = [];
            /** @var Configurable $typeInstance */
            $typeInstance = $product->getTypeInstance();
           if (strstr(get_class($typeInstance), 'Magento\Catalog\Model\Product\Type\Simple') !== 
   false || strstr(get_class($typeInstance), 'Magento\Bundle\Model\Product\Type') !==  false) {
            return null;
        }
            $attributeCollection = $typeInstance->getConfigurableAttributeCollection($product);
            $this->extensionAttributesJoinProcessor->process($attributeCollection);
            foreach ($attributeCollection as $attribute) {
                $values = [];
                $attributeOptions = $attribute->getOptions();
                if (is_array($attributeOptions)) {
                    foreach ($attributeOptions as $option) {
                        /** @var \Magento\ConfigurableProduct\Api\Data\OptionValueInterface $value */
                        $value = $this->optionValueFactory->create();
                        $value->setValueIndex($option['value_index']);
                        $values[] = $value;
                    }
                }
                $attribute->setValues($values);
                $options[] = $attribute;
            }
    
            return $options;
        }
    }

But I get this below issue.

Type Error occurred when creating object: Magento\ConfigurableProduct\Model\Product\ReadHandler,
Argument 1 passed to Magento\ConfigurableProduct\Model\Product\ReadHandler::__construct() 
must be an instance of Magento\ConfigurableProduct\Helper\Product\Options\Loader, instance
of Vendor\Mymodule\Helper\Loader given,
called in /var/www/html/master/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on
line 116 [] []

I could find the mistake.

1

2 Answers 2

2

I have tested the code in the local environment and found the same error as you have mentioned.

Then I noticed that you have not extended the class (Magento\ConfigurableProduct\Helper\Product\Options\Loader) in your module code. I added that in code and it worked!

<?php
    
    namespace Vendor\Mymodule\Helper;
    
    use Magento\Catalog\Api\Data\ProductInterface;
    use Magento\ConfigurableProduct\Api\Data\OptionInterface;
    use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
    use Magento\ConfigurableProduct\Api\Data\OptionValueInterfaceFactory;
    use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
    
    /**
     * Class Loader
     */
    class Loader extends \Magento\ConfigurableProduct\Helper\Product\Options\Loader
    {
       ....
       ....

After modifying the code,

  • rm -rf var/* generated/*
  • php bin/magento setup:di:compile
  • php bin/magento cache:flush

Hope this helps!

0

Please run bin/magento setup:upgrade in your ssh terminal.

I think then your issue will be solve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.