Your IP : 216.73.217.13


Current Path : /var/www/www.indacotrentino.com/www/app/code/Webkul/BuyButton/Plugin/
Upload File :
Current File : //var/www/www.indacotrentino.com/www/app/code/Webkul/BuyButton/Plugin/PriceBuilder.php

<?php
/**
 * Webkul Software
 *
 * @category Webkul
 * @package Webkul_BuyButton
 * @author Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license https://store.webkul.com/license.html
 */
namespace Webkul\BuyButton\Plugin;

use Magento\Catalog\Api\Data\ProductRender\FormattedPriceInfoInterfaceFactory;
use Magento\Framework\Pricing\PriceCurrencyInterface;

class PriceBuilder
{
    /**
     * @var FormattedPriceInfoInterfaceFactory
     */
    private $formattedPriceInfoFactory;

    /**
     * @var PriceCurrencyInterface
     */
    private $priceCurrency;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        PriceCurrencyInterface $priceCurrency,
        FormattedPriceInfoInterfaceFactory $formattedPriceInfoFactory,
        \Magento\Framework\Session\SessionManagerInterface $coreSession
    ) {

        $this->formattedPriceInfoFactory = $formattedPriceInfoFactory;
        $this->priceCurrency = $priceCurrency;
        $this->storeManager = $storeManager;
        $this->_coreSession = $coreSession;
    }

    public function aroundBuild(
        \Magento\Catalog\Model\ProductRender\FormattedPriceInfoBuilder $subject,
        \Closure $proceed,
        $priceInfo,
        $storeId,
        $currencyCode
    ) {
        $currRate = $this->storeManager->getStore()->getCurrentCurrencyRate();
        /** @var FormattedPriceInfo $formattedPriceInfo */
        $formattedPriceInfo = $this->formattedPriceInfoFactory->create();
        $this->_coreSession->start();
        $this->_coreSession->setCurrencyCode($currencyCode);
        foreach ($priceInfo->getData() as $key => $value) {
            if (is_numeric($value)) {
                $value = $value / $currRate;
                $value = $this->priceCurrency->convert($value, $storeId, $currencyCode);
                $priceInfo->setData($key, $value);
                $formattedValue = $this->priceCurrency
                    ->format(
                        $value,
                        true,
                        PriceCurrencyInterface::DEFAULT_PRECISION,
                        $storeId,
                        $currencyCode
                    );
                $formattedPriceInfo->setData($key, $formattedValue);
            }
        }
        $priceInfo->setFormattedPrices($formattedPriceInfo);
    }
}