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;
 }
}
Fabian Schmengler
66.2k25 gold badges191 silver badges422 bronze badges
asked Oct 25, 2016 at 15:42

2 Answers 2

5

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

answered Oct 25, 2016 at 16:47
2
  • Preference is deprecated? Commented Feb 25, 2017 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 Commented Feb 25, 2017 at 9:06
5

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;
 }
}
answered Oct 26, 2016 at 10:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.