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

1 Answer 1

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

Your Answer

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

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