2

We have created custom link types like consumable, recommended, etc..like cross-sells, upsells, related. Now we would like to implement the import functionality for these custom types. We have written below code on vendor file (Magento/CatalogImportExport/Model/Import/Product.php) so that while import custom link types are working as expected.

File Path: Vendor/Magento/CatalogImportExport/Model/Import/Product.php

 /**
     * Links attribute name-to-link type ID.
     *
     * @var array
     */
    protected $_linkNameToId = [
        '_related_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_RELATED,
        '_crosssell_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_CROSSSELL,
        '_upsell_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_UPSELL,
        '_required_' => \StackExchange\RequiredProduct\Model\Product\Link::LINK_TYPE_REQUIRED,
        '_consumable_' => \StackExchange\AdditionalItems\Model\Product\Link::LINK_TYPE_CONSUMABLE,
        '_recommended_' => \StackExchange\AdditionalItems\Model\Product\Link::LINK_TYPE_RECOMMENDED,
        '_optional_language_arts_' => \StackExchange\AdditionalItems\Model\Product\Link::LINK_TYPE_OPTIONAL_LANGUAGE_ARTS,
        '_fivedays_also_includes_' => \StackExchange\AdditionalItems\Model\Product\Link::LINK_TYPE_FIVEDAYS_ALSO_INCLUDES
    ];



/**
         * Map between import file fields and system fields/attributes.
         *
         * @var array
         */
    protected $_fieldsMap = [ ......
                                           .......                                         
            '_required_sku' => 'required_skus',
            '_consumable_sku' => 'consumable_skus',
            '_recommended_sku' => 'recommended_skus',
            '_optional_language_arts_sku' => 'optional_language_arts_skus',
            '_fivedays_also_includes_sku' => 'fivedays_also_includes_skus'
        ];

Now I would like to override and add my data from my custom module. Can anyone guide and provide me the steps, please.

I have followed below link but no luck

https://github.com/magepal/magento2-link-product

My CSV format looks like this:

enter image description here

1
  • Related question: How can those links be exported? magepal/magento2-link-product does not include that, right?
    – robsch
    Commented Jul 22, 2020 at 8:51

2 Answers 2

1

