Skip to content

Commit

Permalink
Disable old product while importing if without any variants
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Sep 30, 2022
1 parent b21db2e commit 6d1b73f
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/Product/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public function import(string $identifier): void

$product = $this->getOrCreateProductFromVariantResponse($productVariantResponse);

$this->disableOldParentProductIfItHasNotAnyVariants($identifier, $product);

$this->handleChannels($product, $productVariantResponse);

$this->handleTaxons($product, $productVariantResponse);
Expand Down Expand Up @@ -199,11 +201,6 @@ private function getOrCreateProductFromVariantResponse(array $productVariantResp
$product = $this->createNewProductFromAkeneoProduct($productVariantResponse);
}

$oldParentProduct = $this->productRepository->findOneByCode($identifier);
if ($oldParentProduct instanceof ProductInterface && count($oldParentProduct->getEnabledVariants()) <= 1) {
$oldParentProduct->setEnabled(false);
}

return $product;
}

Expand Down Expand Up @@ -360,4 +357,27 @@ public function reconcile(array $identifiersToReconcileWith): void
$this->dispatchPostEvent($product, 'update');
}
}

private function disableOldParentProductIfItHasNotAnyVariants(string $identifier, ProductInterface $product): void
{
$oldParentProduct = $this->productRepository->findOneByCode($identifier);
if ($oldParentProduct === null) {
return;
}
if ($oldParentProduct === $product) {
return;
}
if ($oldParentProduct->getVariants()->count() !== 1) {
return;
}
$productVariant = $oldParentProduct->getVariants()->first();
if (!$productVariant instanceof ProductVariantInterface) {
return;
}
if ($productVariant->getCode() !== $identifier) {
return;
}

$oldParentProduct->setEnabled(false);
}
}
110 changes: 110 additions & 0 deletions tests/Integration/DataFixtures/ApiClientMock/Product/1111111186.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"identifier": "1111111186",
"enabled": true,
"family": "shoes",
"categories": [
"master_men_shoes",
"print_shoes",
"supplier_abibas"
],
"groups": [],
"parent": "climbingshoe",
"values": {
"supplier": [
{
"locale": null,
"scope": null,
"data": "abibas"
}
],
"eu_shoes_size": [
{
"locale": null,
"scope": null,
"data": "380"
}
],
"collection": [
{
"locale": null,
"scope": null,
"data": [
"summer_2017"
]
}
],
"price": [
{
"locale": null,
"scope": null,
"data": [
{
"amount": "258.00",
"currency": "EUR"
}
]
}
],
"image": [
{
"locale": null,
"scope": null,
"data": "3/e/2/f/3e2f03ffb13ffde4ff67b9bacb013f68c5c11495_climbingshoe.jpg",
"_links": {
"download": {
"href": "http://127.0.0.1:8080/api/rest/v1/media-files/3/e/2/f/3e2f03ffb13ffde4ff67b9bacb013f68c5c11495_climbingshoe.jpg/download"
}
}
}
],
"name": [
{
"locale": null,
"scope": null,
"data": "climbingshoe"
}
],
"erp_name": [
{
"locale": "en_US",
"scope": null,
"data": "Climbing shoe"
}
],
"weight": [
{
"locale": null,
"scope": null,
"data": {
"amount": "800.0000",
"unit": "GRAM"
}
}
]
},
"created": "2022-04-14T13:14:08+00:00",
"updated": "2022-04-14T13:14:08+00:00",
"associations": {
"PACK": {
"products": [],
"product_models": [],
"groups": []
},
"UPSELL": {
"products": [],
"product_models": [],
"groups": []
},
"X_SELL": {
"products": [],
"product_models": [],
"groups": []
},
"SUBSTITUTION": {
"products": [],
"product_models": [],
"groups": []
}
},
"quantified_associations": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Sylius\Component\Core\Model\ProductTranslation:
climbingshoe_en_US:
locale: "en_US"
name: "Climbingshoe"
slug: "climbingshoe"
description: <paragraph(2)>
climbingshoe_it_IT:
locale: "it_IT"
name: "Scarpa da arrampicata"
slug: "scarpa-da-arrampicata"
description: <paragraph(2)>

Sylius\Component\Core\Model\Product:
climbingshoe:
fallbackLocale: "en_US"
currentLocale: "en_US"
code: "climbingshoe"
enabled: false
translations:
- "@climbingshoe_en_US"
- "@climbingshoe_it_IT"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Sylius\Component\Core\Model\ProductVariant:
climbingshoe_1111111186:
code: '1111111186'
product: '@climbingshoe'
26 changes: 26 additions & 0 deletions tests/Integration/Product/ImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -772,4 +772,30 @@ public function it_disable_old_product_while_importing_product_variant_from_conf
self::assertTrue($newProduct->isEnabled());
self::assertTrue($productVariant->isEnabled());
}

/** @test */
public function it_enables_product_without_variants_while_importing_a_new_one(): void
{
$this->fixtureLoader->load(
[
__DIR__ . '/../DataFixtures/ORM/resources/Locale/en_US.yaml',
__DIR__ . '/../DataFixtures/ORM/resources/Locale/it_IT.yaml',
__DIR__ . '/../DataFixtures/ORM/resources/ProductOption/eu_shoes_size.yaml',
__DIR__ . '/../DataFixtures/ORM/resources/Product/climbingshoe.yaml',
],
[],
[],
PurgeMode::createDeleteMode()
);

$this->importer->import('1111111186');

$product = $this->productRepository->findOneByCode('climbingshoe');
$productVariant = $this->productVariantRepository->findOneBy(['code' => '1111111186']);
self::assertInstanceOf(ProductInterface::class, $product);
self::assertInstanceOf(ProductVariantInterface::class, $productVariant);

self::assertTrue($product->isEnabled());
self::assertTrue($productVariant->isEnabled());
}
}

0 comments on commit 6d1b73f

Please sign in to comment.