123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Catalog\Pricing\Render;
- use Magento\Catalog\Pricing\Price;
- use Magento\Framework\Pricing\Render\PriceBox as BasePriceBox;
- use Magento\Msrp\Pricing\Price\MsrpPrice;
- use Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface;
- use Magento\Framework\View\Element\Template\Context;
- use Magento\Framework\Pricing\SaleableInterface;
- use Magento\Framework\Pricing\Price\PriceInterface;
- use Magento\Framework\Pricing\Render\RendererPool;
- use Magento\Framework\App\ObjectManager;
- use Magento\Catalog\Pricing\Price\MinimalPriceCalculatorInterface;
- /**
- * Class for final_price rendering
- *
- * @method bool getUseLinkForAsLowAs()
- * @method bool getDisplayMinimalPrice()
- */
- class FinalPriceBox extends BasePriceBox
- {
- /**
- * @var SalableResolverInterface
- */
- private $salableResolver;
- /**
- * @var MinimalPriceCalculatorInterface
- */
- private $minimalPriceCalculator;
- /**
- * @param Context $context
- * @param SaleableInterface $saleableItem
- * @param PriceInterface $price
- * @param RendererPool $rendererPool
- * @param array $data
- * @param SalableResolverInterface $salableResolver
- * @param MinimalPriceCalculatorInterface $minimalPriceCalculator
- */
- public function __construct(
- Context $context,
- SaleableInterface $saleableItem,
- PriceInterface $price,
- RendererPool $rendererPool,
- array $data = [],
- SalableResolverInterface $salableResolver = null,
- MinimalPriceCalculatorInterface $minimalPriceCalculator = null
- ) {
- parent::__construct($context, $saleableItem, $price, $rendererPool, $data);
- $this->salableResolver = $salableResolver ?: ObjectManager::getInstance()->get(SalableResolverInterface::class);
- $this->minimalPriceCalculator = $minimalPriceCalculator
- ?: ObjectManager::getInstance()->get(MinimalPriceCalculatorInterface::class);
- }
- /**
- * @return string
- */
- protected function _toHtml()
- {
- if (!$this->salableResolver->isSalable($this->getSaleableItem())) {
- return '';
- }
- $result = parent::_toHtml();
- //Renders MSRP in case it is enabled
- if ($this->isMsrpPriceApplicable()) {
- /** @var BasePriceBox $msrpBlock */
- $msrpBlock = $this->rendererPool->createPriceRender(
- MsrpPrice::PRICE_CODE,
- $this->getSaleableItem(),
- [
- 'real_price_html' => $result,
- 'zone' => $this->getZone(),
- ]
- );
- $result = $msrpBlock->toHtml();
- }
- return $this->wrapResult($result);
- }
- /**
- * Check is MSRP applicable for the current product.
- *
- * @return bool
- */
- protected function isMsrpPriceApplicable()
- {
- try {
- /** @var MsrpPrice $msrpPriceType */
- $msrpPriceType = $this->getSaleableItem()->getPriceInfo()->getPrice('msrp_price');
- } catch (\InvalidArgumentException $e) {
- $this->_logger->critical($e);
- return false;
- }
- $product = $this->getSaleableItem();
- return $msrpPriceType->canApplyMsrp($product) && $msrpPriceType->isMinimalPriceLessMsrp($product);
- }
- /**
- * Wrap with standard required container
- *
- * @param string $html
- * @return string
- */
- protected function wrapResult($html)
- {
- return '<div class="price-box ' . $this->getData('css_classes') . '" ' .
- 'data-role="priceBox" ' .
- 'data-product-id="' . $this->getSaleableItem()->getId() . '" ' .
- 'data-price-box="product-id-' . $this->getSaleableItem()->getId() . '"' .
- '>' . $html . '</div>';
- }
- /**
- * Render minimal amount
- *
- * @return string
- */
- public function renderAmountMinimal()
- {
- $id = $this->getPriceId() ? $this->getPriceId() : 'product-minimal-price-' . $this->getSaleableItem()->getId();
- $amount = $this->minimalPriceCalculator->getAmount($this->getSaleableItem());
- if ($amount === null) {
- return '';
- }
- return $this->renderAmount(
- $amount,
- [
- 'display_label' => __('As low as'),
- 'price_id' => $id,
- 'include_container' => false,
- 'skip_adjustments' => true
- ]
- );
- }
- /**
- * Define if the special price should be shown
- *
- * @return bool
- */
- public function hasSpecialPrice()
- {
- $displayRegularPrice = $this->getPriceType(Price\RegularPrice::PRICE_CODE)->getAmount()->getValue();
- $displayFinalPrice = $this->getPriceType(Price\FinalPrice::PRICE_CODE)->getAmount()->getValue();
- return $displayFinalPrice < $displayRegularPrice;
- }
- /**
- * Define if the minimal price should be shown
- *
- * @return bool
- */
- public function showMinimalPrice()
- {
- $minTierPrice = $this->minimalPriceCalculator->getValue($this->getSaleableItem());
- /** @var Price\FinalPrice $finalPrice */
- $finalPrice = $this->getPriceType(Price\FinalPrice::PRICE_CODE);
- $finalPriceValue = $finalPrice->getAmount()->getValue();
- return $this->getDisplayMinimalPrice()
- && $minTierPrice !== null
- && $minTierPrice < $finalPriceValue;
- }
- /**
- * Get Key for caching block content
- *
- * @return string
- */
- public function getCacheKey()
- {
- return parent::getCacheKey() . ($this->getData('list_category_page') ? '-list-category-page': '');
- }
- /**
- * {@inheritdoc}
- *
- * @return array
- */
- public function getCacheKeyInfo()
- {
- $cacheKeys = parent::getCacheKeyInfo();
- $cacheKeys['display_minimal_price'] = $this->getDisplayMinimalPrice();
- $cacheKeys['is_product_list'] = $this->isProductList();
- return $cacheKeys;
- }
- /**
- * Get flag that price rendering should be done for the list of products
- * By default (if flag is not set) is false
- *
- * @return bool
- */
- public function isProductList()
- {
- $isProductList = $this->getData('is_product_list');
- return $isProductList === true;
- }
- }
|