![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/Catalog/Console/ |
<?php /** * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Cnc * Arkadiusz Tokarczyk <[email protected]> <[email protected]> */ namespace Cnc\Catalog\Console; use Exception; use Magento\Catalog\Model\ResourceModel\Product\Action; use Magento\Catalog\Model\ResourceModel\Product\Collection; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; use Magento\Framework\App\Area; use Magento\Framework\App\State; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Filesystem\Driver\File; use Magento\Framework\Setup\SampleData\FixtureManager; use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Cnc\Catalog\Model\Config; class PopulateUseSerial extends Command { const POPULATE_EXCLUDE_ATTIBUTE_SETS = 'excludeSets'; const POPULATE_INCLUDE_OSCOMMERCE_IDS = 'includeOSIDs'; const INCLUDE_OSCOMMERCE_ID_FILE = 'Cnc_Catalog::fixtures/productsWithSerialNumber.csv'; /** * @var LoggerInterface */ private $logger; /** * @var CollectionFactory */ private $productCollectionFactory; /** * @var Action */ private $action; /** * @var State */ protected $state; protected $errors = []; /** * @var File */ private $file; /** * @var FixtureManager */ private $fixtureManager; /** * PopulateUseSerial constructor. * @param Action $productAction * @param State $state * @param CollectionFactory $productCollectionFactory * @param File $file * @param FixtureManager $fixtureManager * @param LoggerInterface $logger * @param string|null $name */ public function __construct( Action $productAction, State $state, CollectionFactory $productCollectionFactory, File $file, FixtureManager $fixtureManager, LoggerInterface $logger, string $name = null ) { parent::__construct($name); $this->action = $productAction; $this->state = $state; $this->productCollectionFactory = $productCollectionFactory; $this->file = $file; $this->fixtureManager = $fixtureManager; $this->logger = $logger; } /** * @inheritDoc */ protected function configure() { $this->setName('catalog:products:populate-use-serial'); $this->setDescription('Products Serial populate'); $this->addOption( self::POPULATE_EXCLUDE_ATTIBUTE_SETS, null, InputOption::VALUE_OPTIONAL, 'Exclude attribute sets from data populate' ); $this->addOption( self::POPULATE_INCLUDE_OSCOMMERCE_IDS, null, InputOption::VALUE_OPTIONAL, 'Include for specific OSCommerce IDs' ); parent::configure(); } /** * @param InputInterface $input * @param OutputInterface $output * @return int|void|null */ protected function execute( InputInterface $input, OutputInterface $output ) { try { $this->state->setAreaCode(Area::AREA_ADMINHTML); $output->writeln("Preparing products to populate..."); /** @var Collection $productsToUpdate */ $productsToUpdate = $this->productCollectionFactory->create(); if ($input->getOption(self::POPULATE_EXCLUDE_ATTIBUTE_SETS)) { //Exclude attribute sets IDs $excludeAttributeSets = explode(',', $input->getOption(self::POPULATE_EXCLUDE_ATTIBUTE_SETS)); $productsToUpdate->addFieldToFilter('attribute_set_id', ['nin' => $excludeAttributeSets]); } elseif ($input->getOption(self::POPULATE_INCLUDE_OSCOMMERCE_IDS)) { $includeIds = $this->getOSCommerceIdsFromFile(); $productsToUpdate->addAttributeToFilter('cnc_oscom_id', ['in' => $includeIds]); } if (count($productsToUpdate)) { $output->writeln('Processing...'); $i = 0; $batch = 1000; $products = []; foreach ($productsToUpdate as $product) { if ($i === $batch) { $output->writeln("Batch: " . $i); $this->action->updateAttributes($products, [Config::PRODUCT_ATTR_CODE_USE_SERIAL => true], 0); $products = []; $batch += 1000; } else { $i++; } $products[] = $product->getId(); } //process what is left $output->writeln("Batch: " . (++$i)); $this->action->updateAttributes($products, [Config::PRODUCT_ATTR_CODE_USE_SERIAL => true], 0); } else { $output->writeln("Nothing to process."); } } catch (Exception $e) { $output->writeln($e->getMessage()); $this->logger->critical($e->getMessage()); } } /** * @return int[] * @throws FileSystemException * @throws LocalizedException */ private function getOSCommerceIdsFromFile(): array { $data = []; $fileName = $this->fixtureManager->getFixture(self::INCLUDE_OSCOMMERCE_ID_FILE); $file = $this->file->fileOpen($fileName, "r"); while ($row = $this->file->fileGetCsv($file, 0, ",")) { $data[] = $row[0]; } return $data; } }