From 486463f4a88e55366f6789dd95bcd1ec45ac244d Mon Sep 17 00:00:00 2001 From: Matthieu Rolland Date: Tue, 21 May 2024 17:33:37 +0200 Subject: [PATCH] add product quantity option --- config/services.yml | 1 + src/Command/ProductCreatorCommand.php | 5 ++++- src/Creator/ProductCreator.php | 30 +++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/config/services.yml b/config/services.yml index 19e793d..ac8e833 100644 --- a/config/services.yml +++ b/config/services.yml @@ -23,6 +23,7 @@ services: $connection: '@doctrine.dbal.default_connection' $dbPrefix: '%database_prefix%' $faker: '@Faker\Generator' + $commandBus: '@prestashop.core.command_bus' PrestaShop\Module\PsFixturesCreator\Creator\OrderCreator: arguments: diff --git a/src/Command/ProductCreatorCommand.php b/src/Command/ProductCreatorCommand.php index 077218d..d05f253 100644 --- a/src/Command/ProductCreatorCommand.php +++ b/src/Command/ProductCreatorCommand.php @@ -67,6 +67,8 @@ protected function configure(): void ->addOption('attributes', null, InputOption::VALUE_OPTIONAL, 'Number of attributes per attribute group', 5) ->addOption('features', null, InputOption::VALUE_OPTIONAL, 'Number of features per product', 2) ->addOption('featureValues', null, InputOption::VALUE_OPTIONAL, 'Number of values per feature', 5) + ->addOption('stockQuantity', null, InputOption::VALUE_OPTIONAL, 'Quantiy available in stock per product', 0) + ; } @@ -81,10 +83,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int $numberOfFeatures = (int) $input->getOption('features'); $numberOfFeatureValues = (int) $input->getOption('featureValues'); $productsWithCombinations = (int) $input->getOption('productsWithCombinations'); + $stockQuantity = (int) $input->getOption('stockQuantity'); // create products if (!empty($numberOfProducts)) { - $this->productCreator->generate($numberOfProducts, $numberOfFeatures, $numberOfFeatureValues, $shopId); + $this->productCreator->generate($numberOfProducts, $numberOfFeatures, $numberOfFeatureValues, $shopId, $stockQuantity); $output->writeln(sprintf('%s product(s) created', $numberOfProducts)); } diff --git a/src/Creator/ProductCreator.php b/src/Creator/ProductCreator.php index 386267d..2481e33 100644 --- a/src/Creator/ProductCreator.php +++ b/src/Creator/ProductCreator.php @@ -2,8 +2,13 @@ namespace PrestaShop\Module\PsFixturesCreator\Creator; +use Context; use Doctrine\DBAL\Connection; +use Employee; use Faker\Generator as Faker; +use PrestaShop\PrestaShop\Core\CommandBus\CommandBusInterface; +use PrestaShop\PrestaShop\Core\Domain\Product\Stock\Command\UpdateProductStockAvailableCommand; +use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopConstraint; use PrestaShopBundle\Entity\Lang; use PrestaShopBundle\Entity\Repository\LangRepository; use Product; @@ -18,22 +23,24 @@ public function __construct( FeatureCreator $featureCreator, Connection $connection, string $dbPrefix, - Faker $faker + Faker $faker, + CommandBusInterface $commandBus ) { parent::__construct($featureCreator, $connection, $dbPrefix); $this->langRepository = $langRepository; $this->faker = $faker; + $this->commandBus = $commandBus; } - public function generate(int $number, int $numberOfFeatures, int $numberOfFeatureValues, int $shopId): void + public function generate(int $number, int $numberOfFeatures, int $numberOfFeatureValues, int $shopId, int $stockQuantity): void { for ($i = 0; $i < $number; ++$i) { - $productId = $this->createProduct($shopId); + $productId = $this->createProduct($shopId, $stockQuantity); $this->associateFeatures($productId, $numberOfFeatures, $numberOfFeatureValues, $shopId); } } - private function createProduct(int $shopId): int + private function createProduct(int $shopId, int $stockQuantity): int { $product = new Product(); $product->id_shop_list = [$shopId]; @@ -60,7 +67,22 @@ private function createProduct(int $shopId): int $categories = [2, 3, 4]; $product->updateCategories($categories); + $this->updateInStockQuantity($product->id, $shopId, $stockQuantity); return $product->id; } + + private function updateInStockQuantity(int $productId, int $shopId, int $stockQuantity): void + { + // Because we could be in CLI mode, there might be no employee in context, so we must set it manually + $context = Context::getContext(); + if (!isset($context->employee) || !isset($context->employee->id)) { + $context->employee = new Employee(1); + } + + $shopConstraint = ShopConstraint::shop($shopId); + $updateStockCommand = new UpdateProductStockAvailableCommand($productId, $shopConstraint); + $updateStockCommand->setDeltaQuantity($stockQuantity); + $this->commandBus->handle($updateStockCommand); + } }