【问题标题】:Import custom related products through CSV通过 CSV 导入自定义相关产品
【发布时间】:2020-02-19 06:53:42
【问题描述】:

0

我已经为替代产品创建了一个自定义选项卡,并且必须通过 CSV 导入 SKU,例如 CSV 中的related_sku。我无法在产品后端的自定义替代产品选项卡中导入我的产品。请指导我如何使用 CSV 实现此导入。

我已经尝试在核心 magento 的导入模块中添加,以查看是否导入了新字段并与产品替代产品相关联

【问题讨论】:

    标签: import magento2 csv-import


    【解决方案1】:

    要使用 csv 导入自定义字段及其值,请在自定义模块中使用以下事件

    'catalog_product_import_bunch_save_after'

    例如,如果 alternative_product_skus 字段作为自定义字段添加到 csv(在 CSV 中添加具有此名称的列)及其值 sku1,sku2,sku3

    您可以使用“catalog_product_import_bunch_save_after”事件获取此值

    供应商/模块/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="Vendor\Module\Observer\CatalogProductImportBunchSaveAfter" />
        </event>
    </config>
    

    供应商/模块/观察者/CatalogProductImportBunchSaveAfter.php

    <?php
    
    namespace Vendor\Module\Observer;
    
    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;
    
    class CatalogProductImportBunchSaveAfter implements ObserverInterface
    {
        public function execute(\Magento\Framework\Event\Observer $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(); 
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果您已经在模块中创建了新的关系类型,请尝试此补丁。
      在 Magento 2.3.1 版本上验证

      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 \Magento\ImportExport\Model\Import\Entity\AbstractEntity
               '_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,
      +        '_custom_related_' => \vendor\Module\Model\Product::LINK_TYPE_CUSTOM_RELATED,
           ];
      
           /**
      @@ -334,6 +335,8 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
               '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 \Magento\ImportExport\Model\Import\Entity\AbstractEntity
               $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 \Magento\ImportExport\Model\Import\Entity\AbstractEntity
                           $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 \Magento\ImportExport\Model\Import\Entity\AbstractEntity
           }
      
           /**
      +     * Attribute set ID-to-name pairs getter.
      +     *
      +     * @return array
      +     */
      +    public function getLinkNameToId()
      +    {
      +        return $this->_linkNameToId;
      +    }
      +
      +    /**
            * DB connection getter.
            *
            * @return \Magento\Framework\DB\Adapter\AdapterInterface
      

      【讨论】:

        【解决方案3】:

        使用 CSV 导入表显示相关产品。您需要在 CSV 中添加一个“相关 sku”列和一个“相关职位”列。 在“相关 sku”列的行中添加您想要与该项目或产品关联的相关 SKU/SKU。您还可以在“相关职位”列中设置职位。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-06
          • 2014-12-19
          • 2014-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多