Skip to content

Commit

Permalink
[#12451] add delete if null mapping parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncarre committed Feb 13, 2019
1 parent f693f9d commit d61f51e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 1 addition & 2 deletions Helper/ImportHelper.php
Expand Up @@ -274,8 +274,7 @@ public function downloadVisualFromUrl($attributeValue, $attributeCode)

// Check visual
if (filesize($visualPath) < self::EXIF_IMAGETYPE_FILE_MIN_SIZE || exif_imagetype($visualPath) === false) {
$errorMessage = sprintf('Visual not valid for url %s', $attributeValue);
throw new \Exception($errorMessage);
return null;
}
}

Expand Down
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -58,7 +58,8 @@ Import mapping example:
{
"attributeCode": "lens_height",
"dataCode": "hauteurVerre",
"callback": "setMetricUnitAsSuffix"
"callback": "setMetricUnitAsSuffix",
"deleteIfNull": true
},
{
"attributeCode": "universe",
Expand Down Expand Up @@ -126,6 +127,7 @@ Mapping explanation:
* `onlyOnCreation`: Set attribute value only if product is new (checked with `identifier` attribute)
* `locales`: Used to set same attribute value for different locales
* `completeCallback`: Used to add some more fields with **ImportHelper**
* `deleteIfNull': Remove key from item mapping if value is null

### Export

Expand Down
16 changes: 16 additions & 0 deletions Reader/File/Csv/ProductAdvancedReader.php
Expand Up @@ -106,6 +106,13 @@ class ProductAdvancedReader extends ProductReader implements InitializableInterf
*/
const MAPPING_MAX_LENGTH_KEY = 'maxLength';

/**
* Delete if null key
*
* @var string
*/
const MAPPING_DELETE_IF_NULL = 'deleteIfNull';

/**
* Import helper
*
Expand Down Expand Up @@ -359,6 +366,15 @@ protected function updateByMapping($item)
$value = mb_substr($value, 0, $attributeMapping[self::MAPPING_MAX_LENGTH_KEY]);
}

// Don't add value if value is null
if (
isset($attributeMapping[self::MAPPING_DELETE_IF_NULL])
&& $attributeMapping[self::MAPPING_DELETE_IF_NULL] === true
&& $value === null
) {
continue;
}

$newItem[$attributesCode] = $value;
}
}
Expand Down

0 comments on commit d61f51e

Please sign in to comment.