4

I try to override getSelectionTitlePrice() - function. (vendor/magento/module-bundle/Block/Catalog/Product/View/Type/Bundle/Option.php)

if i have understood right the best way to do it is use preference. Have been following lot of answers from everywhere but could not solve the problem. How to Override Core Block, Model and controller in Magento2

when i override getProduct() - function (/Block/Product/View.php) everything work but with getSelectionTitlePrice() - function it doesn't

I tried to solve problem using Plugin but it just extend function not override it?!?

app/code/MySpace/MyModule/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\Block\Catalog\Product\View\Type\Bundle\Option" type="MySpace\MyModule\Block\Catalog\Product\View\Type\Bundle\Option" />
</config>

app/code/MySpace/MyModule/Block/Catalog/Product/View/Type/Bundle/Option.php

<?php
namespace MySpace\MyModule\Block\Catalog\Product\View\Type\Bundle;

/**
* Bundle option renderer
*/

class Option extends \Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option
{

public function getSelectionTitlePrice($selection, $includeContainer = true)
{
    /** will be removed **/
        $logger = \Magento\Framework\App\ObjectManager::getInstance()->get('\Psr\Log\LoggerInterface');
        $logger->debug('Block Override Test');
    /** will be removed **/

    $priceTitle = '<span class="product-name">' . $this->escapeHtml($selection->getName()) . '</span>';
    $priceTitle .= ' &nbsp; ' . ($includeContainer ? '<span class="price-notice">' : '')
        . $this->renderPriceString($selection, $includeContainer) . ($includeContainer ? '</span>' : '') 
        .'<span> xxx->getTaxPercent(fromSomewhere)</span>';
    return $priceTitle;
 }
}
5

To replace a single method, use the around plugin and don't call the original method with $proceed()

  • Preference is deprecated? – Erfan Feb 25 '17 at 5:30
  • 1
    No, but if something can be done with a plugin, it's the better way. The risk of conflicts is much lower and it is generally more upgrade safe – Fabian Schmengler Feb 25 '17 at 9:06
4

Thanks fschmengler , I managed to solve this by using Plugin.

Override/replace single method in block class:

app/code/MySpace/MyModule/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Radio">
        <plugin name="bundle-product-option-radio" type="MySpace\MyModule\Plugin\RadioPlugin" />
    </type>
</config>

app/code/MySpace/MyModule/Plugin/RadioPlugin.php

<?php

namespace MySpace\MyModule\Plugin;

class RadioPlugin
{

     public function aroundGetSelectionTitlePrice(
        \Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Radio $subject,
        \Closure $proceed,
        $selection,
        $includeContainer = true)
    {
        $priceTitle = '<span class="product-name">' . $subject->escapeHtml($selection->getName()) . '</span>';
        $priceTitle .= ' &nbsp; ' . ($includeContainer ? '<span class="price-notice">' : '')
            . $subject->renderPriceString($selection, $includeContainer) . ($includeContainer ? '</span>' : '')
            .'<span> xxx->getTaxPercent(fromSomewhere)</span>';
        return $priceTitle;
    }
}

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.