Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add product stock quantity option #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 28 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 @@ -13,27 +18,31 @@ class ProductCreator extends AbstractProductCreator
private LangRepository $langRepository;
private Faker $faker;

private CommandBusInterface $commandBus;

public function __construct(
LangRepository $langRepository,
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 +69,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);
}
}
Loading