0

I would like to get a custom attribute "bum" (base unit of measure) on the page "final_price.phtml". Do you know if it's possible to add a custom attribute after the price to something like this:

  • before : $ 2.35
  • after : $ 2.35 per RL

final_price.phtml: enter image description here

Thank you for your help

0

Try following way:

Create plugin


<type name="Magento\Framework\Pricing\Render\PriceBox">
    <plugin name="sr_quantity_type_product_render_amount" type="Vendor\Module\Plugin\Pricing\Render\PriceBox" sortOrder="1"/>
</type>

Vendor/Module/Plugin/Pricing/Render/PriceBox.php


namespace Vendor\Module\Plugin\Pricing\Render;

use Magento\Framework\Pricing\Amount\AmountInterface;

class PriceBox
{
    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * PriceBox constructor.
     *
     * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
     */
    public function __construct(\Magento\Catalog\Api\ProductRepositoryInterface $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    public function aroundRenderAmount(
        \Magento\Framework\Pricing\Render\PriceBox $subject,
        \Closure $proceed,
        AmountInterface $amount,
        array $arguments = []
    ) {
        $html = $proceed($amount, $arguments);
        $product = $this->productRepository->getById($subject->getSaleableItem()->getId());
        $bum = $product->getBum() ? $product->getAttributeText('bum') : '';
        if ($bum) {
            $html .= '<div>' . __('per') . ' ' .$bum . '</div>';;
        }

        return $html;
    }
}
| improve this answer | |
  • Thanks for the repply. I don't have the folder 'Module' in 'Vendor'. I have to create it? – VopM Jul 14 '17 at 7:31
  • Yes, it's your custom module. – Sohel Rana Jul 14 '17 at 7:33
  • Ok, can you say me where I have to put the first part (<type ... </type>) ? – VopM Jul 14 '17 at 7:46
  • Inside di.xml, read more devdocs.magento.com/guides/v2.1/extension-dev-guide/… – Sohel Rana Jul 14 '17 at 7:49
  • I put the di.xml file here: /vendor/module/plugin/pricing/etc/di.xml, is it correct? – VopM Jul 14 '17 at 8:14

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.