Skip to content

Commit a42b2d0

Browse files
author
Oleksandr Gorkun
committed
Merge branch '2.2-develop' of https://github.com/magento-qwerty/magento2ce into MAGETWO-81431
2 parents a60d802 + 79463a5 commit a42b2d0

File tree

100 files changed

+3527
-282
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+3527
-282
lines changed

app/code/Magento/Bundle/Pricing/Price/BundleRegularPrice.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function getAmount()
5252
if ($this->product->getPriceType() == Price::PRICE_TYPE_FIXED) {
5353
/** @var \Magento\Catalog\Pricing\Price\CustomOptionPrice $customOptionPrice */
5454
$customOptionPrice = $this->priceInfo->getPrice(CustomOptionPrice::PRICE_CODE);
55-
$price += $customOptionPrice->getCustomOptionRange(true);
55+
$price += $customOptionPrice->getCustomOptionRange(true, $this->getPriceCode());
5656
}
5757
$this->amount[$this->getValue()] = $this->calculator->getMinRegularAmount($price, $this->product);
5858
}
@@ -71,7 +71,7 @@ public function getMaximalPrice()
7171
if ($this->product->getPriceType() == Price::PRICE_TYPE_FIXED) {
7272
/** @var \Magento\Catalog\Pricing\Price\CustomOptionPrice $customOptionPrice */
7373
$customOptionPrice = $this->priceInfo->getPrice(CustomOptionPrice::PRICE_CODE);
74-
$price += $customOptionPrice->getCustomOptionRange(false);
74+
$price += $customOptionPrice->getCustomOptionRange(false, $this->getPriceCode());
7575
}
7676
$this->maximalPrice = $this->calculator->getMaxRegularAmount($price, $this->product);
7777
}

app/code/Magento/Bundle/Pricing/Price/BundleSelectionPrice.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ public function getValue()
128128
'catalog_product_get_final_price',
129129
['product' => $product, 'qty' => $this->bundleProduct->getQty()]
130130
);
131-
$value = $product->getData('final_price') * ($selectionPriceValue / 100);
131+
$price = $this->useRegularPrice ? $product->getData('price') : $product->getData('final_price');
132+
$value = $price * ($selectionPriceValue / 100);
132133
} else {
133134
// calculate price for selection type fixed
134135
$value = $this->priceCurrency->convert($selectionPriceValue);

app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundleSelectionPriceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ public function testGetValueTypeFixedWithSelectionPriceType($useRegularPrice)
201201
[
202202
['qty', null, 1],
203203
['final_price', null, 100],
204+
['price', null, 100],
204205
]
205206
)
206207
);

