TierPrice.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Pricing\Price;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Customer\Api\GroupManagementInterface;
  9. use Magento\Customer\Model\Session;
  10. use Magento\Framework\Pricing\Adjustment\CalculatorInterface;
  11. use Magento\Framework\Pricing\Amount\AmountInterface;
  12. use Magento\Framework\Pricing\Price\AbstractPrice;
  13. use Magento\Framework\Pricing\Price\BasePriceProviderInterface;
  14. use Magento\Framework\Pricing\PriceInfoInterface;
  15. use Magento\Customer\Model\Group\RetrieverInterface as CustomerGroupRetrieverInterface;
  16. /**
  17. * @api
  18. * @since 100.0.2
  19. */
  20. class TierPrice extends AbstractPrice implements TierPriceInterface, BasePriceProviderInterface
  21. {
  22. /**
  23. * Price type tier
  24. */
  25. const PRICE_CODE = 'tier_price';
  26. /**
  27. * @var Session
  28. * @deprecated 102.0.0
  29. */
  30. protected $customerSession;
  31. /**
  32. * @var int
  33. */
  34. protected $customerGroup;
  35. /**
  36. * Raw price list stored in DB
  37. *
  38. * @var array
  39. */
  40. protected $rawPriceList;
  41. /**
  42. * Applicable price list
  43. *
  44. * @var array
  45. */
  46. protected $priceList;
  47. /**
  48. * @var GroupManagementInterface
  49. */
  50. protected $groupManagement;
  51. /**
  52. * @var CustomerGroupRetrieverInterface
  53. */
  54. private $customerGroupRetriever;
  55. /**
  56. * @param Product $saleableItem
  57. * @param float $quantity
  58. * @param CalculatorInterface $calculator
  59. * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  60. * @param Session $customerSession
  61. * @param GroupManagementInterface $groupManagement
  62. * @param CustomerGroupRetrieverInterface|null $customerGroupRetriever
  63. */
  64. public function __construct(
  65. Product $saleableItem,
  66. $quantity,
  67. CalculatorInterface $calculator,
  68. \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
  69. Session $customerSession,
  70. GroupManagementInterface $groupManagement,
  71. CustomerGroupRetrieverInterface $customerGroupRetriever = null
  72. ) {
  73. $quantity = (float)$quantity ? $quantity : 1;
  74. parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
  75. $this->customerSession = $customerSession;
  76. $this->groupManagement = $groupManagement;
  77. $this->customerGroupRetriever = $customerGroupRetriever
  78. ?? \Magento\Framework\App\ObjectManager::getInstance()->get(CustomerGroupRetrieverInterface::class);
  79. if ($saleableItem->hasCustomerGroupId()) {
  80. $this->customerGroup = (int) $saleableItem->getCustomerGroupId();
  81. } else {
  82. $this->customerGroup = (int) $this->customerGroupRetriever->getCustomerGroupId();
  83. }
  84. }
  85. /**
  86. * Get price value
  87. *
  88. * @return bool|float
  89. */
  90. public function getValue()
  91. {
  92. if (null === $this->value) {
  93. $prices = $this->getStoredTierPrices();
  94. $prevQty = PriceInfoInterface::PRODUCT_QUANTITY_DEFAULT;
  95. $this->value = $prevPrice = $tierPrice = false;
  96. $priceGroup = $this->groupManagement->getAllCustomersGroup()->getId();
  97. foreach ($prices as $price) {
  98. if (!$this->canApplyTierPrice($price, $priceGroup, $prevQty)) {
  99. continue;
  100. }
  101. if (false === $prevPrice || $this->isFirstPriceBetter($price['website_price'], $prevPrice)) {
  102. $tierPrice = $prevPrice = $price['website_price'];
  103. $prevQty = $price['price_qty'];
  104. $priceGroup = $price['cust_group'];
  105. $this->value = (float)$tierPrice;
  106. }
  107. }
  108. }
  109. return $this->value;
  110. }
  111. /**
  112. * Returns true if first price is better
  113. *
  114. * Method filters tiers price values, lower tier price value is better
  115. *
  116. * @param float $firstPrice
  117. * @param float $secondPrice
  118. * @return bool
  119. */
  120. protected function isFirstPriceBetter($firstPrice, $secondPrice)
  121. {
  122. return $firstPrice < $secondPrice;
  123. }
  124. /**
  125. * @return int
  126. */
  127. public function getTierPriceCount()
  128. {
  129. return count($this->getTierPriceList());
  130. }
  131. /**
  132. * @return array
  133. */
  134. public function getTierPriceList()
  135. {
  136. if (null === $this->priceList) {
  137. $priceList = $this->getStoredTierPrices();
  138. $this->priceList = $this->filterTierPrices($priceList);
  139. array_walk(
  140. $this->priceList,
  141. function (&$priceData) {
  142. /* convert string value to float */
  143. $priceData['price_qty'] = $priceData['price_qty'] * 1;
  144. $priceData['price'] = $this->applyAdjustment($priceData['price']);
  145. }
  146. );
  147. }
  148. return $this->priceList;
  149. }
  150. /**
  151. * @param array $priceList
  152. * @return array
  153. */
  154. protected function filterTierPrices(array $priceList)
  155. {
  156. $qtyCache = [];
  157. $allCustomersGroupId = $this->groupManagement->getAllCustomersGroup()->getId();
  158. foreach ($priceList as $priceKey => &$price) {
  159. if ($price['price'] >= $this->priceInfo->getPrice(FinalPrice::PRICE_CODE)->getValue()) {
  160. unset($priceList[$priceKey]);
  161. continue;
  162. }
  163. if (isset($price['price_qty']) && $price['price_qty'] == 1) {
  164. unset($priceList[$priceKey]);
  165. continue;
  166. }
  167. /* filter price by customer group */
  168. if ($price['cust_group'] != $this->customerGroup &&
  169. $price['cust_group'] != $allCustomersGroupId) {
  170. unset($priceList[$priceKey]);
  171. continue;
  172. }
  173. /* select a lower price for each quantity */
  174. if (isset($qtyCache[$price['price_qty']])) {
  175. $priceQty = $qtyCache[$price['price_qty']];
  176. if ($this->isFirstPriceBetter($price['website_price'], $priceList[$priceQty]['website_price'])) {
  177. unset($priceList[$priceQty]);
  178. $qtyCache[$price['price_qty']] = $priceKey;
  179. } else {
  180. unset($priceList[$priceKey]);
  181. }
  182. } else {
  183. $qtyCache[$price['price_qty']] = $priceKey;
  184. }
  185. }
  186. return array_values($priceList);
  187. }
  188. /**
  189. * @return float
  190. */
  191. protected function getBasePrice()
  192. {
  193. /** @var float $productPrice is a minimal available price */
  194. return $this->priceInfo->getPrice(BasePrice::PRICE_CODE)->getValue();
  195. }
  196. /**
  197. * Calculates savings percentage according to the given tier price amount
  198. * and related product price amount.
  199. *
  200. * @param AmountInterface $amount
  201. *
  202. * @return float
  203. */
  204. public function getSavePercent(AmountInterface $amount)
  205. {
  206. $productPriceAmount = $this->priceInfo->getPrice(
  207. FinalPrice::PRICE_CODE
  208. )->getAmount();
  209. return round(
  210. 100 - ((100 / $productPriceAmount->getValue()) * $amount->getValue())
  211. );
  212. }
  213. /**
  214. * @param float|string $price
  215. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  216. */
  217. protected function applyAdjustment($price)
  218. {
  219. return $this->calculator->getAmount($price, $this->product);
  220. }
  221. /**
  222. * Can apply tier price
  223. *
  224. * @param array $currentTierPrice
  225. * @param int $prevPriceGroup
  226. * @param float|string $prevQty
  227. * @return bool
  228. */
  229. protected function canApplyTierPrice(array $currentTierPrice, $prevPriceGroup, $prevQty)
  230. {
  231. $custGroupAllId = (int)$this->groupManagement->getAllCustomersGroup()->getId();
  232. // Tier price can be applied, if:
  233. // tier price is for current customer group or is for all groups
  234. if ((int)$currentTierPrice['cust_group'] !== $this->customerGroup
  235. && (int)$currentTierPrice['cust_group'] !== $custGroupAllId
  236. ) {
  237. return false;
  238. }
  239. // and tier qty is lower than product qty
  240. if ($this->quantity < $currentTierPrice['price_qty']) {
  241. return false;
  242. }
  243. // and tier qty is bigger than previous qty
  244. if ($currentTierPrice['price_qty'] < $prevQty) {
  245. return false;
  246. }
  247. // and found tier qty is same as previous tier qty, but current tier group isn't ALL_GROUPS
  248. if ($currentTierPrice['price_qty'] == $prevQty
  249. && $prevPriceGroup !== $custGroupAllId
  250. && $currentTierPrice['cust_group'] === $custGroupAllId
  251. ) {
  252. return false;
  253. }
  254. return true;
  255. }
  256. /**
  257. * Get clear tier price list stored in DB
  258. *
  259. * @return array
  260. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  261. */
  262. protected function getStoredTierPrices()
  263. {
  264. if (null === $this->rawPriceList) {
  265. $this->rawPriceList = $this->product->getData(self::PRICE_CODE);
  266. if (null === $this->rawPriceList || !is_array($this->rawPriceList)) {
  267. /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */
  268. $attribute = $this->product->getResource()->getAttribute(self::PRICE_CODE);
  269. if ($attribute) {
  270. $attribute->getBackend()->afterLoad($this->product);
  271. $this->rawPriceList = $this->product->getData(self::PRICE_CODE);
  272. }
  273. }
  274. if (null === $this->rawPriceList || !is_array($this->rawPriceList)) {
  275. $this->rawPriceList = [];
  276. }
  277. if (!$this->isPercentageDiscount()) {
  278. foreach ($this->rawPriceList as $index => $rawPrice) {
  279. if (isset($rawPrice['price'])) {
  280. $this->rawPriceList[$index]['price'] =
  281. $this->priceCurrency->convertAndRound($rawPrice['price']);
  282. }
  283. if (isset($rawPrice['website_price'])) {
  284. $this->rawPriceList[$index]['website_price'] =
  285. $this->priceCurrency->convertAndRound($rawPrice['website_price']);
  286. }
  287. }
  288. }
  289. }
  290. return $this->rawPriceList;
  291. }
  292. /**
  293. * @return bool
  294. */
  295. public function isPercentageDiscount()
  296. {
  297. return false;
  298. }
  299. }