Files
stemcell/app/code/Stemcell/Catalog/Console/Command/ProtocolsDocumentationRepair.php
T
2026-09-15 10:26:34 +08:00

66 lines
2.5 KiB
PHP

<?php
namespace Stemcell\Catalog\Console\Command;
use Magento\Catalog\Model\ProductRepository;
use Magento\Framework\App\State;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\StateException;
use Symfony\Component\Console\Command\Command;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ProtocolsDocumentationRepair extends Command
{
public function __construct(
private ProductCollectionFactory $productCollectionFactory,
private ProductRepository $productRepository,
private State $state,
)
{
parent::__construct('protocols:documentation:repair');
}
public function execute(InputInterface $input, OutputInterface $output)
{
$this->state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
$store0Collection = $this->productCollectionFactory->create()
->setStoreId(0)
->addAttributeToFilter('protocols_documentation', ['neq' => 'NULL'])
->load();
$store1Collection = $this->productCollectionFactory->create()
->setStoreId(1)
->addAttributeToFilter('protocols_documentation', ['neq' => 'NULL'])
->load();
foreach ($store1Collection as $item) {
/* @var $item \Magento\Catalog\Model\Product */
/* @var $product \Magento\Catalog\Model\Product */
$product = $store0Collection->getItemById($item->getId());
$output->writeln('store 1 product: '. $item->getSku());
$output->writeln('store 0 product: '. $product->getSku());
$output->writeln('');
// $output->writeln('store 1 protocols_documentation:');
// $output->writeln($item->getData('protocols_documentation'));
//
// $output->writeln('store 0 protocols_documentation:');
// $output->writeln($product->getData('protocols_documentation'));
$item->setData('protocols_documentation', $product->getData('protocols_documentation'));
$item->setData('_edit_mode', true);
try {
$this->productRepository->save($item);
} catch (\Exception $e) {
$output->writeln('product: '.$item->getSku().'exception: <error> '.$e->getMessage().'</error>');
}
}
return Command::SUCCESS;
}
}