Skip to content

Instantly share code, notes, and snippets.

@ProxiBlue
Created July 20, 2020 07:28
Show Gist options
  • Save ProxiBlue/225833ae404bb0f596ced68c2190ea57 to your computer and use it in GitHub Desktop.
Save ProxiBlue/225833ae404bb0f596ced68c2190ea57 to your computer and use it in GitHub Desktop.
DO NOT USE: sku cleaning
<?php
namespace ProxiBlue\Shell\Console\Command;
use Magento\Setup\Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
class RemoveSkuSpaces extends Command
{
/**
* @var CollectionFactory
*/
private $productCollectionFactory;
/** @var \Magento\Framework\App\State * */
private $appState;
/**
* @param CollectionFactory $productCollectionFactory
*/
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Framework\App\State $appState
)
{
$this->appState = $appState;
parent::__construct();
$this->productCollectionFactory = $productCollectionFactory;
}
protected function configure()
{
$this->setName('proxiblue-shell:remove-sku-space')
->setDescription('Fix product skus that have spaces pre/post fixed');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->setDecorated(true);
$this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
$collection = $this->productCollectionFactory->create();
foreach ($collection as $item) {
$output->writeln("--" . $item->getSku() . "--");
try {
$item->setSku(trim($item->getSku()))->save();
} catch (\Exception $e) {
$output->writeln("=> " . $item->getSku() . " " . $e->getMessage() );
}
}
}
}
@ProxiBlue
Copy link
Author

DO not use this script it will wipe data. It is now an example of NOT how todo this.

Item is not a fully loaded product
but save operation treats it as a fully loaded product

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment