Skip to content

Commit

Permalink
#26121: special price & tier price are coming in base currency
Browse files Browse the repository at this point in the history
Refactor to pass new optional parameter to @api class constructor
  • Loading branch information
pmarjan committed Jul 11, 2020
1 parent 75f71a4 commit 4c5086a
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 108 deletions.
134 changes: 134 additions & 0 deletions app/code/Magento/Catalog/Model/Product/Price/TierPriceBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Product\Price;

use Magento\Catalog\Api\Data\ProductTierPriceInterface;
use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
use Magento\Catalog\Api\Data\ProductTierPriceExtensionFactory;
use Magento\Catalog\Model\Product\Price\Validation\TierPriceValidator;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Builds ProductTierPriceInterface objects
*/
class TierPriceBuilder
{
/**
* @var int
*/
private $websiteId = 0;

/**
* @var ProductTierPriceInterfaceFactory
*/
protected $tierPriceFactory;

/**
* @var ProductTierPriceExtensionFactory
*/
private $tierPriceExtensionFactory;

/**
* @var ScopeConfigInterface
*/
private $config;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param ProductTierPriceInterfaceFactory $tierPriceFactory
* @param ProductTierPriceExtensionFactory $tierPriceExtensionFactory
* @param ScopeConfigInterface $config
* @param StoreManagerInterface $storeManager
*/
public function __construct(
ProductTierPriceInterfaceFactory $tierPriceFactory,
ProductTierPriceExtensionFactory $tierPriceExtensionFactory,
ScopeConfigInterface $config,
StoreManagerInterface $storeManager
) {
$this->tierPriceFactory = $tierPriceFactory;
$this->tierPriceExtensionFactory = $tierPriceExtensionFactory;
$this->config = $config;
$this->storeManager = $storeManager;
}

/**
* Transform the raw tier prices of the product into array of ProductTierPriceInterface objects
*
* @param array $tierPricesRaw
* @return ProductTierPriceInterface[]
*/
public function buildTierPriceObjects(array $tierPricesRaw): array
{
$prices = [];

foreach ($tierPricesRaw as $tierPriceRaw) {
$prices[] = $this->createTierPriceObjectFromRawData($tierPriceRaw);
}

return $prices;
}

/**
* Transform the raw tier price data into ProductTierPriceInterface object
*
* @param array $tierPriceRaw
* @return ProductTierPriceInterface
*/
private function createTierPriceObjectFromRawData(array $tierPriceRaw): ProductTierPriceInterface
{
//Find and set the website id that would be used as a fallback if the raw data does not bear it itself
$this->setWebsiteForPriceScope();

/** @var ProductTierPriceInterface $tierPrice */
$tierPrice = $this->tierPriceFactory->create()
->setExtensionAttributes($this->tierPriceExtensionFactory->create());

$tierPrice->setCustomerGroupId(
isset($tierPriceRaw['cust_group']) ? $tierPriceRaw['cust_group'] : ''
);
$tierPrice->setValue(
isset($tierPriceRaw['website_price']) ? $tierPriceRaw['website_price'] : $tierPriceRaw['price']
);
$tierPrice->setQty(
isset($tierPriceRaw['price_qty']) ? $tierPriceRaw['price_qty'] : ''
);
$tierPrice->getExtensionAttributes()->setWebsiteId(
isset($tierPriceRaw['website_id']) ? (int)$tierPriceRaw['website_id'] : $this->websiteId
);
if (isset($tierPriceRaw['percentage_value'])) {
$tierPrice->getExtensionAttributes()->setPercentageValue($tierPriceRaw['percentage_value']);
}

return $tierPrice;
}

/**
* Find and set the website id, based on the catalog price scope setting
*/
private function setWebsiteForPriceScope()
{
if ($this->websiteId != 0) {
return;
}

$websiteId = 0;
$value = $this->config->getValue('catalog/price/scope', ScopeInterface::SCOPE_WEBSITE);
if ($value != 0) {
$websiteId = $this->storeManager->getWebsite()->getId();
}

$this->websiteId = (int)$websiteId;
}
}
60 changes: 14 additions & 46 deletions app/code/Magento/Catalog/Model/Product/Type/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\Catalog\Model\Product\Type;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Price\TierPriceBuilder;
use Magento\Customer\Api\GroupManagementInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Store\Model\Store;
Expand Down Expand Up @@ -93,6 +94,11 @@ class Price
*/
private $tierPriceExtensionFactory;

