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.