我们遇到了同样的问题,似乎导入模块在相关产品中存在某种错误
我们通过编写一个希望将2列(SKU家长和儿童的SKU)的新控制台命令解决它related.csv文件VAR文件夹,用逗号作为分隔符的CSV和管道为children_skus分离器
这是文件,如果您想尝试。您将用所需的供应商名称替换Sinapsis,并用所需的模块名称同步
安装模块后,运行bin/magento setup:upgrade
&如果选中bin/magento list
,将看到新命令,可以通过运行来执行bin/magento sync:related
更新
从2.2。*版本开始,需要进行2处更改:保存之前需要额外的一行$product
,以防止在此处报告问题,网址为https://github.com/magento/magento2/issues/10687
$product->unsetData('media_gallery');
并将admin更改为adminhtml in
$this->_appState->setAreaCode('adminhtml');
我认为旧版本的第一个更改是无害的,第二个版本则不一样。所以我只在下面的代码中添加了第一个
应用程序/代码/ Sinapsis /同步/ 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">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="sync_related" xsi:type="object">Sinapsis\Sync\Console\Command\RelatedCommand</item>
</argument>
</arguments>
</type>
应用程序/代码/ Sinapsis /同步/ etc / module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="module.xsd">
<module name="Sinapsis_Sync" setup_version="1.0.0">
</module>
应用/代码/ Sinapsis /同步/ registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Sinapsis_Sync',
__DIR__
);
应用程序/代码/ Sinapsis /同步/控制台/命令/ RelatedCommand.php
<?php
namespace Sinapsis\Sync\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\App\State as AppState;
use Magento\Framework\App\Filesystem\DirectoryList;
class RelatedCommand extends Command
{
const CSV_SEPARATOR = ',';
const CHILDREN_SEPARATOR = '|';
protected $_appState;
protected $_objectManager;
protected $_directorylist;
public function __construct(
DirectoryList $_directorylist,
AppState $appState,
ObjectManagerInterface $objectManager
) {
$this->_appState = $appState;
$this->_objectManager = $objectManager;
$this->_directorylist = $_directorylist;
parent::__construct();
}
protected function configure()
{
$this->setName('sync:related')
->setDescription('Synchronize catalog related products');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<info>Starting process...<info>');
$output->writeln('');
$this->_appState->setAreaCode('admin');
$productRepository = $this->_objectManager->create('Magento\Catalog\Model\ProductRepository');
$output->writeln('<info>Loading csv content...<info>');
$output->writeln('');
$filePath = $this->_directorylist->getPath('var') . DIRECTORY_SEPARATOR . 'related.csv';
//@todo control Exception if file does not exist
$parseData = array();
if (($handle = fopen($filePath, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, self::CSV_SEPARATOR)) !== FALSE) {
$parseData[] = $data;
}
fclose($handle);
} else {
$output->writeln('<info>Could not read .csv file<info>');
return;
}
$headers = array_shift($parseData); // remove headers
foreach ($parseData as $row){
$skuParent = trim($row[0]);
$skuChildren = trim($row[1]);
$output->writeln('<info>Loading parent product ' . $skuParent . ' ... <info>');
try {
$product = $productRepository->get($skuParent);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e){
$output->writeln('<info>Could not load!<info>');
continue;
}
$links = $product->getProductLinks();
$children = explode(self::CHILDREN_SEPARATOR, $skuChildren);
$i = 1;
foreach ($children as $skuChild){
$output->writeln('<info>Loading related product ' . $skuChild . ' ... <info>');
try {
$child = $productRepository->get($skuChild);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e){
$output->writeln('<info>Could not load!<info>');
continue;
}
$productLink = $this->_objectManager->create('Magento\Catalog\Api\Data\ProductLinkInterface')
->setSku($skuParent)
->setLinkedProductSku($skuChild)
->setPosition($i)
->setLinkType('related');
$links[] = $productLink;
$i++;
}
$product->setProductLinks($links);
$product->unsetData('media_gallery');
$productRepository->save($product);
$output->writeln('<info>Relations saved for ' . $skuParent . '<info>');
}
$output->writeln('');
$output->writeln('<info>Done<info>');
}
}