Skip to content

Commit

Permalink
Merge pull request magento#4203 from magento-tango/PR-2019-05-13
Browse files Browse the repository at this point in the history
[tango] MAGETWO-99075: Duplicate orders with same quote ID when form is submitted using Enter key
  • Loading branch information
dhorytskyi authored May 24, 2019
2 parents a2385d2 + e91f2ac commit 9f8bfc8
Show file tree
Hide file tree
Showing 32 changed files with 1,191 additions and 506 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ define(
*/
onError: function (response) {
braintree.showError($t('Payment ' + this.getTitle() + ' can\'t be initialized'));
this.isPlaceOrderActionAllowed(true);
throw response.message;
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ define([
*/
placeOrderClick: function () {
if (this.validateCardType() && additionalValidators.validate()) {
this.isPlaceOrderActionAllowed(false);
$(this.getSelector('submit')).trigger('click');
}
},
Expand Down
379 changes: 222 additions & 157 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php

Large diffs are not rendered by default.

180 changes: 116 additions & 64 deletions app/code/Magento/CatalogRule/Model/Indexer/IndexBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magento\CatalogRule\Model\Indexer;

use Magento\Catalog\Model\Product;
use Magento\CatalogRule\Model\ResourceModel\Rule\Collection as RuleCollection;
use Magento\CatalogRule\Model\ResourceModel\Rule\CollectionFactory as RuleCollectionFactory;
use Magento\CatalogRule\Model\Rule;
use Magento\Framework\App\ObjectManager;
Expand All @@ -15,6 +16,8 @@
use Magento\CatalogRule\Model\Indexer\IndexerTableSwapperInterface as TableSwapper;

/**
* Catalog rule index builder
*
* @api
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
Expand Down Expand Up @@ -270,14 +273,14 @@ public function reindexByIds(array $ids)
*/
protected function doReindexByIds($ids)
{
$this->cleanByIds($ids);
$this->cleanProductIndex($ids);

$products = $this->productLoader->getProducts($ids);
foreach ($this->getActiveRules() as $rule) {
foreach ($products as $product) {
$this->applyRule($rule, $product);
}
$activeRules = $this->getActiveRules();
foreach ($products as $product) {
$this->applyRules($activeRules, $product);
}
$this->reindexRuleGroupWebsite->execute();
}

/**
Expand Down Expand Up @@ -322,6 +325,30 @@ protected function doReindexFull()
);
}

/**
* Clean product index
*
* @param array $productIds
* @return void
*/
private function cleanProductIndex(array $productIds): void
{
$where = ['product_id IN (?)' => $productIds];
$this->connection->delete($this->getTable('catalogrule_product'), $where);
}

/**
* Clean product price index
*
* @param array $productIds
* @return void
*/
private function cleanProductPriceIndex(array $productIds): void
{
$where = ['product_id IN (?)' => $productIds];
$this->connection->delete($this->getTable('catalogrule_product_price'), $where);
}

/**
* Clean by product ids
*
Expand All @@ -330,51 +357,35 @@ protected function doReindexFull()
*/
protected function cleanByIds($productIds)
{
$query = $this->connection->deleteFromSelect(
$this->connection
->select()
->from($this->resource->getTableName('catalogrule_product'), 'product_id')
->distinct()
->where('product_id IN (?)', $productIds),
$this->resource->getTableName('catalogrule_product')
);
$this->connection->query($query);

$query = $this->connection->deleteFromSelect(
$this->connection->select()
->from($this->resource->getTableName('catalogrule_product_price'), 'product_id')
->distinct()
->where('product_id IN (?)', $productIds),
$this->resource->getTableName('catalogrule_product_price')
);
$this->connection->query($query);
$this->cleanProductIndex($productIds);
$this->cleanProductPriceIndex($productIds);
}

/**
* Assign product to rule
*
* @param Rule $rule
* @param Product $product
* @return $this
* @throws \Exception
* @SuppressWarnings(PHPMD.NPathComplexity)
* @return void
*/
protected function applyRule(Rule $rule, $product)
private function assignProductToRule(Rule $rule, Product $product): void
{
$ruleId = $rule->getId();
$productEntityId = $product->getId();
$websiteIds = array_intersect($product->getWebsiteIds(), $rule->getWebsiteIds());

if (!$rule->validate($product)) {
return $this;
return;
}

$ruleId = (int) $rule->getId();
$productEntityId = (int) $product->getId();
$ruleProductTable = $this->getTable('catalogrule_product');
$this->connection->delete(
$this->resource->getTableName('catalogrule_product'),
$ruleProductTable,
[
$this->connection->quoteInto('rule_id = ?', $ruleId),
$this->connection->quoteInto('product_id = ?', $productEntityId)
'rule_id = ?' => $ruleId,
'product_id = ?' => $productEntityId,
]
);

$websiteIds = array_intersect($product->getWebsiteIds(), $rule->getWebsiteIds());
$customerGroupIds = $rule->getCustomerGroupIds();
$fromTime = strtotime($rule->getFromDate());
$toTime = strtotime($rule->getToDate());
Expand All @@ -385,43 +396,70 @@ protected function applyRule(Rule $rule, $product)
$actionStop = $rule->getStopRulesProcessing();

$rows = [];
try {
foreach ($websiteIds as $websiteId) {
foreach ($customerGroupIds as $customerGroupId) {
$rows[] = [
'rule_id' => $ruleId,
'from_time' => $fromTime,
'to_time' => $toTime,
'website_id' => $websiteId,
'customer_group_id' => $customerGroupId,
'product_id' => $productEntityId,
'action_operator' => $actionOperator,
'action_amount' => $actionAmount,
'action_stop' => $actionStop,
'sort_order' => $sortOrder,
];

if (count($rows) == $this->batchCount) {
$this->connection->insertMultiple($this->getTable('catalogrule_product'), $rows);
$rows = [];
}
foreach ($websiteIds as $websiteId) {
foreach ($customerGroupIds as $customerGroupId) {
$rows[] = [
'rule_id' => $ruleId,
'from_time' => $fromTime,
'to_time' => $toTime,
'website_id' => $websiteId,
'customer_group_id' => $customerGroupId,
'product_id' => $productEntityId,
'action_operator' => $actionOperator,
'action_amount' => $actionAmount,
'action_stop' => $actionStop,
'sort_order' => $sortOrder,
];

if (count($rows) == $this->batchCount) {
$this->connection->insertMultiple($ruleProductTable, $rows);
$rows = [];
}
}

if (!empty($rows)) {
$this->connection->insertMultiple($this->resource->getTableName('catalogrule_product'), $rows);
}
} catch (\Exception $e) {
throw $e;
}
if ($rows) {
$this->connection->insertMultiple($ruleProductTable, $rows);
}
}

/**
* Apply rule
*
* @param Rule $rule
* @param Product $product
* @return $this
* @throws \Exception
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function applyRule(Rule $rule, $product)
{
$this->assignProductToRule($rule, $product);
$this->reindexRuleProductPrice->execute($this->batchCount, $product);
$this->reindexRuleGroupWebsite->execute();

return $this;
}

/**
* Apply rules
*
* @param RuleCollection $ruleCollection
* @param Product $product
* @return void
*/
private function applyRules(RuleCollection $ruleCollection, Product $product): void
{
foreach ($ruleCollection as $rule) {
$this->assignProductToRule($rule, $product);
}

$this->cleanProductPriceIndex([$product->getId()]);
$this->reindexRuleProductPrice->execute($this->batchCount, $product);
}

/**
* Retrieve table name
*
* @param string $tableName
* @return string
*/
Expand All @@ -431,6 +469,8 @@ protected function getTable($tableName)
}

/**
* Update rule product data
*
* @param Rule $rule
* @return $this
* @deprecated 100.2.0
Expand All @@ -456,6 +496,8 @@ protected function updateRuleProductData(Rule $rule)
}

/**
* Apply all rules
*
* @param Product|null $product
* @throws \Exception
* @return $this
Expand Down Expand Up @@ -495,8 +537,10 @@ protected function deleteOldData()
}

/**
* Calculate rule product price
*
* @param array $ruleData
* @param null $productData
* @param array $productData
* @return float
* @deprecated 100.2.0
* @see ProductPriceCalculator::calculate
Expand All @@ -507,6 +551,8 @@ protected function calcRuleProductPrice($ruleData, $productData = null)
}

/**
* Get rule products statement
*
* @param int $websiteId
* @param Product|null $product
* @return \Zend_Db_Statement_Interface
Expand All @@ -520,6 +566,8 @@ protected function getRuleProductsStmt($websiteId, Product $product = null)
}

/**
* Save rule product prices
*
* @param array $arrData
* @return $this
* @throws \Exception
Expand All @@ -535,7 +583,7 @@ protected function saveRuleProductPrices($arrData)
/**
* Get active rules
*
* @return array
* @return RuleCollection
*/
protected function getActiveRules()
{
Expand All @@ -545,14 +593,16 @@ protected function getActiveRules()
/**
* Get active rules
*
* @return array
* @return RuleCollection
*/
protected function getAllRules()
{
return $this->ruleCollectionFactory->create();
}

/**
* Get product
*
* @param int $productId
* @return Product
*/
Expand All @@ -565,6 +615,8 @@ protected function getProduct($productId)
}

/**
* Log critical exception
*
* @param \Exception $e
* @return void
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,19 @@ public function execute(array $priceData, $useAdditionalTable = false)
);
}

$productIds = [];

try {
foreach ($priceData as $key => $data) {
$productIds['product_id'] = $data['product_id'];
$priceData[$key]['rule_date'] = $this->dateFormat->formatDate($data['rule_date'], false);
$priceData[$key]['latest_start_date'] = $this->dateFormat->formatDate(
$data['latest_start_date'],
false
);
$priceData[$key]['earliest_end_date'] = $this->dateFormat->formatDate(
$data['earliest_end_date'],
false
);
}
$connection->insertOnDuplicate($indexTable, $priceData);
} catch (\Exception $e) {
throw $e;
foreach ($priceData as $key => $data) {
$priceData[$key]['rule_date'] = $this->dateFormat->formatDate($data['rule_date'], false);
$priceData[$key]['latest_start_date'] = $this->dateFormat->formatDate(
$data['latest_start_date'],
false
);
$priceData[$key]['earliest_end_date'] = $this->dateFormat->formatDate(
$data['earliest_end_date'],
false
);
}
$connection->insertOnDuplicate($indexTable, $priceData);

return true;
}
}
Loading

0 comments on commit 9f8bfc8

Please sign in to comment.