Finally, I found a solution for this I have the override Magento/CatalogImportExport/Model/Import/Product.php file

  1. Created di.xml in the following file path app/code/StackExchange/AdditionalItems/etc/di.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\CatalogImportExport\Model\Import\Product" type="StackExchange\AdditionalItems\Model\Import\Product"/>
    </config>
    
  2. Created file app/code/StackExchange/AdditionalItems/Model/Import/Product.php

    <?php
      namespace StackExchange\AdditionalItems\Model\Import;
     class Product extends \Magento\CatalogImportExport\Model\Import\Product
     {
    
    
    const COL_MEDIA_IMAGE = '_media_image';
    
    const COL_STORE = '_store';
    
    const COL_TYPE = 'product_type';
    
    const COL_ATTR_SET = '_attribute_set';
    
    const COL_PRODUCT_WEBSITES = '_product_websites';    
    
    protected $_linkNameToId = [
        '_related_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_RELATED,
        '_crosssell_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_CROSSSELL,
        '_upsell_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_UPSELL,
        '_required_' => \Kensium\RequiredProduct\Model\Product\Link::LINK_TYPE_REQUIRED,
        '_consumable_' => \Kensium\AdditionalItems\Model\Product\Link::LINK_TYPE_CONSUMABLE,
        '_recommended_' => \Kensium\AdditionalItems\Model\Product\Link::LINK_TYPE_RECOMMENDED,
        '_optional_language_arts_' => \Kensium\AdditionalItems\Model\Product\Link::LINK_TYPE_OPTIONAL_LANGUAGE_ARTS,
        '_fivedays_also_includes_' => \Kensium\AdditionalItems\Model\Product\Link::LINK_TYPE_FIVEDAYS_ALSO_INCLUDES
    ];
    
    protected $_fieldsMap = [
        'image' => 'base_image',
        'image_label' => "base_image_label",
        'thumbnail' => 'thumbnail_image',
        'thumbnail_label' => 'thumbnail_image_label',
        self::COL_MEDIA_IMAGE => 'additional_images',
        '_media_image_label' => 'additional_image_labels',
        '_media_is_disabled' => 'hide_from_product_page',
        self::COL_STORE => 'store_view_code',
        self::COL_ATTR_SET => 'attribute_set_code',
        self::COL_TYPE => 'product_type',
        self::COL_PRODUCT_WEBSITES => 'product_websites',
        'status' => 'product_online',
        'news_from_date' => 'new_from_date',
        'news_to_date' => 'new_to_date',
        'options_container' => 'display_product_options_in',
        'minimal_price' => 'map_price',
        'msrp' => 'msrp_price',
        'msrp_enabled' => 'map_enabled',
        'special_from_date' => 'special_price_from_date',
        'special_to_date' => 'special_price_to_date',
        'min_qty' => 'out_of_stock_qty',
        'backorders' => 'allow_backorders',
        'min_sale_qty' => 'min_cart_qty',
        'max_sale_qty' => 'max_cart_qty',
        'notify_stock_qty' => 'notify_on_stock_below',
        '_related_sku' => 'related_skus',
        '_related_position' => 'related_position',
        '_crosssell_sku' => 'crosssell_skus',
        '_crosssell_position' => 'crosssell_position',
        '_upsell_sku' => 'upsell_skus',
        '_upsell_position' => 'upsell_position',
        'meta_keyword' => 'meta_keywords',
        '_required_sku' => 'required_skus',
        '_consumable_sku' => 'consumable_skus',
        '_recommended_sku' => 'recommended_skus',
        '_optional_language_arts_sku' => 'optional_language_arts_skus',
        '_fivedays_also_includes_sku' => 'fivedays_also_includes_skus'
    ];    
    
    
     }
    
  3. Added sequence from my custom module

     <?xml version="1.0"?>
       <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="StackExchange_AdditionalItems" setup_version="1.2.1">
           <sequence>                   
              <module name="Magento_CatalogImportExport"/>
           </sequence>
    </module>
    

  4. php bin/magento setup:di:compile

1
  • 2
    In upcoming Magento 2.4 it should work via di.xml - not need anymore to overwrite the Product.php
    – Alex
    Commented May 7, 2020 at 10:23
1

As with Magento 2.4 and higher, you can fix this with use of dependency injection and a plugin. This approach is cleaner and more upgrade proof than a class rewrite.

Let's say the code of the custom link type is custom_type and you have a module Vendor_Module, you will need this code:

app/code/Vendor/Module/etc/adminhtml/di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\CatalogImportExport\Model\Import\Product\LinkProcessor">
        <arguments>
            <argument name="linkNameToId" xsi:type="array">
                <!-- Const should be id of link type as added in db table catalog_product_link_type -->
                <item name="_custom_type_" xsi:type="const">Vendor\Module\Model\Product\Link::LINK_TYPE_CUSTOM_TYPE</item>
            </argument>
        </arguments>
    </type>
    <type name="Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType">
        <plugin sortOrder="1" name="vendorModuleAbstractType" type="Vendor\Module\Plugin\Model\Import\Product\Type\AbstractTypePlugin"/>
    </type>
</config>

app/code/Vendor/Module/Plugin/Model/Import/Product/Type/AbstractTypePlugin:

<?php

namespace Vendor\Module\Plugin\Model\Import\Product\Type;

use Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType;

class AbstractTypePlugin
{
    /**
     * @param AbstractType $subject
     * @param string[]     $result
     * @return string[]
     */
    public function afterGetCustomFieldsMapping(AbstractType $subject, array $result): array
    {
        $result['_custom_type_sku'] = 'custom_type_skus';

        return $result;
    }
}

After cache flush you can import custom link type with product import in admin using a csv file like this:

"sku","custom_type_skus"
"product_sku","linked_sku_1,linked_sku_2"
1
  • 1
    This works fine for me on Magento 2.4.5 as well.
    – vijay rami
    Commented Nov 13, 2022 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.