Skip to content

Commit 1d23a21

Browse files
committed
MAGETWO-88504: Tiered pricing and quantity Increments do not work with decimal inventory
1 parent 9bdfce4 commit 1d23a21

File tree

8 files changed

+117
-9
lines changed

8 files changed

+117
-9
lines changed

app/code/Magento/Bundle/Ui/DataProvider/Product/BundleDataProvider.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
99
use Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider;
1010
use Magento\Bundle\Helper\Data;
11+
use Magento\CatalogInventory\Api\StockRegistryInterface;
12+
use Magento\Framework\Exception\NoSuchEntityException;
1113

1214
class BundleDataProvider extends ProductDataProvider
1315
{
@@ -16,6 +18,11 @@ class BundleDataProvider extends ProductDataProvider
1618
*/
1719
protected $dataHelper;
1820

21+
/**
22+
* @var StockRegistryInterface
23+
*/
24+
private $stockRegistry;
25+
1926
/**
2027
* Construct
2128
*
@@ -28,6 +35,7 @@ class BundleDataProvider extends ProductDataProvider
2835
* @param \Magento\Ui\DataProvider\AddFilterToCollectionInterface[] $addFilterStrategies
2936
* @param array $meta
3037
* @param array $data
38+
* @param StockRegistryInterface $stockRegistry
3139
*/
3240
public function __construct(
3341
$name,
@@ -38,7 +46,8 @@ public function __construct(
3846
array $meta = [],
3947
array $data = [],
4048
array $addFieldStrategies = [],
41-
array $addFilterStrategies = []
49+
array $addFilterStrategies = [],
50+
StockRegistryInterface $stockRegistry = null
4251
) {
4352
parent::__construct(
4453
$name,
@@ -52,6 +61,8 @@ public function __construct(
5261
);
5362

5463
$this->dataHelper = $dataHelper;
64+
$this->stockRegistry = $stockRegistry
65+
?: \Magento\Framework\App\ObjectManager::getInstance()->get(StockRegistryInterface::class);
5566
}
5667

5768
/**
@@ -74,9 +85,29 @@ public function getData()
7485
}
7586
$items = $this->getCollection()->toArray();
7687

88+
foreach ($items as $index => $item) {
89+
$items[$index]['selection_qty_is_integer'] = !$this->isProductQtyDecimal($item['entity_id']);
90+
}
91+
7792
return [
7893
'totalRecords' => $this->getCollection()->getSize(),
7994
'items' => array_values($items),
8095
];
8196
}
97+
98+
/**
99+
* @param int $productId
100+
* @return bool
101+
*/
102+
private function isProductQtyDecimal(int $productId): bool
103+
{
104+
try {
105+
$productStock = $this->stockRegistry->getStockItem($productId);
106+
$result = $productStock->getIsQtyDecimal();
107+
} catch (NoSuchEntityException $e) {
108+
$result = false;
109+
}
110+
111+
return $result;
112+
}
82113
}

app/code/Magento/Bundle/Ui/DataProvider/Product/Form/Modifier/BundlePanel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ protected function getBundleOptions()
377377
'selection_price_type' => '',
378378
'selection_price_value' => '',
379379
'selection_qty' => '',
380+
'selection_qty_is_integer' => '',
380381
],
381382
'links' => ['insertData' => '${ $.provider }:${ $.dataProvider }'],
382383
'imports' => [
@@ -682,10 +683,11 @@ protected function getBundleSelections()
682683
'validation' => [
683684
'required-entry' => true,
684685
'validate-number' => true,
686+
'validate-digits' => true,
685687
'validate-greater-than-zero' => true
686688
],
687689
'imports' => [
688-
'isInteger' => '${ $.provider }:${ $.parentScope }.selection_qty_is_integer'
690+
'validateDigits' => '${ $.provider }:${ $.parentScope }.selection_qty_is_integer',
689691
],
690692
],
691693
],

