cancel
Showing results for 
Search instead for 
Did you mean: 

undefined method in getConfigurableAttributeCollection

SOLVED

undefined method in getConfigurableAttributeCollection

We have issue on checkout. Will not redirect in some cases. Help is welcome.

Magento 2.3.3.

Error:

Error: Uncaught exception 'Error' with message 'Call to undefined method Magento\Catalog\Model\Product\Type\Simple::getConfigurableAttributeCollection()' in /home/ortlieb/public_html/vendor/magento/module-configurable-product/Helper/Product/Options/Loader.php:52

     

Error: Uncaught exception 'Error' with message 'Call to undefined method Magento\Catalog\Model\Product\Type\Simple::getConfigurableAttributeCollection()' in /home/ortlieb/public_html/vendor/magento/module-configurable-product/Helper/Product/Options/Loader.php:52
in Magento\ConfigurableProduct\Helper\Product\Options\Loader::load called at /home/ortlieb/public_html/vendor/magento/module-configurable-product/Model/Product/ReadHandler.php (48) in Magento\ConfigurableProduct\Model\Product\ReadHandler::execute called at /home/ortlieb/public_html/vendor/magento/framework/EntityManager/Operation/Read/ReadExtensions.php (48) in Magento\Framework\EntityManager\Operation\Read\ReadExtensions::execute called at /home/ortlieb/public_html/vendor/magento/framework/EntityManager/Operation/Read.php (112) in Magento\Framework\EntityManager\Operation\Read::execute called at /home/ortlieb/public_html/vendor/magento/framework/EntityManager/EntityManager.php (70) in Magento\Framework\EntityManager\EntityManager::load called at /home/ortlieb/public_html/vendor/magento/module-catalog/Model/ResourceModel/Product.php (648) in Magento\Catalog\Model\ResourceModel\Product::load called at /home/ortlieb/public_html/generated/code/Magento/Catalog/Model/ResourceModel/Product/Interceptor.php (258) in Magento\Catalog\Model\ResourceModel\Product\Interceptor::load called at /home/ortlieb/public_html/vendor/magento/framework/Model/AbstractModel.php (540) in Magento\Framework\Model\AbstractModel::load called at /home/ortlieb/public_html/vendor/magento/framework/Interception/Interceptor.php (58) in Magento\Catalog\Model\Product\Interceptor::___callParent called at /home/ortlieb/public_html/vendor/magento/framework/Interception/Interceptor.php (138) in Magento\Catalog\Model\Product\Interceptor::Magento\Framework\Interception\{closure} called at /home/ortlieb/public_html/vendor/magento/framework/Interception/Interceptor.php (153) in Magento\Catalog\Model\Product\Interceptor::___callPlugins called at

1 ACCEPTED SOLUTION

Accepted Solutions

Re: undefined method in getConfigurableAttributeCollection

Hello @emile_rdam 

 

Yes, it is correct.

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

View solution in original post

6 REPLIES 6

Re: undefined method in getConfigurableAttributeCollection

Hello @emile_rdam 

 

I found a temporary solution by adding the below simple condition in Magento\ConfigurableProduct\Helper\Product\Options\Loader::load(ProductInterface $product) after line number 51:

 

if (get_class($typeInstance) == 'Magento\Catalog\Model\Product\Type\Simple' || get_class($typeInstance) == 'Magento\Bundle\Model\Product\Type')
{
            return null;
}

I hope it helps.

 

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

Re: undefined method in getConfigurableAttributeCollection

Hi, thank you./ So where exactly do I make this change? In /public_html/vendor/magento/module-configurable-product/Helper/Product/Options/Loade.php is my guess?

Then I change

        $options = [];
        /** @var Configurable $typeInstance */
        $typeInstance = $product->getTypeInstance();
        $attributeCollection = $typeInstance->getConfigurableAttributeCollection($product);
        $this->extensionAttributesJoinProcessor->process($attributeCollection);
        foreach ($attributeCollection as $attribute) {

to

        $options = [];
        /** @var Configurable $typeInstance */
        $typeInstance = $product->getTypeInstance();
		if (get_class($typeInstance) == 'Magento\Catalog\Model\Product\Type\Simple' || get_class($typeInstance) == 'Magento\Bundle\Model\Product\Type')
{
            return null;
}
        $attributeCollection = $typeInstance->getConfigurableAttributeCollection($product);
        $this->extensionAttributesJoinProcessor->process($attributeCollection);
        foreach ($attributeCollection as $attribute) {

This the location to do this? Thanks

 

Re: undefined method in getConfigurableAttributeCollection

Hello @emile_rdam 

 

Yes, it is correct.

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

Re: undefined method in getConfigurableAttributeCollection

So far, so good. Looks like this will solve it in our first tests.

Re: undefined method in getConfigurableAttributeCollection

Is not recommended edit the core files either magento or any framework. I made one override to the class. I don't know if there is a best way to fix this issue, but for mi works.

 

di.xml:

 

<?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="Custom\Module\Helper\Product\Options\Loader"/>
</config>

 

Override:

 

<?php
/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Custom\Module\Helper\Product\Options;

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
 * @package Custom\Module\Helper\Product\Options
 */
class Loader extends \Magento\ConfigurableProduct\Helper\Product\Options\Loader
{
    /**
     * @var OptionValueInterfaceFactory
     */
    private $optionValueFactory;

    /**
     * @var JoinProcessorInterface
     */
    private $extensionAttributesJoinProcessor;

    /**
     * Loader 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): array
    {
        $options = [];
        /** @var Configurable $typeInstance */
        $typeInstance = $product->getTypeInstance();
        if ($typeInstance instanceof \Magento\Catalog\Model\Product\Type\Simple ||
            $typeInstance instanceof \Magento\Bundle\Model\Product\Type) {
            return $options;
        }

        $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;
    }
}

Re: undefined method in getConfigurableAttributeCollection

rookie question - where should the override file be?

 


@JhonSalazar wrote:

Is not recommended edit the core files either magento or any framework. I made one override to the class. I don't know if there is a best way to fix this issue, but for mi works.

 

di.xml:

 

<?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="Custom\Module\Helper\Product\Options\Loader"/>
</config>

 

Override:

 

<?php
/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Custom\Module\Helper\Product\Options;

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
 * @package Custom\Module\Helper\Product\Options
 */
class Loader extends \Magento\ConfigurableProduct\Helper\Product\Options\Loader
{
    /**
     * @var OptionValueInterfaceFactory
     */
    private $optionValueFactory;

    /**
     * @var JoinProcessorInterface
     */
    private $extensionAttributesJoinProcessor;

    /**
     * Loader 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): array
    {
        $options = [];
        /** @var Configurable $typeInstance */
        $typeInstance = $product->getTypeInstance();
        if ($typeInstance instanceof \Magento\Catalog\Model\Product\Type\Simple ||
            $typeInstance instanceof \Magento\Bundle\Model\Product\Type) {
            return $options;
        }

        $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;
    }
}