Magento2: CSV Import of `sku,attribute` empties `url_key` value

Created on 23 Jul 2018  路  11Comments  路  Source: magento/magento2

Preconditions

  1. Magento 2.2.5 installation with a product created.

Steps to reproduce

  1. Import a partial CSV with just sku,description columns.
  2. Check the Product to find its URL Key is gone.

Expected result

  1. URL Key should not be altered if it wasn't included in the CSV.

Actual result

  1. URL Key is changed to empty.
  2. See \Magento\CatalogImportExport\Model\Import\Product::getUrlKey
CatalogImportExport Fixed in 2.1.x Fixed in 2.2.x Clear Description Confirmed Format is valid Ready for Work Reproduced on 2.2.x Reproduced on 2.3.x

Most helpful comment

Hi

I have the same problem with the same magento verison 2.2.5, I've been debugging the code to figure it out what happends, because in another magento 2.2.2 I haven't this issues . I found a difference on a validation on the file vendor/magento/module-catalog-import-export/Model/Import/Product/Type/AbstractType.php (Line 537). And this change make the url_key dissapear.

Line on magento 2.2.2 (works properly)
if (!$attrParams['is_static'] && empty($rowData[$attrCode])) {

same line on Magento 2.2.4 and 2.2.5 (Fails)
if (!$attrParams['is_static'] && !isset($rowData[$attrCode])) {

I hope it helps to someone.

All 11 comments

Hi @josephmcdermott. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • [ ] Summary of the issue
  • [ ] Information on your environment
  • [ ] Steps to reproduce
  • [ ] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento-engcom-team give me {$VERSION} instance

where {$VERSION} is version tags (starting from 2.2.0+) or develop branches (2.2-develop +).
For more details, please, review the Magento Contributor Assistant documentation.

@josephmcdermott do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [ ] yes
  • [ ] no

@josephmcdermott, thank you for your report.
We've acknowledged the issue and added to our backlog.

Hi

I have the same problem with the same magento verison 2.2.5, I've been debugging the code to figure it out what happends, because in another magento 2.2.2 I haven't this issues . I found a difference on a validation on the file vendor/magento/module-catalog-import-export/Model/Import/Product/Type/AbstractType.php (Line 537). And this change make the url_key dissapear.

Line on magento 2.2.2 (works properly)
if (!$attrParams['is_static'] && empty($rowData[$attrCode])) {

same line on Magento 2.2.4 and 2.2.5 (Fails)
if (!$attrParams['is_static'] && !isset($rowData[$attrCode])) {

I hope it helps to someone.

Hello @magento-engcom-team , do you know why this validation was changed?

The issue looks to me to be simply related to the handling of the url_key column in this function called in the main loop for each row of the product import sheet:

foreach ($bunch as $rowNum => $rowData) {
    ...
    $rowData[self::URL_KEY] = $this->getUrlKey($rowData);
    ...
}
    protected function getUrlKey($rowData)
    {
        if (!empty($rowData[self::URL_KEY])) {
            return strtolower($rowData[self::URL_KEY]);
        }

        if (!empty($rowData[self::COL_NAME])) {
            return $this->productUrl->formatUrlKey($rowData[self::COL_NAME]);
        }

        return '';
    }

If the url_key column is not set in the data, then it will be set to '', and subsequently clears out the product's url attribute. I think there needs to be a tighter check of if the import sheet is intending to update the url_key or not here.

I also found other fid for this issue.
File: Magento\CatalogImportExport\Model\Import\Product
Method: _saveProducts
After change line:

$rowData[self::URL_KEY] = $urlKey;

to:

$urlKey = $this->getUrlKey($rowData);
if (!empty($urlKey)) {
    $rowData[self::URL_KEY] = $urlKey;
}

then column won't be set in import data, and url_key won't be set to empty.

Unfortunately that's not quite enough @evilprophet as if you import a CSV with sku,name only, all of the URL Keys will be replaced with the value provided in the name column.

I'm having a look at this currently, but have to say I'm disappointed at the lack of input from the Magento core team @magento-engcom-team since this is a pretty major issue.

@evilprophet take a look at the above PR, I think that covers all scenarios that I can tell. Of course it would be great to get feedback from @magento-engcom-team also to perform their own tests.

@josephmcdermott Yes, you are right I forgot about case when there is a name without url_key. Your solution looks a bit better.

For the record, I applied PR #17882 as a composer patch to a 2.2.5 Commerce installation and it fixed this issue for me. The merchant had a custom text field attribute and the url_key value was getting deleted when they uploaded a CSV that contained that column. Thanks for your contribution, @josephmcdermott!

Was this page helpful?
0 / 5 - 0 ratings