Skip to content

Commit

Permalink
add product quantity option
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Rolland authored and Matthieu Rolland committed May 21, 2024
1 parent aff685e commit 486463f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion src/Command/ProductCreatorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)

;
}

Expand All @@ -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));
}

Expand Down
30 changes: 26 additions & 4 deletions src/Creator/ProductCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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);
}
}

0 comments on commit 486463f

Please sign in to comment.