Skip to content

Commit

Permalink
ENGCOM-2174: import-export-improvements #82 : super attribute error m…
Browse files Browse the repository at this point in the history
…essage improvements #115

 - Merge Pull Request magento-engcom/import-export-improvements#115 from tadhgbowe/import-export-improvements:import-export-issue-82-error-messaging
 - Merged commits:
   1. e99c99e
   2. 65bce0a
   3. 7f9f6db
   4. a76e3a3
   5. 3a0599d
   6. 6a5b15d
   7. b98f417
   8. 8db836b
  • Loading branch information
magento-engcom-team committed Jul 9, 2018
2 parents 0c2fa6f + 8db836b commit 6223196
Showing 1 changed file with 81 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class Configurable extends \Magento\CatalogImportExport\Model\Import\Product\Typ
/**
* Error codes.
*/
const ERROR_ATTRIBUTE_CODE_DOES_NOT_EXIST = 'attrCodeDoesNotExist';

const ERROR_ATTRIBUTE_CODE_NOT_GLOBAL_SCOPE = 'attrCodeNotGlobalScope';

const ERROR_ATTRIBUTE_CODE_NOT_TYPE_SELECT = 'attrCodeNotTypeSelect';

const ERROR_ATTRIBUTE_CODE_IS_NOT_SUPER = 'attrCodeIsNotSuper';

const ERROR_INVALID_OPTION_VALUE = 'invalidOptionValue';
Expand All @@ -39,10 +45,19 @@ class Configurable extends \Magento\CatalogImportExport\Model\Import\Product\Typ
* Validation failure message template definitions
*
* @var array
*
* Note: Some of these messages exceed maximum limit of 120 characters per line. Split up accordingly.
*/
protected $_messageTemplates = [
self::ERROR_ATTRIBUTE_CODE_IS_NOT_SUPER => 'Attribute with code "%s" is not super',
self::ERROR_INVALID_OPTION_VALUE => 'Invalid option value for attribute "%s"',
self::ERROR_ATTRIBUTE_CODE_DOES_NOT_EXIST => 'Column configurable_variations: Attribute with code ' .
'"%s" does not exist or is missing from product attribute set',
self::ERROR_ATTRIBUTE_CODE_NOT_GLOBAL_SCOPE => 'Column configurable_variations: Attribute with code ' .
'"%s" is not super - it needs to have Global Scope',
self::ERROR_ATTRIBUTE_CODE_NOT_TYPE_SELECT => 'Column configurable_variations: Attribute with code ' .
'"%s" is not super - it needs to be Input Type of Dropdown, Visual Swatch or Text Swatch',
self::ERROR_ATTRIBUTE_CODE_IS_NOT_SUPER => 'Column configurable_variations: Attribute with code ' .
'"%s" is not super',
self::ERROR_INVALID_OPTION_VALUE => 'Column configurable_variations: Invalid option value for attribute "%s"',
self::ERROR_INVALID_WEBSITE => 'Invalid website code for super attribute',
self::ERROR_DUPLICATED_VARIATIONS => 'SKU %s contains duplicated variations',
self::ERROR_UNIDENTIFIABLE_VARIATION => 'Configurable variation "%s" is unidentifiable',
Expand Down Expand Up @@ -289,10 +304,11 @@ protected function _isParticularAttributesValid(array $rowData, $rowNum)
{
if (!empty($rowData['_super_attribute_code'])) {
$superAttrCode = $rowData['_super_attribute_code'];

if (!$this->_isAttributeSuper($superAttrCode)) {
// check attribute superity
$this->_entityModel->addRowError(self::ERROR_ATTRIBUTE_CODE_IS_NOT_SUPER, $rowNum, $superAttrCode);
// Identify reason why attribute is not super:
if (!$this->identifySuperAttributeError($superAttrCode, $rowNum)) {
$this->_entityModel->addRowError(self::ERROR_ATTRIBUTE_CODE_IS_NOT_SUPER, $rowNum, $superAttrCode);
}
return false;
} elseif (isset($rowData['_super_attribute_option']) && strlen($rowData['_super_attribute_option'])) {
$optionKey = strtolower($rowData['_super_attribute_option']);
Expand All @@ -305,6 +321,66 @@ protected function _isParticularAttributesValid(array $rowData, $rowNum)
return true;
}

/**
* Identify exactly why a super attribute code is not super.
*
* @param string $superAttrCode
* @param int $rowNum
* @return bool
*/
private function identifySuperAttributeError($superAttrCode, $rowNum)
{
// This attribute code is not a super attribute. Need to give a clearer message why?
$reasonFound = false;
$codeExists = false;

// Does this attribute code exist?
$sourceAttribute = $this->doesSuperAttributeExist($superAttrCode);
if (is_array($sourceAttribute)) {
$codeExists = true;
// Does attribute have the correct settings?
if (isset($sourceAttribute['is_global']) && $sourceAttribute['is_global'] !== '1') {
$this->_entityModel->addRowError(self::ERROR_ATTRIBUTE_CODE_NOT_GLOBAL_SCOPE, $rowNum, $superAttrCode);
$reasonFound = true;
} elseif (isset($sourceAttribute['type']) && $sourceAttribute['type'] !== 'select') {
$this->_entityModel->addRowError(self::ERROR_ATTRIBUTE_CODE_NOT_TYPE_SELECT, $rowNum, $superAttrCode);
$reasonFound = true;
}
}

if ($codeExists === false) {
$this->_entityModel->addRowError(self::ERROR_ATTRIBUTE_CODE_DOES_NOT_EXIST, $rowNum, $superAttrCode);
$reasonFound = true;
}

return $reasonFound;
}

/**
* Does the super attribute exist in the current attribute set?
*
* @param string $superAttrCode
* @return array
*/
private function doesSuperAttributeExist($superAttrCode)
{
$returnAttributeArray = null;
if (is_array(self::$commonAttributesCache)) {
$filteredAttribute = array_filter(
self::$commonAttributesCache,
function ($element) use ($superAttrCode) {
return $element['code'] == $superAttrCode;
}
);

// Return the first element of the filtered array (if found).
if (count($filteredAttribute)) {
$returnAttributeArray = array_shift($filteredAttribute);
}
}
return $returnAttributeArray;
}

/**
* Array of SKU to array of super attribute values for all products.
*
Expand Down

0 comments on commit 6223196

Please sign in to comment.