Skip to content

Commit

Permalink
Merge pull request #2133 from magento-tsg/2.2-develop-pr20
Browse files Browse the repository at this point in the history
[TSG] Backporting for 2.2 (pr20) (2.2.4)
  • Loading branch information
Alexander Akimov authored Feb 27, 2018
2 parents 6f188ab + a9150c8 commit f726dac
Show file tree
Hide file tree
Showing 20 changed files with 925 additions and 76 deletions.
27 changes: 12 additions & 15 deletions app/code/Magento/Backend/Model/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,33 +202,30 @@ public function getUrl($routePath = null, $routeParams = null)
}

$cacheSecretKey = false;
if (is_array($routeParams) && isset($routeParams['_cache_secret_key'])) {
if (isset($routeParams['_cache_secret_key'])) {
unset($routeParams['_cache_secret_key']);
$cacheSecretKey = true;
}
$result = parent::getUrl($routePath, $routeParams);
if (!$this->useSecretKey()) {
return $result;
}

$this->_setRoutePath($routePath);
$routeName = $this->_getRouteName('*');
$controllerName = $this->_getControllerName(self::DEFAULT_CONTROLLER_NAME);
$actionName = $this->_getActionName(self::DEFAULT_ACTION_NAME);
if ($cacheSecretKey) {
$secret = [self::SECRET_KEY_PARAM_NAME => "\${$routeName}/{$controllerName}/{$actionName}\$"];
} else {
$secret = [
self::SECRET_KEY_PARAM_NAME => $this->getSecretKey($routeName, $controllerName, $actionName),
];
}
if (is_array($routeParams)) {
$routeParams = array_merge($secret, $routeParams);
} else {
$routeParams = $secret;
}
if (is_array($this->_getRouteParams())) {
$routeParams = array_merge($this->_getRouteParams(), $routeParams);

if (!isset($routeParams[self::SECRET_KEY_PARAM_NAME])) {
if (!is_array($routeParams)) {
$routeParams = [];
}
$secretKey = $cacheSecretKey
? "\${$routeName}/{$controllerName}/{$actionName}\$"
: $this->getSecretKey($routeName, $controllerName, $actionName);
$routeParams[self::SECRET_KEY_PARAM_NAME] = $secretKey;
}

return parent::getUrl("{$routeName}/{$controllerName}/{$actionName}", $routeParams);
}

Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public function testGetCustomAttributes()
$this->assertEquals("description", $this->category->getCustomAttribute($descriptionAttributeCode)->getValue());

//Change the attribute value, should reflect in getCustomAttribute
$this->category->setData($descriptionAttributeCode, "new description");
$this->category->setCustomAttribute($descriptionAttributeCode, "new description");
$this->assertEquals(1, count($this->category->getCustomAttributes()));
$this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode));
$this->assertEquals(
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ public function testGetCustomAttributes()
$this->assertEquals("red", $this->model->getCustomAttribute($colorAttributeCode)->getValue());

//Change the attribute value, should reflect in getCustomAttribute
$this->model->setData($colorAttributeCode, "blue");
$this->model->setCustomAttribute($colorAttributeCode, "blue");
$this->assertEquals(1, count($this->model->getCustomAttributes()));
$this->assertNotNull($this->model->getCustomAttribute($colorAttributeCode));
$this->assertEquals("blue", $this->model->getCustomAttribute($colorAttributeCode)->getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,7 @@ protected function _saveProducts()
$storeId = !empty($rowData[self::COL_STORE])
? $this->getStoreIdByCode($rowData[self::COL_STORE])
: Store::DEFAULT_STORE_ID;
if (isset($rowData['_media_is_disabled'])) {
if (isset($rowData['_media_is_disabled']) && strlen(trim($rowData['_media_is_disabled']))) {
$disabledImages = array_flip(
explode($this->getMultipleValueSeparator(), $rowData['_media_is_disabled'])
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ public function prepareAttributesWithDefaultValueForSave(array $rowData, $withDe
if ($attrParams['is_static']) {
continue;
}
if (isset($rowData[$attrCode]) && strlen($rowData[$attrCode])) {
if (isset($rowData[$attrCode]) && strlen(trim($rowData[$attrCode]))) {
if (in_array($attrParams['type'], ['select', 'boolean'])) {
$resultAttrs[$attrCode] = $attrParams['options'][strtolower($rowData[$attrCode])];
} elseif ('multiselect' == $attrParams['type']) {
Expand Down
19 changes: 13 additions & 6 deletions app/code/Magento/CatalogInventory/Model/ResourceModel/Stock.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,20 @@ protected function _construct()
}

/**
* Lock Stock Item records
* Lock Stock Item records.
*
* @param int[] $productIds
* @param int $websiteId
* @return array
*/
public function lockProductsStock($productIds, $websiteId)
public function lockProductsStock(array $productIds, $websiteId)
{
if (empty($productIds)) {
return [];
}
$itemTable = $this->getTable('cataloginventory_stock_item');
$select = $this->getConnection()->select()->from(['si' => $itemTable])
->where('website_id=?', $websiteId)
->where('website_id = ?', $websiteId)
->where('product_id IN(?)', $productIds)
->forUpdate(true);

Expand All @@ -136,12 +136,19 @@ public function lockProductsStock($productIds, $websiteId)
->columns(
[
'product_id' => 'entity_id',
'type_id' => 'type_id'
'type_id' => 'type_id',
]
);
$this->getConnection()->query($select);
$items = [];

return $this->getConnection()->fetchAll($selectProducts);
foreach ($this->getConnection()->query($select)->fetchAll() as $si) {
$items[$si['product_id']] = $si;
}
foreach ($this->getConnection()->fetchAll($selectProducts) as $p) {
$items[$p['product_id']]['type_id'] = $p['type_id'];
}

return $items;
}

/**
Expand Down
19 changes: 16 additions & 3 deletions app/code/Magento/CatalogInventory/Model/StockManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,42 @@ class StockManagement implements StockManagementInterface
*/
private $qtyCounter;

/**
* @var StockRegistryStorage
*/
private $stockRegistryStorage;

/**
* @param ResourceStock $stockResource
* @param StockRegistryProviderInterface $stockRegistryProvider
* @param StockState $stockState
* @param StockConfigurationInterface $stockConfiguration
* @param ProductRepositoryInterface $productRepository
* @param QtyCounterInterface $qtyCounter
* @param StockRegistryStorage|null $stockRegistryStorage
*/
public function __construct(
ResourceStock $stockResource,
StockRegistryProviderInterface $stockRegistryProvider,
StockState $stockState,
StockConfigurationInterface $stockConfiguration,
ProductRepositoryInterface $productRepository,
QtyCounterInterface $qtyCounter
QtyCounterInterface $qtyCounter,
StockRegistryStorage $stockRegistryStorage = null
) {
$this->stockRegistryProvider = $stockRegistryProvider;
$this->stockState = $stockState;
$this->stockConfiguration = $stockConfiguration;
$this->productRepository = $productRepository;
$this->qtyCounter = $qtyCounter;
$this->resource = $stockResource;
$this->stockRegistryStorage = $stockRegistryStorage ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(StockRegistryStorage::class);
}

/**
* Subtract product qtys from stock.
* Return array of items that require full save
* Return array of items that require full save.
*
* @param string[] $items
* @param int $websiteId
Expand All @@ -92,17 +101,20 @@ public function registerProductsSale($items, $websiteId = null)
$fullSaveItems = $registeredItems = [];
foreach ($lockedItems as $lockedItemRecord) {
$productId = $lockedItemRecord['product_id'];
$this->stockRegistryStorage->removeStockItem($productId, $websiteId);

/** @var StockItemInterface $stockItem */
$orderedQty = $items[$productId];
$stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId);
$stockItem->setQty($lockedItemRecord['qty']); // update data from locked item
$canSubtractQty = $stockItem->getItemId() && $this->canSubtractQty($stockItem);
if (!$canSubtractQty || !$this->stockConfiguration->isQty($lockedItemRecord['type_id'])) {
continue;
}
if (!$stockItem->hasAdminArea()
&& !$this->stockState->checkQty($productId, $orderedQty, $stockItem->getWebsiteId())
) {
$this->getResource()->rollBack();
$this->getResource()->commit();
throw new \Magento\Framework\Exception\LocalizedException(
__('Not all of your products are available in the requested quantity.')
);
Expand All @@ -122,6 +134,7 @@ public function registerProductsSale($items, $websiteId = null)
}
$this->qtyCounter->correctItemsQty($registeredItems, $websiteId, '-');
$this->getResource()->commit();

return $fullSaveItems;
}

Expand Down
Loading

0 comments on commit f726dac

Please sign in to comment.