/**
* @var TierPriceBuilder
*/
private $tierPriceBuilder;

/**
* Constructor
*
Expand All @@ -103,9 +109,10 @@ class Price
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param PriceCurrencyInterface $priceCurrency
* @param GroupManagementInterface $groupManagement
* @param \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory
* @param \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory @deprecated obsolete dependency
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param ProductTierPriceExtensionFactory|null $tierPriceExtensionFactory
* @param ProductTierPriceExtensionFactory|null $tierPriceExtensionFactory @deprecated obsolete dependency
* @param TierPriceBuilder $tierPriceBuilder
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -118,7 +125,8 @@ public function __construct(
GroupManagementInterface $groupManagement,
\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
ProductTierPriceExtensionFactory $tierPriceExtensionFactory = null
ProductTierPriceExtensionFactory $tierPriceExtensionFactory = null,
?TierPriceBuilder $tierPriceBuilder = null
) {
$this->_ruleFactory = $ruleFactory;
$this->_storeManager = $storeManager;
Expand All @@ -131,6 +139,8 @@ public function __construct(
$this->config = $config;
$this->tierPriceExtensionFactory = $tierPriceExtensionFactory ?: ObjectManager::getInstance()
->get(ProductTierPriceExtensionFactory::class);
$this->tierPriceBuilder = $tierPriceBuilder ?: ObjectManager::getInstance()
->get(TierPriceBuilder::class);
}

/**
Expand Down Expand Up @@ -371,49 +381,7 @@ public function getTierPrices($product)
{
$tierPricesRaw = $this->getExistingPrices($product, 'tier_price');

return $this->buildProductTierPriceInterfaceObjects($tierPricesRaw);
}

/**
* Return ProductTierPriceInterface[] given raw tier prices array
*
* @param array $tierPricesRaw
* @return \Magento\Catalog\Api\Data\ProductTierPriceInterface[]
*/
public function transformTierPrices($tierPricesRaw)
{
return $this->buildProductTierPriceInterfaceObjects($tierPricesRaw);
}

