Create products programmatically in Shopware 6 is easy. Let's learn how it works.
The example will be added inside SaveProductCommand.php
, part of MatheusGontijoHelloWorld
plugin.
services.xml
<service id="MatheusGontijo\HelloWorld\Command\SaveProductCommand">
<argument type="service" id="product.repository" />
<tag name="console.command" />
</service>
PHP
serviceYou can inject into any service. I'm currently injecting into a command. See the following:
<?php declare(strict_types=1);
namespace MatheusGontijo\HelloWorld\Command;
use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Framework\Context;
class SaveProductCommand extends Command
{
protected static $defaultName = 'matheus-gontijo:save-product';
protected EntityRepositoryInterface $productRepository;
public function __construct(
EntityRepositoryInterface $productRepository,
string $name = null
) {
parent::__construct($name);
$this->productRepository = $productRepository;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$suffix = uniqid();
$this->productRepository->create([[
'id' => Uuid::randomHex(),
'name' => 'This is a sample product ' . $suffix,
'taxId' => 'b6e10c80b24946a48bb74e742d280c75',
'stock' => 999,
'createdAt' => '2022-01-01T10:17:05+02:00',
'price' => [
[
'currencyId' => 'b7d2554b0ce847cd82f3ac9bd1c0dfca',
'gross' => 99,
'net' => 99,
'linked' => true,
]
],
'productNumber' => $suffix,
'visibilities' => [
[
'salesChannelId' => '5ce47a708e484e4d8c20221f8cdfa08f',
'visibility' => ProductVisibilityDefinition::VISIBILITY_ALL,
],
],
]], Context::createDefaultContext());
return self::SUCCESS;
}
}
After running my command three times, that's what I got on the admin:
And that's what I got in the storefront:
There we go! We got three new products added to our Shopware 6 store! Both on admin and storefront!
In case you have multiple sales channels and they are in different languages such as German (de-DE
), English (en-GB
) and Spanish (es-ES
), you can save the product like the following example. Pretty much the same, but array keys like name
and description
go under translations
. Make sure you pass the correct data for each respective language. See the example:
$this->productRepository->create([[
'id' => Uuid::randomHex(),
'taxId' => '45488b52278447539af75092596acd10',
'stock' => 999,
'createdAt' => '2022-01-01T00:01:01+00:00',
'price' => [
[
'currencyId' => 'b7d2554b0ce847cd82f3ac9bd1c0dfca',
'gross' => 99,
'net' => 99,
'linked' => true,
]
],
'productNumber' => uniqid(),
'visibilities' => [
[
'salesChannelId' => 'af27e0367c0a4096a4649e45563ad324',
'visibility' => ProductVisibilityDefinition::VISIBILITY_ALL,
],
[
'salesChannelId' => '643daa5c83f84c949b1b9fdb68cd0013',
'visibility' => ProductVisibilityDefinition::VISIBILITY_ALL,
],
[
'salesChannelId' => '15a59f8d6b7f4fd4afe3947f749af8b0',
'visibility' => ProductVisibilityDefinition::VISIBILITY_ALL,
],
],
'translations' => [
'en-GB' => [
'name' => 'Football ball',
'description' => 'This is a football ball.',
],
'de-DE' => [
'name' => 'Fußball',
'description' => 'Das ist ein Fußball.',
],
'es-ES' => [
'name' => 'Pelota de fútbol',
'description' => 'Esta es una pelota de fútbol.',
],
],
]], Context::createDefaultContext());
See the final result: the same product has been successfully saved in three different languages!
I have written complementary blogposts that might answer possible questions you have. Here they are:
Thanks for reading the blogpost. Talk to you later!