Skip to content

Commit

Permalink
Merge pull request #1116 from magento-tsg/2.1.8-develop-pr11
Browse files Browse the repository at this point in the history
[TSG] Backporting for 2.1 (pr11) (2.1.8)
  • Loading branch information
Volodymyr Klymenko authored May 19, 2017
2 parents 0acbc57 + 37028f0 commit ff253a7
Show file tree
Hide file tree
Showing 67 changed files with 2,313 additions and 320 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Pricing\Price;

use Magento\Framework\Pricing\SaleableInterface;
use Magento\Framework\Pricing\Amount\AmountInterface;

/**
* Interface define methods which control display of "As low as" price.
*/
interface MinimalPriceCalculatorInterface
{
/**
* Get raw value for "as low as" price.
*
* @param SaleableInterface $saleableItem
* @return float|null
*/
public function getValue(SaleableInterface $saleableItem);

/**
* Return structured object with "as low as" value.
*
* @param SaleableInterface $saleableItem
* @return AmountInterface|null
*/
public function getAmount(SaleableInterface $saleableItem);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Pricing\Price;

use Magento\Framework\Pricing\SaleableInterface;
use Magento\Framework\Pricing\Adjustment\CalculatorInterface;
use Magento\Framework\Pricing\Amount\AmountInterface;

/**
* MinimalTierPriceCalculator shows minimal value of Tier Prices.
*/
class MinimalTierPriceCalculator implements MinimalPriceCalculatorInterface
{
/**
* Price Calculator interface.
*
* @var CalculatorInterface
*/
private $calculator;

/**
* @param CalculatorInterface $calculator
*/
public function __construct(CalculatorInterface $calculator)
{
$this->calculator = $calculator;
}

/**
* Get raw value of "as low as" as a minimal among tier prices.
*
* @param SaleableInterface $saleableItem
* @return float|null
*/
public function getValue(SaleableInterface $saleableItem)
{
/** @var TierPrice $price */
$price = $saleableItem->getPriceInfo()->getPrice(TierPrice::PRICE_CODE);
$tierPriceList = $price->getTierPriceList();

$tierPrices = [];
foreach ($tierPriceList as $tierPrice) {
/** @var AmountInterface $price */
$price = $tierPrice['price'];
$tierPrices[] = $price->getValue();
}

return $tierPrices ? min($tierPrices) : null;
}

/**
* Return calculated amount object that keeps "as low as" value.
*
* @param SaleableInterface $saleableItem
* @return AmountInterface|null
*/
public function getAmount(SaleableInterface $saleableItem)
{
$value = $this->getValue($saleableItem);

return $value === null ? null : $this->calculator->getAmount($value, $saleableItem);
}
}
85 changes: 57 additions & 28 deletions app/code/Magento/Catalog/Pricing/Render/FinalPriceBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,61 @@

use Magento\Catalog\Pricing\Price;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Module\Manager;
use Magento\Framework\Pricing\Render;
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\Msrp\Pricing\Price\MsrpPrice;
use Magento\Framework\View\Element\Template\Context;

/**
* Class for final_price rendering
* Class for final_price rendering.
*
* @method bool getUseLinkForAsLowAs()
* @method bool getDisplayMinimalPrice()
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class FinalPriceBox extends BasePriceBox
{
/**
* @var SalableResolverInterface
* Interface resolver provided to check is product available for sale.
*
* @var \Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface
*/
private $salableResolver;

/** @var Manager */
/**
* Module statuses manager.
*
* @var \Magento\Framework\Module\Manager
*/
private $moduleManager;

/**
* Shows minimal value of Tier Prices.
*
* @var \Magento\Catalog\Pricing\Price\MinimalPriceCalculatorInterface
*/
private $minimalPriceCalculator;

/**
* @param Context $context
* @param SaleableInterface $saleableItem
* @param PriceInterface $price
* @param RendererPool $rendererPool
* @param array $data
* @param SalableResolverInterface $salableResolver
* @param \Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface $salableResolver
*/
public function __construct(
Context $context,
SaleableInterface $saleableItem,
PriceInterface $price,
RendererPool $rendererPool,
array $data = [],
SalableResolverInterface $salableResolver = null
\Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface $salableResolver = null
) {
parent::__construct($context, $saleableItem, $price, $rendererPool, $data);
$this->salableResolver = $salableResolver ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(SalableResolverInterface::class);
$this->salableResolver = $salableResolver ?: ObjectManager::getInstance()
->get(\Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface::class);
}

/**
Expand Down Expand Up @@ -116,7 +125,7 @@ private function isMsrpPriceApplicable()
}

/**
* Wrap with standard required container
* Wrap with standard required container.
*
* @param string $html
* @return string
Expand All @@ -130,17 +139,21 @@ protected function wrapResult($html)
}

/**
* Render minimal amount
* Render minimal amount.
*
* @return string
*/
public function renderAmountMinimal()
{
/** @var \Magento\Catalog\Pricing\Price\FinalPrice $price */
$price = $this->getPriceType(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE);
$id = $this->getPriceId() ? $this->getPriceId() : 'product-minimal-price-' . $this->getSaleableItem()->getId();
$amount = $this->getMinimalPriceCalculator()->getAmount($this->getSaleableItem());

if ($amount === null) {
return '';
}

return $this->renderAmount(
$price->getMinimalPrice(),
$amount,
[
'display_label' => __('As low as'),
'price_id' => $id,
Expand All @@ -151,7 +164,7 @@ public function renderAmountMinimal()
}

/**
* Define if the special price should be shown
* Define if the special price should be shown.
*
* @return bool
*/
Expand All @@ -163,23 +176,25 @@ public function hasSpecialPrice()
}

/**
* Define if the minimal price should be shown
* Define if the minimal price should be shown.
*
* @return bool
*/
public function showMinimalPrice()
{
$minTierPrice = $this->getMinimalPriceCalculator()->getValue($this->getSaleableItem());

/** @var Price\FinalPrice $finalPrice */
$finalPrice = $this->getPriceType(Price\FinalPrice::PRICE_CODE);
$finalPriceValue = $finalPrice->getAmount()->getValue();
$minimalPriceAValue = $finalPrice->getMinimalPrice()->getValue();

return $this->getDisplayMinimalPrice()
&& $minimalPriceAValue
&& $minimalPriceAValue < $finalPriceValue;
&& $minTierPrice !== null
&& $minTierPrice < $finalPriceValue;
}

/**
* Get Key for caching block content
* Get Key for caching block content.
*
* @return string
*/
Expand All @@ -203,19 +218,19 @@ public function getCacheKeyInfo()

/**
* @deprecated
* @return Manager
* @return \Magento\Framework\Module\Manager
*/
private function getModuleManager()
{
if ($this->moduleManager === null) {
$this->moduleManager = ObjectManager::getInstance()->get(Manager::class);
$this->moduleManager = ObjectManager::getInstance()->get(\Magento\Framework\Module\Manager::class);
}
return $this->moduleManager;
}

/**
* Get flag that price rendering should be done for the list of products
* By default (if flag is not set) is false
* Get flag that price rendering should be done for the list of products.
* By default (if flag is not set) is false.
*
* @return bool
*/
Expand All @@ -224,4 +239,18 @@ public function isProductList()
$isProductList = $this->getData('is_product_list');
return $isProductList === true;
}

/**
* @deprecated
* @return \Magento\Catalog\Pricing\Price\MinimalPriceCalculatorInterface
*/
private function getMinimalPriceCalculator()
{
if ($this->minimalPriceCalculator == null) {
$this->minimalPriceCalculator = ObjectManager::getInstance()
->get(\Magento\Catalog\Pricing\Price\MinimalPriceCalculatorInterface::class);
}

return $this->minimalPriceCalculator;
}
}
Loading

0 comments on commit ff253a7

Please sign in to comment.