app/code/Magento/Bundle/view/adminhtml/web/js/components/bundle-option-qty.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ define([
1313
valueUpdate: 'input',
1414
isInteger: true,
1515
validation: {
16-
'validate-number': true
16+
'validate-number': true,
17+
'validate-digits': true
1718
}
1819
},
1920

@@ -32,6 +33,16 @@ define([
3233
var notEqual = this.value() !== this.initialValue.toString();
3334

3435
return !this.visible() ? false : notEqual;
36+
},
37+
38+
/**
39+
* Update field validation rules
40+
*
41+
* @param value
42+
*/
43+
validateDigits: function(value) {
44+
this.isInteger = value;
45+
this.validation['validate-digits'] = this.isInteger;
3546
}
3647

3748
});

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/AdvancedPricing.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Magento\Ui\Component\Form\Element\Select;
2525
use Magento\Ui\Component\Form\Field;
2626
use Magento\Ui\Component\Modal;
27+
use Magento\CatalogInventory\Api\StockRegistryInterface;
28+
use Magento\Framework\Exception\NoSuchEntityException;
2729

2830
/**
2931
* Class AdvancedPricing
@@ -100,6 +102,11 @@ class AdvancedPricing extends AbstractModifier
100102
*/
101103
private $customerGroupSource;
102104