app/code/Magento/Catalog/Block/Product/View.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ public function getWishlistOptions()
127127
*/
128128
protected function _prepareLayout()
129129
{
130-
$this->getLayout()->createBlock(\Magento\Catalog\Block\Breadcrumbs::class);
131130
$product = $this->getProduct();
132131
if (!$product) {
133132
return parent::_prepareLayout();

app/code/Magento/Catalog/Model/Category.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,15 +1135,19 @@ public function afterDeleteCommit()
11351135
*/
11361136
public function getIdentities()
11371137
{
1138-
$identities = [
1139-
self::CACHE_TAG . '_' . $this->getId(),
1140-
];
1141-
if (!$this->getId() || $this->hasDataChanges()
1142-
|| $this->isDeleted() || $this->dataHasChangedFor(self::KEY_INCLUDE_IN_MENU)
1143-
) {
1144-
$identities[] = self::CACHE_TAG;
1145-
$identities[] = Product::CACHE_PRODUCT_CATEGORY_TAG . '_' . $this->getId();
1138+
$identities = [];
1139+
if ($this->getId()) {
1140+
$identities[] = self::CACHE_TAG . '_' . $this->getId();
1141+
1142+
if ($this->hasDataChanges() || $this->isDeleted() || $this->dataHasChangedFor(self::KEY_INCLUDE_IN_MENU)) {
1143+
$identities[] = Product::CACHE_PRODUCT_CATEGORY_TAG . '_' . $this->getId();
1144+
}
1145+
1146+
if ($this->isObjectNew()) {
1147+
$identities[] = self::CACHE_TAG;
1148+
}
11461149
}
1150+
11471151
return $identities;
11481152
}
11491153

app/code/Magento/Catalog/Model/Product.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ public function getUpdatedAt()
610610
*
611611
* @param bool $calculate
612612
* @return void
613+
* @deprecated
613614
*/
614615
public function setPriceCalculation($calculate = true)
615616
{
@@ -1065,7 +1066,12 @@ protected function _afterLoad()
10651066
*/
10661067
public function cleanCache()
10671068
{
1068-
$this->_cacheManager->clean('catalog_product_' . $this->getId());
1069+
if ($this->getId()) {
1070+
$this->_cacheManager->clean(
1071+
self::CACHE_TAG . '_' . $this->getId()
1072+
);
1073+
}
1074+
10691075
return $this;
10701076
}
10711077

@@ -1159,10 +1165,11 @@ public function setFinalPrice($price)
11591165
*/
11601166
public function getFinalPrice($qty = null)
11611167
{
1162-
if ($this->_getData('final_price') === null) {
1163-
$this->setFinalPrice($this->getPriceModel()->getFinalPrice($qty, $this));
1168+
if ($this->_calculatePrice || $this->_getData('final_price') === null) {
1169+
return $this->getPriceModel()->getFinalPrice($qty, $this);
1170+
} else {
1171+
return $this->_getData('final_price');
11641172
}
1165-
return $this->_getData('final_price');
11661173
}
11671174

11681175
/**
@@ -2287,7 +2294,12 @@ public function getImage()
22872294
*/
22882295
public function getIdentities()
22892296
{
2290-
$identities = [self::CACHE_TAG . '_' . $this->getId()];
2297+
$identities = [];
2298+
2299+
if ($this->getId()) {
2300+
$identities[] = self::CACHE_TAG . '_' . $this->getId();
2301+
}
2302+
22912303
if ($this->getIsChangedCategories()) {
22922304
foreach ($this->getAffectedCategoryIds() as $categoryId) {
22932305
$identities[] = self::CACHE_PRODUCT_CATEGORY_TAG . '_' . $categoryId;
@@ -2299,6 +2311,7 @@ public function getIdentities()
22992311
$identities[] = self::CACHE_PRODUCT_CATEGORY_TAG . '_' . $categoryId;
23002312
}
23012313
}
2314+
23022315
if ($this->_appState->getAreaCode() == \Magento\Framework\App\Area::AREA_FRONTEND) {
23032316
$identities[] = self::CACHE_TAG;
23042317
}
@@ -2718,4 +2731,18 @@ public function setStockData($stockData)
27182731
$this->setData('stock_data', $stockData);
27192732
return $this;
27202733
}
2734+
2735+
/**
2736+
* {@inheritDoc}
2737+
*/
2738+
public function getCacheTags()
2739+
{
2740+
//Preferring individual tags over broad ones.
2741+
$individualTags = $this->getIdentities();
2742+
if ($individualTags) {
2743+
return $individualTags;
2744+
}
2745+
2746+
return parent::getCacheTags();
2747+
}
27212748
}

app/code/Magento/Catalog/Model/Product/Option/Value.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Magento\Catalog\Model\Product\Option;
1313
use Magento\Framework\Model\AbstractModel;
1414
use Magento\Catalog\Pricing\Price\BasePrice;
15+
use Magento\Catalog\Pricing\Price\CustomOptionPriceCalculator;
16+
use Magento\Catalog\Pricing\Price\RegularPrice;
1517

1618
/**
1719
* Catalog product option select type model
@@ -21,6 +23,9 @@
2123
* @method \Magento\Catalog\Model\Product\Option\Value setOptionId(int $value)
2224
*
2325
* @SuppressWarnings(PHPMD.LongVariable)
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) - added use of constants instead of string literals:
27+
* BasePrice::PRICE_CODE - instead of 'base_price'
28+
* RegularPrice::PRICE_CODE - instead of 'regular_price'
2429
* @since 100.0.2
2530
*/
2631
class Value extends AbstractModel implements \Magento\Catalog\Api\Data\ProductCustomOptionValuesInterface
@@ -61,6 +66,11 @@ class Value extends AbstractModel implements \Magento\Catalog\Api\Data\ProductCu
6166
*/
6267
protected $_valueCollectionFactory;
6368

69+
/**
70+
* @var CustomOptionPriceCalculator
71+
*/
72+
private $customOptionPriceCalculator;
73+
6474
/**
6575
* @param \Magento\Framework\Model\Context $context
6676
* @param \Magento\Framework\Registry $registry
@@ -75,9 +85,12 @@ public function __construct(
7585
\Magento\Catalog\Model\ResourceModel\Product\Option\Value\CollectionFactory $valueCollectionFactory,
7686
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
7787
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
78-
array $data = []
88+
array $data = [],
89+
CustomOptionPriceCalculator $customOptionPriceCalculator = null
7990
) {
8091
$this->_valueCollectionFactory = $valueCollectionFactory;
92+
$this->customOptionPriceCalculator = $customOptionPriceCalculator
93+
?? \Magento\Framework\App\ObjectManager::getInstance()->get(CustomOptionPriceCalculator::class);
8194
parent::__construct(
8295
$context,
8396
$registry,
@@ -224,10 +237,8 @@ public function saveValues()
224237
*/
225238
public function getPrice($flag = false)
226239
{
227-
if ($flag && $this->getPriceType() == self::TYPE_PERCENT) {
228-
$basePrice = $this->getOption()->getProduct()->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getValue();
229-
$price = $basePrice * ($this->_getData(self::KEY_PRICE) / 100);
230-
return $price;
240+
if ($flag) {
241+
return $this->customOptionPriceCalculator->getOptionPriceByPriceCode($this, BasePrice::PRICE_CODE);
231242
}
232243
return $this->_getData(self::KEY_PRICE);
233244
}
@@ -239,12 +250,7 @@ public function getPrice($flag = false)
239250
*/
240251
public function getRegularPrice()
241252
{
242-
if ($this->getPriceType() == self::TYPE_PERCENT) {
243-
$basePrice = $this->getOption()->getProduct()->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue();
244-
$price = $basePrice * ($this->_getData(self::KEY_PRICE) / 100);
245-
return $price;
246-
}
247-
return $this->_getData(self::KEY_PRICE);
253+
return $this->customOptionPriceCalculator->getOptionPriceByPriceCode($this, RegularPrice::PRICE_CODE);
248254
}
249255

250256
/**

app/code/Magento/Catalog/Pricing/Price/CustomOptionPrice.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,42 @@ class CustomOptionPrice extends AbstractPrice implements CustomOptionPriceInterf
3535
*/
3636
protected $excludeAdjustment = null;
3737

38+
/**
39+
* @var CustomOptionPriceCalculator
40+
*/
41+
private $customOptionPriceCalculator;
42+
3843
/**
3944
* @param SaleableInterface $saleableItem
4045
* @param float $quantity
4146
* @param CalculatorInterface $calculator
4247
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
43-
* @param array $excludeAdjustment
48+
* @param array|null $excludeAdjustment
49+
* @param CustomOptionPriceCalculator|null $customOptionPriceCalculator
4450
*/
4551
public function __construct(
4652
SaleableInterface $saleableItem,
4753
$quantity,
4854
CalculatorInterface $calculator,
4955
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
50-
$excludeAdjustment = null
56+
$excludeAdjustment = null,
57+
CustomOptionPriceCalculator $customOptionPriceCalculator = null
5158
) {
5259
parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
5360
$this->excludeAdjustment = $excludeAdjustment;
61+
$this->customOptionPriceCalculator = $customOptionPriceCalculator
62+
?? \Magento\Framework\App\ObjectManager::getInstance()->get(CustomOptionPriceCalculator::class);
5463
}
5564

5665
/**
5766
* Get minimal and maximal option values
5867
*
68+
* @param string $priceCode
5969
* @return array
6070
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
6171
* @SuppressWarnings(PHPMD.NPathComplexity)
6272
*/
63-
public function getValue()
73+
public function getValue($priceCode = \Magento\Catalog\Pricing\Price\BasePrice::PRICE_CODE)
6474
{
6575
$optionValues = [];
6676
$options = $this->product->getOptions();
@@ -85,7 +95,8 @@ public function getValue()
8595
} else {
8696
/** @var $optionValue \Magento\Catalog\Model\Product\Option\Value */
8797
foreach ($optionItem->getValues() as $optionValue) {
88-
$price = $optionValue->getPrice($optionValue->getPriceType() == Value::TYPE_PERCENT);
98+
$price =
99+
$this->customOptionPriceCalculator->getOptionPriceByPriceCode($optionValue, $priceCode);
89100
if ($min === null) {
90101
$min = $price;
91102
} elseif ($price < $min) {
@@ -133,12 +144,13 @@ public function getCustomAmount($amount = null, $exclude = null, $context = [])
133144
* Return the minimal or maximal price for custom options
134145
*
135146
* @param bool $getMin
147+
* @param string $priceCode
136148
* @return float
137149
*/
138-
public function getCustomOptionRange($getMin)
150+
public function getCustomOptionRange($getMin, $priceCode = \Magento\Catalog\Pricing\Price\BasePrice::PRICE_CODE)
139151
{
140152
$optionValue = 0.;
141-
$options = $this->getValue();
153+
$options = $this->getValue($priceCode);
142154
foreach ($options as $option) {
143155
if ($getMin) {
144156
$optionValue += $option['min'];
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
8+
use Magento\Catalog\Model\Product\Option\Value as ProductOptionValue;
9+
10+
/**
11+
* Calculates prices of custom options of the product.
12+
*/
13+
class CustomOptionPriceCalculator
14+
{
15+
/**
16+
* Calculates prices of custom option by code.
17+
*
18+
* Price is calculated depends on Price Code.
19+
* Existing logic was taken from methods \Magento\Catalog\Model\Product\Option\Value::(getPrice|getRegularPrice)
20+
* where $priceCode was hardcoded and changed to have dynamical approach.
21+
*
22+
* Examples of usage:
23+
* \Magento\Catalog\Pricing\Price\CustomOptionPrice::getValue
24+
* \Magento\Catalog\Model\Product\Option\Value::getPrice
25+
* \Magento\Catalog\Model\Product\Option\Value::getRegularPrice
26+
*
27+
* @param ProductOptionValue $optionValue
28+
* @param string $priceCode
29+
* @return float|int
30+
*/
31+
public function getOptionPriceByPriceCode(
32+
ProductOptionValue $optionValue,
33+
string $priceCode = BasePrice::PRICE_CODE
34+
) {
35+
if ($optionValue->getPriceType() === ProductOptionValue::TYPE_PERCENT) {
36+
$basePrice = $optionValue->getOption()->getProduct()->getPriceInfo()->getPrice($priceCode)->getValue();
37+
$price = $basePrice * ($optionValue->getData(ProductOptionValue::KEY_PRICE) / 100);
38+
return $price;
39+
}
40+
return $optionValue->getData(ProductOptionValue::KEY_PRICE);
41+
}
42+
}

app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,20 @@ public function testGetImageWithoutAttributeCode()
506506

507507
$this->assertEquals('http://www.example.com/catalog/category/myimage', $result);
508508
}
509+
510+
/**
511+
* @return void
512+
*/
513+
public function testGetIdentities()
514+
{
515+
$category = $this->getCategoryModel();
516+
517+
//Without an ID no identities can be given.
518+
$this->assertEmpty($category->getIdentities());
519+
520+
//Now because ID is set we can get some
521+
$category->setId(42);
522+
523+
$this->assertNotEmpty($category->getIdentities());
524+
}
509525
}

0 commit comments

Comments
 (0)