Skip to content

Commit

Permalink
feat: import tax category for product variants
Browse files Browse the repository at this point in the history
  • Loading branch information
olimination committed Nov 16, 2021
1 parent d480e40 commit 99c416f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Processor/ProductProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use Sylius\Component\Product\Model\ProductOptionValueInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Taxation\Model\TaxCategory;
use Sylius\Component\Taxation\Repository\TaxCategoryRepositoryInterface;
use Sylius\Component\Taxonomy\Factory\TaxonFactoryInterface;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;

Expand Down Expand Up @@ -84,6 +86,7 @@ final class ProductProcessor implements ResourceProcessorInterface
private $productVariantFactory;
/** @var RepositoryInterface */
private $productOptionValueRepository;
private TaxCategoryRepositoryInterface $taxCategoryRepository;

public function __construct(
ProductFactoryInterface $productFactory,
Expand All @@ -106,6 +109,7 @@ public function __construct(
ImageTypesProviderInterface $imageTypesProvider,
SlugGeneratorInterface $slugGenerator,
?TransformerPoolInterface $transformerPool,
TaxCategoryRepositoryInterface $taxCategoryRepository,
EntityManagerInterface $manager,
array $headerKeys
) {
Expand All @@ -131,6 +135,7 @@ public function __construct(
$this->productOptionValueRepository = $productOptionValueRepository;
$this->channelPricingFactory = $channelPricingFactory;
$this->channelPricingRepository = $channelPricingRepository;
$this->taxCategoryRepository = $taxCategoryRepository;
}

/**
Expand Down Expand Up @@ -195,13 +200,22 @@ private function loadProductFromParentCode(array $data): ProductInterface
return $mainProduct;
}

private function setTaxCategory(ProductVariantInterface $productVariant, array $data)
{
/** @var TaxCategory $taxCategory */
$taxCategory = $this->taxCategoryRepository->findOneBy(['code' => $data['tax_category_code']]);

if ($taxCategory) {
$productVariant->setTaxCategory($taxCategory);
}
}

/**
* @throws ImporterException
*/
private function setVariantAndOptions(ProductInterface $product, array $data): void
{
$productOptionValueList = $this->getProductOptionValueListFromData($data);

$productVariant = $this->getOrCreateProductVariant($product, $productOptionValueList);

$productVariant->setCode($data['Code']);
Expand All @@ -211,6 +225,7 @@ private function setVariantAndOptions(ProductInterface $product, array $data): v
$productVariant->setName(
$this->generateVariantName($data, $product->getName())
);
$this->setTaxCategory($productVariant, $data);

/** @var ProductOptionValueInterface $productOptionValue */
foreach ($productOptionValueList as $productOptionValue) {
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ services:
- '@sylius.service.product_image_code'
- "@sylius.generator.slug"
- "@sylius.importers_transformer_pool"
- "@sylius.repository.tax_category"
- "@doctrine.orm.entity_manager"
- ['Code', 'Locale', 'Name', 'Description', 'Short_description', 'Meta_description', 'Meta_keywords', 'Main_taxon', 'Taxons', 'Channels', 'Enabled', 'Price']

Expand Down

0 comments on commit 99c416f

Please sign in to comment.