skip to Main Content

0

I have created a custom tab for Alternative Products and have to import SKU’s through CSV like related_sku in CSV. I am unable to import my products in custom alternative products tab in product back-end. Please guide how I can achieve this import with CSV.

I have tried making additions in import module of core magento to see if new field is imported and associated to product alternate products

3

Answers


  1. To import custom field and its value using csv, in your custom module use following event

    ‘catalog_product_import_bunch_save_after’

    For example, if alternative_product_skus field is added as a custom field in csv(add column with this name in CSV) and its values sku1,sku2,sku3

    You can get this values using “catalog_product_import_bunch_save_after” event

    Vendor/Module/etc/adminhtml/events.html

    <?xml version="1.0" encoding="UTF-8" ?>
    <config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
            xsi:noNamespaceSchemaLocation='urn:magento:framework/Event/etc/events.xsd'>
        <event name="catalog_product_import_bunch_save_after">
            <observer name="vendor_module_catalog_product_import_bunch_save_after" instance="VendorModuleObserverCatalogProductImportBunchSaveAfter" />
        </event>
    </config>
    

    Vendor/Module/Observer/CatalogProductImportBunchSaveAfter.php

    <?php
    
    namespace VendorModuleObserver;
    
    use MagentoFrameworkEventObserver;
    use MagentoFrameworkEventObserverInterface;
    
    class CatalogProductImportBunchSaveAfter implements ObserverInterface
    {
        public function execute(MagentoFrameworkEventObserver $observer)
        {
            try{
                $bunch = $observer->getBunch();
                foreach($bunch as $product) {
                    // here you can get the row as a product object and get custom field value
                    $alternativeProducts = $product['alternative_product_skus'];
                    /*
                      ....
                      custom code to assign this values to the product
                      ....
                    */
                }
            }catch (Execption $e) {
                $e->getMessage(); 
            }
        }
    }
    
    Login or Signup to reply.
  2. Try this patch if you already have created a new relation type in your module.

    Verified on Magento 2.3.1 version

    diff --git a/vendor/magento/module-catalog-import-export/Model/Import/Product.php b/vendor/magento/module-catalog-import-export/Model/Import/Product.php
    index dc9d219..0e19ef8 100644
    --- a/vendor/magento/module-catalog-import-export/Model/Import/Product.php
    +++ b/vendor/magento/module-catalog-import-export/Model/Import/Product.php
    @@ -226,6 +226,7 @@ class Product extends MagentoImportExportModelImportEntityAbstractEntity
             '_related_' => MagentoCatalogModelProductLink::LINK_TYPE_RELATED,
             '_crosssell_' => MagentoCatalogModelProductLink::LINK_TYPE_CROSSSELL,
             '_upsell_' => MagentoCatalogModelProductLink::LINK_TYPE_UPSELL,
    +        '_custom_related_' => vendorModuleModelProduct::LINK_TYPE_CUSTOM_RELATED,
         ];
    
         /**
    @@ -334,6 +335,8 @@ class Product extends MagentoImportExportModelImportEntityAbstractEntity
             'min_sale_qty' => 'min_cart_qty',
             'max_sale_qty' => 'max_cart_qty',
             'notify_stock_qty' => 'notify_on_stock_below',
    +        '_custom_related_sku' => 'custom_related_sku',
    +        '_custom_related_position' => 'custom_related_position',
             '_related_sku' => 'related_skus',
             '_related_position' => 'related_position',
             '_crosssell_sku' => 'crosssell_skus',
    @@ -1257,7 +1260,7 @@ class Product extends MagentoImportExportModelImportEntityAbstractEntity
             $nextLinkId = $this->_resourceHelper->getNextAutoincrement($mainTable);
    
             // pre-load 'position' attributes ID for each link type once
    -        foreach ($this->_linkNameToId as $linkName => $linkId) {
    +        foreach ($this->getLinkNameToId() as $linkName => $linkId) {
                 $select = $this->_connection->select()->from(
                     $resource->getTable('catalog_product_link_attribute'),
                     ['id' => 'product_link_attribute_id']
    @@ -1292,7 +1295,7 @@ class Product extends MagentoImportExportModelImportEntityAbstractEntity
                         $linkKey = "{$productId}-{$linkData['linked_id']}-{$linkData['link_type_id']}";
                         $productLinkKeys[$linkKey] = $linkData['id'];
                     }
    -                foreach ($this->_linkNameToId as $linkName => $linkId) {
    +                foreach ($this->getLinkNameToId() as $linkName => $linkId) {
                         $productIds[] = $productId;
                         if (isset($rowData[$linkName . 'sku'])) {
                             $linkSkus = explode($this->getMultipleValueSeparator(), $rowData[$linkName . 'sku']);
    @@ -2333,6 +2336,16 @@ class Product extends MagentoImportExportModelImportEntityAbstractEntity
         }
    
         /**
    +     * Attribute set ID-to-name pairs getter.
    +     *
    +     * @return array
    +     */
    +    public function getLinkNameToId()
    +    {
    +        return $this->_linkNameToId;
    +    }
    +
    +    /**
          * DB connection getter.
          *
          * @return MagentoFrameworkDBAdapterAdapterInterface
    
    Login or Signup to reply.
  3. To get related products to show using a CSV import sheet. You need to add a ‘related sku’s’ column and a ‘related positions’ column in your CSV.
    Add the related SKU/SKU’s you want associated with that item or product in the row of the ‘related sku’s’ column. You can also set the position in ‘related position’ column.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search