1

How to override this file in Magento2

Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Options

This code is in my 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\Bundle\Block\Catalog\Product\View\Type\Bundle\Options" type="Magentoo\Bundle\Block\Catalog\Product\View\Type\Bundle\Option" />
</config>
2
  • A plugin is the best way to change Magento core functionality only public methods can be modified – Devidas Feb 18 '20 at 9:36
  • how can i create it – Junaid Ashfaq Feb 18 '20 at 9:42
1

Replace your code with following code in your 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\Bundle\Block\Catalog\Product\View\Type\Bundle\Option" type="Magentoo\Bundle\Block\Catalog\Product\View\Type\Bundle\Option" />
</config>
3
  • it not show changes – Junaid Ashfaq Feb 18 '20 at 9:26
  • give me a proper way to override this file – Junaid Ashfaq Feb 18 '20 at 9:27
  • its 'Option' not 'Options', check in your code – Hiren Feb 18 '20 at 11:05
0

Create etc/frontend/di.xml

<type name="\Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Options">
        <plugin name="Custom_Module::aftershowSingle"
                type="Custom\Module\Plugin\Block\Catalog\Product\View\Type\Bundle\Options"
                sortOrder="10" />
</type>

Then create a plugin in your custom module and override path in your module and implement code as your functionality

<?php
/**
namespace Custom\Module\Plugin\Block\Catalog\Product\View\Type\Bundle;

class Option
{
    /**
     * @var \Magento\Framework\Registry
     */
    protected $_registry;

    /**
     * @param \Magento\Framework\Registry $registry
     */
    public function __construct(
        \Magento\Framework\Registry $registry
    ) {
        $this->_registry = $registry;
    }

    /**
     *
     * @param array $result
     *
     * @return bool
     */
    public function aftershowSingle(
        \Magento\Catalog\Model\ResourceModel\Product\Collection $collection,
        $result
    ) {
        if ($this->_registry->registry('mp_flat_catalog_flag')) {
            $result = 0;
        }
        return $result;
    }
}

I just override showsingle method.

Note

• Plugins only work on public methods.

• They do not work on final methods, final classes.

• They must be configured in di.xml.

Important: plugins can be used on interfaces, abstract classes or parent classes. The plugin methods will be called for any implementation of those abstractions.

0

Simple way. You can copy that file and paste in Vendor\YourModuleName\Block\Catalog\Product\View\Type\Bundle\Options folder.

Just change whatever you need to change in that file. remove the functions whichever you are not using. run the M2 commands and check the changes.

Create di.xml file with below content:

<?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\Bundle\Block\Catalog\Product\View\Type\Bundle\Options" type="Vendor\YourModuleName\Block\Catalog\Product\View\Type\Bundle\Options"/>
</config>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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