0

How to get Product Id in FinalPriceBox Plugin in magento 2

Using FinalPriceBox Plugin to hide price in all page how to get a product id within this plugin.

/app/code/Vendor/Module/etc/di.xml

<type name="Magento\Catalog\Pricing\Render\FinalPriceBox">
     <plugin name="vendor_module_finalpricebox" type="Vendor\Module\Plugin\FinalPriceBox"/>
</type>

/app/code/Vendor/Module/Plugin/FinalPriceBox.php

<?php 
namespace Vendor\Module\Plugin;

use Vendor\Module\Block\Button;
use Vendor\Module\Helper\HidePrice;
use Magento\Catalog\Pricing\Render\FinalPriceBox as FinalPriceBoxRenderer;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

use function strstr;

/**
 * Class FinalPriceBox
 */
class FinalPriceBox extends \Magento\Catalog\Pricing\Render\FinalPriceBox
{
    /** @var ScopeConfigInterface */
    private $config;
    private $buttonBlock;
    private $hidePriceHelper;
     protected $productRepository;
    /**
     * FinalPriceBox constructor.
     * @param ScopeConfigInterface $config
     * @param Button $buttonBlock
     * @param Data $hidePriceHelper
     */
    public function __construct(
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        ScopeConfigInterface $config,
        HidePrice $hidePriceHelper,
        Button $buttonBlock
    )
    {
        $this->config          = $config;
        $this->buttonBlock     = $buttonBlock;
        $this->hidePriceHelper = $hidePriceHelper;
        $this->productRepository = $productRepository;


    }

    /**
     * @param FinalPriceBoxRenderer $finalPriceBox
     * @param $result
     * @return string
     */
    public function aroundToHtml(FinalPriceBoxRenderer $finalPriceBox, $result)
    {

        //How to get Product id in this plugin
        $template = 'button.phtml';
        return $this->buttonBlock
                    ->setTemplate('Vendor_Module::' . $template)
                    ->toHtml();
    }
}

enter image description here

2
  • Did you found solution? Feb 13 '20 at 9:10
  • no, i didn`t found any solution Feb 13 '20 at 9:23
1

You can use this below plugin code :

<?php
namespace Vendor\Module\Plugin;

/**
 * Class FinalPriceBox
 */
class FinalPriceBox {

    public function aroundToHtml($subject, callable $proceed) {
        if ($subject->getSaleableItem()->getId() == 1) {
            return ''; // Remove final price box
        } else {
            return $proceed(); // Display final price box
        }

    }
}

You can get product id by this code : $subject->getSaleableItem()->getId()

Hope, It will helpful for you.

3
  • i was already tried a above method its reflects prnt.sc/r1ogjg Feb 13 '20 at 9:43
  • Can you please upload your plugin full code after use my answer? Feb 13 '20 at 9:45
  • 1
    its workings i fix the issue in di.xml file Feb 13 '20 at 9:50
0
/**
     * @param FinalPriceBoxRenderer $finalPriceBox
     * @param $result
     * @return string
     */
    public function aroundToHtml(FinalPriceBoxRenderer $finalPriceBox, $result)
    {
        // it gets you the product id
        $productId = $this->getSaleableItem()->getId();

       // $this->getSaleableItem() holds the instance of the current product

        $template = 'button.phtml';
        return $this->buttonBlock
                    ->setTemplate('Vendor_Module::' . $template)
                    ->toHtml();
    }    
1
  • I was already used this term $this->getSaleableItem()->getId(); but its gives on error as a call to undefinite getId() Feb 13 '20 at 6:58

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.