Skip to content

Commit a36c4de

Browse files
author
Alexander Akimov
authored
Merge pull request #1749 from magento-tsg/2.1-develop-pr36
[TSG] Backporting for 2.1 (pr36) (2.1.11)
2 parents 59f244b + c7fc5d8 commit a36c4de

File tree

40 files changed

+1354
-96
lines changed

40 files changed

+1354
-96
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function __construct(\Magento\Catalog\Block\Product\Context $context, arr
124124
*/
125125
public function getAddToCartUrl($product, $additional = [])
126126
{
127-
if ($product->getTypeInstance()->hasRequiredOptions($product)) {
127+
if (!$product->getTypeInstance()->isPossibleBuyFromList($product)) {
128128
if (!isset($additional['_escape'])) {
129129
$additional['_escape'] = true;
130130
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,4 +1092,16 @@ public function getAssociatedProducts($product)
10921092
{
10931093
return [];
10941094
}
1095+
1096+
/**
1097+
* Check if product can be potentially buyed from the category page or some
1098+
* other list
1099+
*
1100+
* @param \Magento\Catalog\Model\Product $product
1101+
* @return bool
1102+
*/
1103+
public function isPossibleBuyFromList($product)
1104+
{
1105+
return !$this->hasRequiredOptions($product);
1106+
}
10951107
}

app/code/Magento/Catalog/Test/Unit/Block/Product/ListProductTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ public function testGetAddToCartPostParams()
195195
];
196196

197197
$this->typeInstanceMock->expects($this->once())
198-
->method('hasRequiredOptions')
198+
->method('isPossibleBuyFromList')
199199
->with($this->equalTo($this->productMock))
200-
->will($this->returnValue(false));
200+
->will($this->returnValue(true));
201201
$this->cartHelperMock->expects($this->any())
202202
->method('getAddUrl')
203203
->with($this->equalTo($this->productMock), $this->equalTo([]))

app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ define([
8686
}
8787

8888
if (res.backUrl) {
89+
var eventData = {
90+
'form': form,
91+
'redirectParameters': []
92+
}
93+
// trigger global event, so other modules will be able add parameters to redirect url
94+
$('body').trigger('catalogCategoryAddToCartRedirect', eventData);
95+
if (eventData.redirectParameters.length > 0) {
96+
var parameters = res.backUrl.split('#');
97+
parameters.push(eventData.redirectParameters.join('&'));
98+
res.backUrl = parameters.join('#');
99+
}
89100
window.location = res.backUrl;
90101
return;
91102
}

app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,4 +1488,25 @@ public function getSalableUsedProducts(Product $product, $requiredAttributeIds =
14881488

14891489
return $usedSalableProducts;
14901490
}
1491+
1492+
/**
1493+
* @inheritdoc
1494+
*/
1495+
public function isPossibleBuyFromList($product)
1496+
{
1497+
/** @var bool $isAllCustomOptionsDisplayed */
1498+
$isAllCustomOptionsDisplayed = true;
1499+
1500+
foreach ($this->getConfigurableAttributes($product) as $attribute) {
1501+
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $eavAttribute */
1502+
$eavAttribute = $attribute->getProductAttribute();
1503+
1504+
$isAllCustomOptionsDisplayed = (
1505+
$isAllCustomOptionsDisplayed
1506+
&& $eavAttribute->getData('used_in_product_listing')
1507+
);
1508+
}
1509+
1510+
return $isAllCustomOptionsDisplayed;
1511+
}
14911512
}

app/code/Magento/ConfigurableProduct/Model/ResourceModel/Indexer/Stock/Configurable.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,25 @@ protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = f
2929
$connection = $this->getConnection();
3030
$idxTable = $usePrimaryTable ? $this->getMainTable() : $this->getIdxTable();
3131
$select = parent::_getStockStatusSelect($entityIds, $usePrimaryTable);
32+
$linkField = $metadata->getLinkField();
3233
$select->reset(
3334
\Magento\Framework\DB\Select::COLUMNS
3435
)->columns(
3536
['e.entity_id', 'cis.website_id', 'cis.stock_id']
3637
)->joinLeft(
3738
['l' => $this->getTable('catalog_product_super_link')],
38-
'l.parent_id = e.' . $metadata->getLinkField(),
39+
'l.parent_id = e.' . $linkField,
3940
[]
4041
)->join(
4142
['le' => $this->getTable('catalog_product_entity')],
4243
'le.entity_id = l.product_id',
4344
[]
45+
)->joinInner(
46+
['cpei' => $this->getTable('catalog_product_entity_int')],
47+
'le.' . $linkField . ' = cpei.' . $linkField
48+
. ' AND cpei.attribute_id = ' . $this->_getAttribute('status')->getId()
49+
. ' AND cpei.value = ' . ProductStatus::STATUS_ENABLED,
50+
[]
4451
)->joinLeft(
4552
['i' => $idxTable],
4653
'i.product_id = l.product_id AND cis.website_id = i.website_id AND cis.stock_id = i.stock_id',

app/code/Magento/CustomerImportExport/Model/Import/Address.php

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
namespace Magento\CustomerImportExport\Model\Import;
77

88
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source as Sources;
9+
use Magento\Framework\Stdlib\DateTime;
910
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1011
use Magento\Framework\App\ObjectManager;
1112
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
1213
use Magento\Store\Model\Store;
14+
use Magento\ImportExport\Model\Import;
1315

1416
/**
1517
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -21,7 +23,7 @@ class Address extends AbstractCustomer
2123
/**#@+
2224
* Attribute collection name
2325
*/
24-
const ATTRIBUTE_COLLECTION_NAME = 'Magento\Customer\Model\ResourceModel\Address\Attribute\Collection';
26+
const ATTRIBUTE_COLLECTION_NAME = \Magento\Customer\Model\ResourceModel\Address\Attribute\Collection::class;
2527

2628
/**#@-*/
2729

@@ -217,7 +219,7 @@ class Address extends AbstractCustomer
217219
protected $_addressFactory;
218220

219221
/**
220-
* @var \Magento\Framework\Stdlib\DateTime
222+
* @var DateTime
221223
*/
222224
protected $dateTime;
223225

@@ -270,7 +272,7 @@ class Address extends AbstractCustomer
270272
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
271273
* @param \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressColFactory
272274
* @param \Magento\Customer\Model\ResourceModel\Address\Attribute\CollectionFactory $attributesFactory
273-
* @param \Magento\Framework\Stdlib\DateTime $dateTime
275+
* @param DateTime $dateTime
274276
* @param \Magento\Customer\Model\Address\Validator\Postcode $postcodeValidator
275277
* @param array $data
276278
* @param Sources\CountryWithWebsites|null $countryWithWebsites
@@ -294,7 +296,7 @@ public function __construct(
294296
\Magento\Customer\Model\CustomerFactory $customerFactory,
295297
\Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressColFactory,
296298
\Magento\Customer\Model\ResourceModel\Address\Attribute\CollectionFactory $attributesFactory,
297-
\Magento\Framework\Stdlib\DateTime $dateTime,
299+
DateTime $dateTime,
298300
\Magento\Customer\Model\Address\Validator\Postcode $postcodeValidator,
299301
array $data = [],
300302
Sources\CountryWithWebsites $countryWithWebsites = null
@@ -586,12 +588,8 @@ protected function _prepareDataForUpdate(array $rowData)
586588
$defaults = [];
587589
$newAddress = true;
588590
// get address id
589-
if (isset(
590-
$this->_addresses[$customerId]
591-
) && in_array(
592-
$rowData[self::COLUMN_ADDRESS_ID],
593-
$this->_addresses[$customerId]
594-
)
591+
if (isset($this->_addresses[$customerId])
592+
&& in_array($rowData[self::COLUMN_ADDRESS_ID], $this->_addresses[$customerId])
595593
) {
596594
$newAddress = false;
597595
$addressId = $rowData[self::COLUMN_ADDRESS_ID];
@@ -601,7 +599,7 @@ protected function _prepareDataForUpdate(array $rowData)
601599
$entityRow = [
602600
'entity_id' => $addressId,
603601
'parent_id' => $customerId,
604-
'updated_at' => (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
602+
'updated_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT),
605603
];
606604
$websiteId = $this->_websiteCodeToId[$rowData[self::COLUMN_WEBSITE]];
607605

@@ -624,7 +622,16 @@ protected function _prepareDataForUpdate(array $rowData)
624622
$value = $attributeParams['options'][strtolower($rowData[$attributeAlias])];
625623
} elseif ('datetime' == $attributeParams['type']) {
626624
$value = (new \DateTime())->setTimestamp(strtotime($rowData[$attributeAlias]));
627-
$value = $value->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
625+
$value = $value->format(DateTime::DATETIME_PHP_FORMAT);
626+
} elseif ('multiselect' == $attributeParams['type']) {
627+
$separator = isset($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]) ?
628+
$this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR] :
629+
Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR;
630+
$value = str_replace(
631+
$separator,
632+
Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR,
633+
$rowData[$attributeAlias]
634+
);
628635
} else {
629636
$value = $rowData[$attributeAlias];
630637
}
@@ -645,22 +652,11 @@ protected function _prepareDataForUpdate(array $rowData)
645652
}
646653

647654
// let's try to find region ID
648-
$entityRow['region_id'] = null;
649-
if (!empty($rowData[self::COLUMN_REGION])) {
650-
$countryNormalized = strtolower($rowData[self::COLUMN_COUNTRY_ID]);
651-
$regionNormalized = strtolower($rowData[self::COLUMN_REGION]);
652-
653-
if (isset($this->_countryRegions[$countryNormalized][$regionNormalized])) {
654-
$regionId = $this->_countryRegions[$countryNormalized][$regionNormalized];
655-
$entityRow[self::COLUMN_REGION] = $this->_regions[$regionId];
656-
$entityRow['region_id'] = $regionId;
657-
}
658-
}
655+
$entityRow = $this->fillRegionData($rowData, $entityRow);
659656

660657
if ($newAddress) {
661658
$entityRowNew = $entityRow;
662-
$entityRowNew['created_at'] =
663-
(new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
659+
$entityRowNew['created_at'] = (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT);
664660
} else {
665661
$entityRowUpdate = $entityRow;
666662
}
@@ -839,7 +835,16 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
839835
continue;
840836
}
841837
if (isset($rowData[$attributeCode]) && strlen($rowData[$attributeCode])) {
842-
$this->isAttributeValid($attributeCode, $attributeParams, $rowData, $rowNumber);
838+
$multiSeparator = isset($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]) ?
839+
$this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR] :
840+
Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR;
841+
$this->isAttributeValid(
842+
$attributeCode,
843+
$attributeParams,
844+
$rowData,
845+
$rowNumber,
846+
$multiSeparator
847+
);
843848
} elseif ($attributeParams['is_required'] && (!isset(
844849
$this->_addresses[$customerId]
845850
) || !in_array(
@@ -940,4 +945,28 @@ public function setCustomerAttributes($customerAttributes)
940945
$this->_customerAttributes = $customerAttributes;
941946
return $this;
942947
}
948+
949+
/**
950+
* Try to get region_id and set it to $entityRow.
951+
*
952+
* @param array $rowData
953+
* @param array $entityRow
954+
* @return array
955+
*/
956+
private function fillRegionData(array $rowData, array $entityRow)
957+
{
958+
$entityRow['region_id'] = null;
959+
if (!empty($rowData[self::COLUMN_REGION])) {
960+
$countryNormalized = strtolower($rowData[self::COLUMN_COUNTRY_ID]);
961+
$regionNormalized = strtolower($rowData[self::COLUMN_REGION]);
962+
963+
if (isset($this->_countryRegions[$countryNormalized][$regionNormalized])) {
964+
$regionId = $this->_countryRegions[$countryNormalized][$regionNormalized];
965+
$entityRow[self::COLUMN_REGION] = $this->_regions[$regionId];
966+
$entityRow['region_id'] = $regionId;
967+
}
968+
}
969+
970+
return $entityRow;
971+
}
943972
}

app/code/Magento/ImportExport/Model/Import/AbstractEntity.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
99
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1010
use Magento\Framework\App\ResourceConnection;
11+
use Magento\ImportExport\Model\Import;
1112

1213
/**
1314
* Import entity abstract model
@@ -638,11 +639,17 @@ public function getMasterAttributeCode()
638639
* @param array $attributeParams Attribute params
639640
* @param array $rowData Row data
640641
* @param int $rowNumber
642+
* @param string $multiSeparator
641643
* @return bool
642644
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
643645
*/
644-
public function isAttributeValid($attributeCode, array $attributeParams, array $rowData, $rowNumber)
645-
{
646+
public function isAttributeValid(
647+
$attributeCode,
648+
array $attributeParams,
649+
array $rowData,
650+
$rowNumber,
651+
$multiSeparator = Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR
652+
) {
646653
$message = '';
647654
switch ($attributeParams['type']) {
648655
case 'varchar':
@@ -657,7 +664,13 @@ public function isAttributeValid($attributeCode, array $attributeParams, array $
657664
break;
658665
case 'select':
659666
case 'multiselect':
660-
$valid = isset($attributeParams['options'][strtolower($rowData[$attributeCode])]);
667+
$valid = true;
668+
foreach (explode($multiSeparator, strtolower($rowData[$attributeCode])) as $value) {
669+
$valid = isset($attributeParams['options'][$value]);
670+
if (!$valid) {
671+
break;
672+
}
673+
}
661674
$message = self::ERROR_INVALID_ATTRIBUTE_OPTION;
662675
break;
663676
case 'int':

app/code/Magento/ImportExport/Model/Import/Entity/AbstractEav.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract class AbstractEav extends \Magento\ImportExport\Model\Import\AbstractEn
1616
/**
1717
* Attribute collection name
1818
*/
19-
const ATTRIBUTE_COLLECTION_NAME = 'Magento\Framework\Data\Collection';
19+
const ATTRIBUTE_COLLECTION_NAME = \Magento\Framework\Data\Collection::class;
2020

2121
/**
2222
* Store manager
@@ -222,9 +222,14 @@ public function getAttributeOptions(
222222
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
223223
$value = is_array($option['value']) ? $option['value'] : [$option];
224224
foreach ($value as $innerOption) {
225+
// skip ' -- Please Select -- ' option
225226
if (strlen($innerOption['value'])) {
226-
// skip ' -- Please Select -- ' option
227-
$options[strtolower($innerOption[$index])] = $innerOption['value'];
227+
if ($attribute->isStatic()) {
228+
$options[strtolower($innerOption[$index])] = $innerOption['value'];
229+
} else {
230+
// Non-static attributes flip keys an values
231+
$options[$innerOption['value']] = $innerOption[$index];
232+
}
228233
}
229234
}
230235
}

app/code/Magento/ImportExport/Test/Unit/Model/Import/EntityAbstractTest.php renamed to app/code/Magento/ImportExport/Test/Unit/Model/Import/AbstractEntityTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Magento\ImportExport\Model\Import\AbstractEntity;
1515

16-
class EntityAbstractTest extends \Magento\ImportExport\Test\Unit\Model\Import\AbstractImportTestCase
16+
class AbstractEntityTest extends \Magento\ImportExport\Test\Unit\Model\Import\AbstractImportTestCase
1717
{
1818
/**
1919
* Abstract import entity model
@@ -402,6 +402,36 @@ public function testIsAttributeValid(array $data)
402402
$this->assertEquals(1, $this->_model->getErrorAggregator()->getErrorsCount(), 'Wrong count of errors');
403403
}
404404

405+
/**
406+
* Test for method isAttributeValid() for multiselect attribute with custom separator.
407+
*
408+
* @return void
409+
*/
410+
public function testMultiSelectIsAttributeValidWithCustomSeparator()
411+
{
412+
$data = $this->_getDataSet(
413+
'test3',
414+
'select',
415+
'1;3',
416+
'custom',
417+
null,
418+
[1 => 'test1', 2 => 'test2', 3 => 'test3']
419+
);
420+
$attributeCode = $data['code'];
421+
$attributeParams = [
422+
'type' => $data['type'],
423+
'options' => isset($data['options']) ? $data['options'] : null,
424+
'is_unique' => isset($data['is_unique']) ? $data['is_unique'] : null,
425+
];
426+
427+
$rowData = [$attributeCode => $data['valid_value']];
428+
$this->assertTrue($this->_model->isAttributeValid($attributeCode, $attributeParams, $rowData, 0, ';'));
429+
430+
$rowData[$attributeCode] = $data['invalid_value'];
431+
$this->assertFalse($this->_model->isAttributeValid($attributeCode, $attributeParams, $rowData, 0, ';'));
432+
$this->assertEquals(1, $this->_model->getErrorAggregator()->getErrorsCount(), 'Wrong count of errors');
433+
}
434+
405435
/**
406436
* Data provide which retrieve data for test attributes
407437
*

0 commit comments

Comments
 (0)