/**
* Return ProductTierPriceInterface[] given raw tier prices array
*
* @param array $tierPricesRaw
* @return \Magento\Catalog\Api\Data\ProductTierPriceInterface[]
*/
private function buildProductTierPriceInterfaceObjects($tierPricesRaw)
{
$prices = [];
foreach ($tierPricesRaw as $price) {
/** @var \Magento\Catalog\Api\Data\ProductTierPriceInterface $tierPrice */
$tierPrice = $this->tierPriceFactory->create()
->setExtensionAttributes($this->tierPriceExtensionFactory->create());
$tierPrice->setCustomerGroupId($price['cust_group']);
if (array_key_exists('website_price', $price)) {
$value = $price['website_price'];
} else {
$value = $price['price'];
}
$tierPrice->setValue($value);
$tierPrice->setQty($price['price_qty']);
if (isset($price['percentage_value'])) {
$tierPrice->getExtensionAttributes()->setPercentageValue($price['percentage_value']);
}
$websiteId = isset($price['website_id']) ? $price['website_id'] : $this->getWebsiteForPriceScope();
$tierPrice->getExtensionAttributes()->setWebsiteId($websiteId);
$prices[] = $tierPrice;
}
return $prices;
return $this->tierPriceBuilder->buildTierPriceObjects($tierPricesRaw);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions app/code/Magento/Catalog/Pricing/Price/TierPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class TierPrice extends AbstractPrice implements TierPriceInterface, BasePricePr
* @param Session $customerSession
* @param GroupManagementInterface $groupManagement
* @param CustomerGroupRetrieverInterface|null $customerGroupRetriever
* @param int|null $customerGroup
*/
public function __construct(
Product $saleableItem,
Expand All @@ -78,15 +79,18 @@ public function __construct(
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
Session $customerSession,
GroupManagementInterface $groupManagement,
CustomerGroupRetrieverInterface $customerGroupRetriever = null
CustomerGroupRetrieverInterface $customerGroupRetriever = null,
$customerGroup = null
) {
$quantity = (float)$quantity ? $quantity : 1;
parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
$this->customerSession = $customerSession;
$this->groupManagement = $groupManagement;
$this->customerGroupRetriever = $customerGroupRetriever
?? \Magento\Framework\App\ObjectManager::getInstance()->get(CustomerGroupRetrieverInterface::class);
if ($saleableItem->hasCustomerGroupId()) {
if ($customerGroup) {
$this->customerGroup = $customerGroup;
} elseif ($saleableItem->hasCustomerGroupId()) {
$this->customerGroup = (int) $saleableItem->getCustomerGroupId();
} else {
$this->customerGroup = (int) $this->customerGroupRetriever->getCustomerGroupId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Magento\Customer\Model\GroupManagement;
use Magento\Catalog\Api\Data\ProductTierPriceInterface;
use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool;
use Magento\Catalog\Model\Product\Type\Price;
use Magento\CatalogCustomerGraphQl\Pricing\Price\TierPriceFactory;
use Magento\Catalog\Pricing\Price\TierPriceFactory;
use Magento\Catalog\Model\Product\Price\TierPriceBuilder;

/**
* Get product tier price information
Expand Down Expand Up @@ -57,37 +57,37 @@ class Tiers
private $products = [];

/**
* @var Price
* @var TierPriceFactory
*/
private $price;
private $tierPriceFactory;

/**
* @var TierPriceFactory
* @var TierPriceBuilder
*/
private $tierPriceFactory;
private $tierPriceBuilder;

/**
* @param CollectionFactory $collectionFactory
* @param ProductResource $productResource
* @param PriceProviderPool $priceProviderPool
* @param int $customerGroupId
* @param Price $price
* @param TierPriceFactory $tierPriceFactory
* @param TierPriceBuilder $tierPriceBuilder
*/
public function __construct(
CollectionFactory $collectionFactory,
ProductResource $productResource,
PriceProviderPool $priceProviderPool,
$customerGroupId,
Price $price,
TierPriceFactory $tierPriceFactory
TierPriceFactory $tierPriceFactory,
TierPriceBuilder $tierPriceBuilder
) {
$this->collectionFactory = $collectionFactory;
$this->productResource = $productResource;
$this->priceProviderPool = $priceProviderPool;
$this->customerGroupId = $customerGroupId;
$this->price = $price;
$this->tierPriceFactory = $tierPriceFactory;
$this->tierPriceBuilder = $tierPriceBuilder;
}

/**
Expand Down Expand Up @@ -121,15 +121,15 @@ public function getProductTierPrices($productId): ?array
[
'saleableItem' => $this->products[$productId],
'quantity' => 1,
'customerGroupId' => $this->customerGroupId
'customerGroup' => $this->customerGroupId
]
);

/** @var array $tierPricesRaw */
$tierPricesRaw = $tierPrice->getTierPriceList();

/** @var ProductTierPriceInterface[] $tierPrices */
$tierPrices = $this->price->transformTierPrices($tierPricesRaw);
$tierPrices = $this->tierPriceBuilder->buildTierPriceObjects($tierPricesRaw);

return $tierPrices;
}
Expand Down

This file was deleted.

Loading

0 comments on commit 4c5086a

Please sign in to comment.