105+
/**
106+
* @var StockRegistryInterface
107+
*/
108+
private $stockRegistry;
109+
103110
/**
104111
* @param LocatorInterface $locator
105112
* @param StoreManagerInterface $storeManager
@@ -111,6 +118,7 @@ class AdvancedPricing extends AbstractModifier
111118
* @param ArrayManager $arrayManager
112119
* @param string $scopeName
113120
* @param GroupSourceInterface $customerGroupSource
121+
* @param StockRegistryInterface $stockRegistry
114122
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
115123
*/
116124
public function __construct(
@@ -123,7 +131,8 @@ public function __construct(
123131
Data $directoryHelper,
124132
ArrayManager $arrayManager,
125133
$scopeName = '',
126-
GroupSourceInterface $customerGroupSource = null
134+
GroupSourceInterface $customerGroupSource = null,
135+
StockRegistryInterface $stockRegistry = null
127136
) {
128137
$this->locator = $locator;
129138
$this->storeManager = $storeManager;
@@ -136,6 +145,8 @@ public function __construct(
136145
$this->scopeName = $scopeName;
137146
$this->customerGroupSource = $customerGroupSource
138147
?: ObjectManager::getInstance()->get(GroupSourceInterface::class);
148+
$this->stockRegistry = $stockRegistry
149+
?: ObjectManager::getInstance()->get(StockRegistryInterface::class);
139150
}
140151

141152
/**
@@ -500,7 +511,8 @@ private function getTierPriceStructure($tierPricePath)
500511
'validation' => [
501512
'required-entry' => true,
502513
'validate-greater-than-zero' => true,
503-
'validate-digits' => true,
514+
'validate-number' => true,
515+
'validate-digits' => $this->hasPriceQtyDigitsValidationPassed(),
504516
],
505517
],
506518
],
@@ -674,4 +686,25 @@ private function getStore()
674686
{
675687
return $this->locator->getStore();
676688
}
689+
690+
/**
691+
* Get Price Qty digits validation
692+
*
693+
* @return bool
694+
*/
695+
private function hasPriceQtyDigitsValidationPassed(): bool
696+
{
697+
$productId = $this->locator->getProduct()->getId();
698+
if ($productId) {
699+
try {
700+
$result = $this->stockRegistry->getStockItem($productId)->getIsQtyDecimal();
701+
} catch (NoSuchEntityException $e) {
702+
$result = false;
703+
}
704+
} else {
705+
$result = false;
706+
}
707+
708+
return $result;
709+
}
677710
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/General.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Catalog\Model\Locator\LocatorInterface;
1010
use Magento\Ui\Component\Form;
1111
use Magento\Framework\Stdlib\ArrayManager;
12+
use Magento\CatalogInventory\Api\StockRegistryInterface;
13+
use Magento\Framework\Exception\NoSuchEntityException;
1214

1315
/**
1416
* Data provider for main panel of product page
@@ -35,16 +37,25 @@ class General extends AbstractModifier
3537
*/
3638
private $localeCurrency;
3739

40+
/**
41+
* @var StockRegistryInterface
42+
*/
43+
private $stockRegistry;
44+
3845
/**
3946
* @param LocatorInterface $locator
4047
* @param ArrayManager $arrayManager
48+
* @param StockRegistryInterface $stockRegistry
4149
*/
4250
public function __construct(
4351
LocatorInterface $locator,
44-
ArrayManager $arrayManager
52+
ArrayManager $arrayManager,
53+
StockRegistryInterface $stockRegistry = null
4554
) {
4655
$this->locator = $locator;
4756
$this->arrayManager = $arrayManager;
57+
$this->stockRegistry = $stockRegistry
58+
?: \Magento\Framework\App\ObjectManager::getInstance()->get(StockRegistryInterface::class);
4859
}
4960

5061
/**
@@ -106,7 +117,7 @@ protected function customizeAdvancedPriceFormat(array $data)
106117
$value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE] =
107118
$this->formatPrice($value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE]);
108119
$value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE_QTY] =
109-
(int)$value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE_QTY];
120+
$this->formatPriceQty($value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE_QTY]);
110121
}
111122
}
112123

@@ -408,6 +419,24 @@ protected function formatPrice($value)
408419
return $value;
409420
}
410421

422+
/**
423+
* Make price qty even int or decimal
424+
*
425+
* @param int|float|string $priceQty
426+
* @return int|float
427+
*/
428+
private function formatPriceQty($priceQty)
429+
{
430+
$productId = $this->locator->getProduct()->getId();
431+
try {
432+
$isQtyDecimal = $this->stockRegistry->getStockItem($productId)->getIsQtyDecimal();
433+
} catch (NoSuchEntityException $e) {
434+
$isQtyDecimal = false;
435+
}
436+
437+
return ($isQtyDecimal ? (float) $priceQty : (int) $priceQty);
438+
}
439+
411440
/**
412441
* Format number according to the locale of the currency and precision of input
413442
*

app/code/Magento/CatalogInventory/Model/Stock/Item.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ public function getQtyIncrements()
401401
if ($this->getUseConfigQtyIncrements()) {
402402
$this->qtyIncrements = $this->stockConfiguration->getQtyIncrements($this->getStoreId());
403403
} else {
404-
$this->qtyIncrements = (int) $this->getData(static::QTY_INCREMENTS);
404+
$qtyIncrements = $this->getData(static::QTY_INCREMENTS);
405+
$this->qtyIncrements = ($this->getIsQtyDecimal() ? (float) $qtyIncrements : (int) $qtyIncrements);
405406
}
406407
}
407408
if ($this->qtyIncrements <= 0) {

app/code/Magento/CatalogInventory/Model/Stock/StockItemRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\CatalogInventory\Api\Data\StockItemInterface;
1111
use Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory;
1212
use Magento\CatalogInventory\Api\StockConfigurationInterface;
13-
use Magento\CatalogInventory\Api\StockItemRepositoryInterface as StockItemRepositoryInterface;
13+
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
1414
use Magento\CatalogInventory\Model\Indexer\Stock\Processor;
1515
use Magento\CatalogInventory\Model\ResourceModel\Stock\Item as StockItemResource;
1616
use Magento\CatalogInventory\Model\Spi\StockStateProviderInterface;

app/code/Magento/CatalogInventory/view/adminhtml/web/js/components/qty-validator-changer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ define([
2020
var isDigits = value !== 1;
2121

2222
this.validation['validate-integer'] = isDigits;
23+
this.validation['validate-digits'] = isDigits;
2324
this.validation['less-than-equals-to'] = isDigits ? 99999999 : 99999999.9999;
2425
this.validate();
2526
}

0 commit comments

Comments
 (0)