Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FinalPriceBox.php
Go to the documentation of this file.
1 <?php
8 
19 
26 class FinalPriceBox extends BasePriceBox
27 {
31  private $salableResolver;
32 
36  private $minimalPriceCalculator;
37 
47  public function __construct(
48  Context $context,
52  array $data = [],
53  SalableResolverInterface $salableResolver = null,
54  MinimalPriceCalculatorInterface $minimalPriceCalculator = null
55  ) {
56  parent::__construct($context, $saleableItem, $price, $rendererPool, $data);
57  $this->salableResolver = $salableResolver ?: ObjectManager::getInstance()->get(SalableResolverInterface::class);
58  $this->minimalPriceCalculator = $minimalPriceCalculator
59  ?: ObjectManager::getInstance()->get(MinimalPriceCalculatorInterface::class);
60  }
61 
65  protected function _toHtml()
66  {
67  if (!$this->salableResolver->isSalable($this->getSaleableItem())) {
68  return '';
69  }
70 
71  $result = parent::_toHtml();
72  //Renders MSRP in case it is enabled
73  if ($this->isMsrpPriceApplicable()) {
75  $msrpBlock = $this->rendererPool->createPriceRender(
76  MsrpPrice::PRICE_CODE,
77  $this->getSaleableItem(),
78  [
79  'real_price_html' => $result,
80  'zone' => $this->getZone(),
81  ]
82  );
83  $result = $msrpBlock->toHtml();
84  }
85 
86  return $this->wrapResult($result);
87  }
88 
94  protected function isMsrpPriceApplicable()
95  {
96  try {
98  $msrpPriceType = $this->getSaleableItem()->getPriceInfo()->getPrice('msrp_price');
99  } catch (\InvalidArgumentException $e) {
100  $this->_logger->critical($e);
101  return false;
102  }
103 
104  $product = $this->getSaleableItem();
105  return $msrpPriceType->canApplyMsrp($product) && $msrpPriceType->isMinimalPriceLessMsrp($product);
106  }
107 
114  protected function wrapResult($html)
115  {
116  return '<div class="price-box ' . $this->getData('css_classes') . '" ' .
117  'data-role="priceBox" ' .
118  'data-product-id="' . $this->getSaleableItem()->getId() . '" ' .
119  'data-price-box="product-id-' . $this->getSaleableItem()->getId() . '"' .
120  '>' . $html . '</div>';
121  }
122 
128  public function renderAmountMinimal()
129  {
130  $id = $this->getPriceId() ? $this->getPriceId() : 'product-minimal-price-' . $this->getSaleableItem()->getId();
131 
132  $amount = $this->minimalPriceCalculator->getAmount($this->getSaleableItem());
133  if ($amount === null) {
134  return '';
135  }
136 
137  return $this->renderAmount(
138  $amount,
139  [
140  'display_label' => __('As low as'),
141  'price_id' => $id,
142  'include_container' => false,
143  'skip_adjustments' => true
144  ]
145  );
146  }
147 
153  public function hasSpecialPrice()
154  {
155  $displayRegularPrice = $this->getPriceType(Price\RegularPrice::PRICE_CODE)->getAmount()->getValue();
156  $displayFinalPrice = $this->getPriceType(Price\FinalPrice::PRICE_CODE)->getAmount()->getValue();
157  return $displayFinalPrice < $displayRegularPrice;
158  }
159 
165  public function showMinimalPrice()
166  {
167  $minTierPrice = $this->minimalPriceCalculator->getValue($this->getSaleableItem());
168 
170  $finalPrice = $this->getPriceType(Price\FinalPrice::PRICE_CODE);
171  $finalPriceValue = $finalPrice->getAmount()->getValue();
172 
173  return $this->getDisplayMinimalPrice()
174  && $minTierPrice !== null
175  && $minTierPrice < $finalPriceValue;
176  }
177 
183  public function getCacheKey()
184  {
185  return parent::getCacheKey() . ($this->getData('list_category_page') ? '-list-category-page': '');
186  }
187 
193  public function getCacheKeyInfo()
194  {
195  $cacheKeys = parent::getCacheKeyInfo();
196  $cacheKeys['display_minimal_price'] = $this->getDisplayMinimalPrice();
197  $cacheKeys['is_product_list'] = $this->isProductList();
198  return $cacheKeys;
199  }
200 
207  public function isProductList()
208  {
209  $isProductList = $this->getData('is_product_list');
210  return $isProductList === true;
211  }
212 }
getData($key='', $index=null)
Definition: DataObject.php:119
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$amount
Definition: order.php:14
getPriceId($defaultPrefix=null, $defaultSuffix=null)
Definition: PriceBox.php:115
__construct(Context $context, SaleableInterface $saleableItem, PriceInterface $price, RendererPool $rendererPool, array $data=[], SalableResolverInterface $salableResolver=null, MinimalPriceCalculatorInterface $minimalPriceCalculator=null)
renderAmount(AmountInterface $amount, array $arguments=[])
Definition: PriceBox.php:143