0

In order to override the function getBundleOptions from magento2/vendor/magento/module-bundle/Helper/Catalog/Product/Configuration.php i have implemented the following module:

MySpace/MyModule3/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <preference for="Magento\Bundle\Helper\Catalog\Product\Configuration" type="MySpace\MyModule3\Helper\Bundle\Catalog\Product\Configuration\Configuration" />
</config>

MySpace/MyModule3/Helper/Bundle/Catalog/Product/Configuration/Configuration.php

<?php
namespace MySpace\MyModule3\Helper\Bundle\Catalog\Product\Configuration;
class Configuration extends Magento\Bundle\Helper\Catalog\Product\Configuration
{
    /**
     * Get bundled selections (slections-products collection)
     *
     * Returns array of options objects.
     * Each option object will contain array of selections objects
     *
     * @param ItemInterface $item
     * @return array
     */
    public function getBundleOptions(ItemInterface $item)
    {
        $options = [];
        $product = $item->getProduct();

        /** @var \Magento\Bundle\Model\Product\Type $typeInstance */
        $typeInstance = $product->getTypeInstance();

        // get bundle options
        $optionsQuoteItemOption = $item->getOptionByCode('bundle_option_ids');
        $bundleOptionsIds = $optionsQuoteItemOption
            ? $this->serializer->unserialize($optionsQuoteItemOption->getValue())
            : [];

        if ($bundleOptionsIds) {
            /** @var \Magento\Bundle\Model\ResourceModel\Option\Collection $optionsCollection */
            $optionsCollection = $typeInstance->getOptionsByIds($bundleOptionsIds, $product);

            // get and add bundle selections collection
            $selectionsQuoteItemOption = $item->getOptionByCode('bundle_selection_ids');

            $bundleSelectionIds = $this->serializer->unserialize($selectionsQuoteItemOption->getValue());

            if (!empty($bundleSelectionIds)) {
                $selectionsCollection = $typeInstance->getSelectionsByIds($bundleSelectionIds, $product);

                $bundleOptions = $optionsCollection->appendSelections($selectionsCollection, true);
                foreach ($bundleOptions as $bundleOption) {
                    if ($bundleOption->getSelections()) {
                        $option = ['label' => $bundleOption->getTitle(), 'value' => []];

                        $bundleSelections = $bundleOption->getSelections();

                        foreach ($bundleSelections as $bundleSelection) {
                            $qty = $this->getSelectionQty($product, $bundleSelection->getSelectionId()) * 1;
                            if ($qty) {
                                $option['value'][] = $this->escaper->escapeHtml($bundleSelection->getName());
                                $option['has_html'] = true;
                            }
                        }

                        if ($option['value']) {
                            $options[] = $option;
                        }
                    }
                }
            }
        }

        return $options;
    }
}

However, the module is not working, and still returns the following:

Fatal error:  Class 'MySpace\MyModule3\Helper\Bundle\Catalog\Product\Configuration\Magento\Bundle\Helper\Catalog\Product\Configuration' not found in /data/web/magento2/app/code/MySpace/MyModule3/Helper/Bundle/Catalog/Product/Configuration/Configuration.php on line 3

Any ideas of how to correct for this error, and make the module work?

4
  • I think you forgot to add "\" before exdends class in "Configuration.php" file, It should be "\Magento\Bundle\Helper\Catalog\Product\Configuration" insted of "Magento\Bundle\Helper\Catalog\Product\Configuration" Commented Aug 27, 2019 at 7:50
  • Hi @Kishor After adjusting it as indicated; i get the following error when compiling: Warning: Declaration of MySpace\MyModule3\Helper\Bundle\Catalog\Product\Configuration\Configuration::getBundleOptions(MySpace\MyModule3\Helper\Bundle \Catalog\Product\Configuration\ItemInterface $item) should be compatible with Magento\Bundle\Helper\Catalog\Product\Configuration::getBundleOptions(M agento\Catalog\Model\Product\Configuration\Item\ItemInterface $item) in /data/web/magento2/app/code/MySpace/MyModule3/Helper/Bundle/Catalog/Product/Configuration/Configuration.php on line 65 Commented Aug 27, 2019 at 12:48
  • Not sure but you need to add this "use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;" under the namespace line and run the "php bin/magento setup:di:compile" command. Please check core file code to get more information. Commented Aug 27, 2019 at 13:08
  • The error is gone when compiling; now i get the following error on the screen: Notice: Undefined property: MySpace\MyModule3\Helper\Bundle\Catalog\Product\Configuration\Configuration\Interceptor::$serializer in /data/web/magento2/app/code/MySpace/MyModule3/Helper/Bundle/Catalog/Product/Configuration/Configuration.php on line 27 Commented Aug 27, 2019 at 13:42

1 Answer 1

0

In This FileMySpace/MyModule3/Helper/Bundle/Catalog/Product/Configuration/Configuration.php Change definition to class Configuration extends \Magento\Bundle\Helper\Catalog\Product\Configuration

1
  • Hi Diego. Thanks for your answer. I did it already. I also implemented “use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;" under the namespace line, as suggested by Kishor. Now i get the following error to see on the screen: Notice: Undefined property: MySpace\MyModule3\Helper\Bundle\Catalog\Product\Configuration\Configuration\Interceptor::$serializer in /data/web/magento2/app/code/MySpace/MyModule3/Helper/Bundle/Catalog/Product/Configuration/Configuration.php on line 27 Commented Aug 28, 2019 at 14:44

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.