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 .= ' ' . ($includeContainer ? '<span class="price-notice">' : '')
. $this->renderPriceString($selection, $includeContainer) . ($includeContainer ? '</span>' : '')
.'<span> xxx->getTaxPercent(fromSomewhere)</span>';
return $priceTitle;
}
}
2 Answers 2
To replace a single method, use the around plugin and don't call the original method with $proceed()
-
-
1No, 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 safeFabian Schmengler– Fabian Schmengler2017年02月25日 09:06:38 +00:00Commented Feb 25, 2017 at 9:06
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 .= ' ' . ($includeContainer ? '<span class="price-notice">' : '')
. $subject->renderPriceString($selection, $includeContainer) . ($includeContainer ? '</span>' : '')
.'<span> xxx->getTaxPercent(fromSomewhere)</span>';
return $priceTitle;
}
}
Explore related questions
See similar questions with these tags.