Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

magento/magento2#38564: Product overview in backend is showing price with currency symbol of filtered store view and default currency of default store view #39375

Open
wants to merge 2 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
namespace Magento\Catalog\Test\Unit\Ui\Component\Listing\Columns;

use Magento\Catalog\Ui\Component\Listing\Columns\Price;
use Magento\Directory\Model\Currency;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\Framework\Locale\CurrencyInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;

class PriceTest extends TestCase
Expand Down Expand Up @@ -74,10 +79,55 @@ protected function setUp(): void
* @param array $input
* @param array $output
* @return void
* @throws LocalizedException
* @throws NoSuchEntityException
* @throws Exception
* @dataProvider prepareDataSourceDataProvider
*/
public function testPrepareDataSource(array $input, array $output): void
{
$storeId = Store::DEFAULT_STORE_ID;
$this->context->expects($this->once())->method('getFilterParam')->willReturn($storeId);

$store = $this->createMock(Store::class);
$this->storeManager->expects($this->once())->method('getStore')->with($storeId)->willReturn($store);

$currency = $this->createMock(Currency::class);
$store->expects($this->once())->method('getCurrentCurrency')->willReturn($currency);
$store->expects($this->once())->method('getBaseCurrencyCode')->willReturn('USD');
$currency->expects($this->once())->method('getCode')->willReturn('USD');
$this->priceCurrency->expects($this->never())->method('convert');

$this->priceCurrency->method('format')
->willReturn('formatted');
$this->assertEquals($output, $this->model->prepareDataSource($input));
}

/**
* @param array $input
* @param array $output
* @return void
* @throws Exception
* @throws LocalizedException
* @throws NoSuchEntityException
* @dataProvider prepareDataSourceDataProvider
*/
public function testPrepareDataSourceWithPriceConversion(array $input, array $output): void
{
$storeId = Store::DEFAULT_STORE_ID;
$price = $input['data']['items'][0]['price'];
$this->context->expects($this->once())->method('getFilterParam')->willReturn($storeId);

$store = $this->createMock(Store::class);
$this->storeManager->expects($this->once())->method('getStore')->with($storeId)->willReturn($store);

$currency = $this->createMock(Currency::class);
$store->expects($this->once())->method('getCurrentCurrency')->willReturn($currency);
$store->expects($this->once())->method('getBaseCurrencyCode')->willReturn('USD');
$currency->expects($this->once())->method('getCode')->willReturn('EUR');

$this->priceCurrency->expects($this->once())->method('convert')->with($price, $store, $currency);

$this->priceCurrency->method('format')
->willReturn('formatted');
$this->assertEquals($output, $this->model->prepareDataSource($input));
Expand Down
15 changes: 14 additions & 1 deletion app/code/Magento/Catalog/Ui/Component/Listing/Columns/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Magento\Catalog\Ui\Component\Listing\Columns;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
Expand Down Expand Up @@ -65,6 +67,8 @@ public function __construct(
*
* @param array $dataSource
* @return array
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function prepareDataSource(array $dataSource)
{
Expand All @@ -73,11 +77,20 @@ public function prepareDataSource(array $dataSource)
$this->context->getFilterParam('store_id', \Magento\Store\Model\Store::DEFAULT_STORE_ID)
);

$currency = $store->getCurrentCurrency();
$convert = $store->getBaseCurrencyCode() !== $currency->getCode();

$fieldName = $this->getData('name');
foreach ($dataSource['data']['items'] as & $item) {
if (isset($item[$fieldName])) {

$price = $item[$fieldName];
if ($convert) {
$price = $this->priceCurrency->convert($price, $store, $currency);
}

$item[$fieldName] = $this->priceCurrency->format(
sprintf("%F", $item[$fieldName]),
sprintf("%F", $price),
false,
PriceCurrencyInterface::DEFAULT_PRECISION,
$store
Expand Down