This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,135 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Console\Command;
use Magento\Catalog\Api\CategoryLinkManagementInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable as ConfigurableResource;
use Magento\Framework\Console\Cli;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Synchronizes configurable product categories to child products.
*/
class CopyConfigurableCategoriesCommand extends Command
{
private const COMMAND_NAME = 'esun:catalog:copy-configurable-categories';
/**
* @param CollectionFactory $productCollectionFactory Product collection factory.
* @param ProductResource $productResource Product resource.
* @param ConfigurableResource $configurableResource Configurable relation resource.
* @param CategoryLinkManagementInterface $categoryLinkManagement Category link service.
*/
public function __construct(
private readonly CollectionFactory $productCollectionFactory,
private readonly ProductResource $productResource,
private readonly ConfigurableResource $configurableResource,
private readonly CategoryLinkManagementInterface $categoryLinkManagement
) {
parent::__construct(self::COMMAND_NAME);
}
/**
* Configure the command.
*
* @return void
*/
protected function configure(): void
{
$this->setDescription(
'Synchronize configurable product categories to all child products.'
);
}
/**
* Run the category copy.
*
* @param InputInterface $input Console input.
* @param OutputInterface $output Console output.
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$parents = $this->productCollectionFactory->create();
$parents->addFieldToFilter('type_id', Configurable::TYPE_CODE);
$updated = 0;
$failed = 0;
/** @var Product $parent */
foreach ($parents as $parent) {
$categoryIds = array_values(array_unique(array_map(
'intval',
$this->productResource->getCategoryIds($parent)
)));
if ($categoryIds === []) {
continue;
}
$expectedCategoryIds = $categoryIds;
sort($expectedCategoryIds);
$childrenByGroup = $this->configurableResource->getChildrenIds((int) $parent->getId());
$childIds = [];
foreach ($childrenByGroup as $group) {
foreach ($group as $childId) {
$childIds[(int) $childId] = (int) $childId;
}
}
$childIds = array_values($childIds);
if ($childIds === []) {
continue;
}
$children = $this->productCollectionFactory->create();
$children->addIdFilter($childIds);
$children->addAttributeToSelect('sku');
/** @var Product $child */
foreach ($children as $child) {
$childCategoryIds = array_values(array_unique(array_map(
'intval',
$this->productResource->getCategoryIds($child)
)));
sort($childCategoryIds);
if ($childCategoryIds === $expectedCategoryIds) {
continue;
}
try {
$this->categoryLinkManagement->assignProductToCategories(
(string) $child->getSku(),
$categoryIds
);
$updated++;
$output->writeln(sprintf(
'<info>Updated %s from parent %s.</info>',
$child->getSku(),
$parent->getSku()
));
} catch (\Throwable $exception) {
$failed++;
$output->writeln(sprintf(
'<error>Failed %s: %s</error>',
$child->getSku(),
$exception->getMessage()
));
}
}
}
$output->writeln(sprintf(
'<comment>Finished: %d updated, %d failed.</comment>',
$updated,
$failed
));
return $failed === 0 ? Cli::RETURN_SUCCESS : Cli::RETURN_FAILURE;
}
}
@@ -0,0 +1,96 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Console\Command;
use Esun\Catalog\Model\ResourceModel\SpecificationBrand\CollectionFactory as BrandCollectionFactory;
use Esun\Catalog\Model\SpecificationArchiveRebuilder;
use Magento\Framework\Console\Cli;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class RebuildSpecificationArchiveCommand extends Command
{
private const COMMAND_NAME = 'esun:catalog:specification-archive:rebuild';
private const OPTION_BRAND_ID = 'brand-id';
private const OPTION_FORCE = 'force';
public function __construct(
private readonly BrandCollectionFactory $brandCollectionFactory,
private readonly SpecificationArchiveRebuilder $archiveRebuilder
) {
parent::__construct(self::COMMAND_NAME);
}
protected function configure(): void
{
$this->setDescription('Rebuild downloadable specification ZIP archives from parsed specification files.');
$this->addOption(
self::OPTION_BRAND_ID,
null,
InputOption::VALUE_REQUIRED,
'Only rebuild the specified specification brand ID.'
);
$this->addOption(
self::OPTION_FORCE,
'f',
InputOption::VALUE_NONE,
'Overwrite an existing valid brand archive.'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$brandIdOption = $input->getOption(self::OPTION_BRAND_ID);
$brandId = $brandIdOption !== null ? filter_var($brandIdOption, FILTER_VALIDATE_INT) : null;
if ($brandIdOption !== null && (!$brandId || $brandId < 1)) {
$output->writeln('<error>The --brand-id option must be a positive integer.</error>');
return Cli::RETURN_FAILURE;
}
$collection = $this->brandCollectionFactory->create()
->addFieldToFilter('status', 1)
->setOrder('sort_order', 'ASC')
->setOrder('brand_name', 'ASC');
if ($brandId !== null) {
$collection->addFieldToFilter('brand_id', $brandId);
}
if (!$collection->getSize()) {
$output->writeln('<error>No enabled specification brands matched the command options.</error>');
return Cli::RETURN_FAILURE;
}
$force = (bool)$input->getOption(self::OPTION_FORCE);
$failed = 0;
foreach ($collection as $brand) {
$brandName = (string)$brand->getData('brand_name');
try {
$result = $this->archiveRebuilder->rebuild($brand, $force);
if ($result['status'] === 'skipped') {
$output->writeln(sprintf('<comment>Skipped %s: archive already exists.</comment>', $brandName));
continue;
}
$message = sprintf(
'%s %s: %d file(s), %d missing file(s), %s',
ucfirst($result['status']),
$brandName,
$result['file_count'],
$result['missing_count'],
$result['path']
);
$output->writeln('<info>' . $message . '</info>');
} catch (\Throwable $exception) {
$failed++;
$output->writeln(sprintf('<error>Failed %s: %s</error>', $brandName, $exception->getMessage()));
}
}
return $failed === 0 ? Cli::RETURN_SUCCESS : Cli::RETURN_FAILURE;
}
}