Skip to content

Commit

Permalink
PAC-894 Add clean-up for configurable products
Browse files Browse the repository at this point in the history
  • Loading branch information
svizevv committed Jul 24, 2024
1 parent b5aeb1c commit e3275ef
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 3 deletions.
61 changes: 61 additions & 0 deletions src/Actions/Processors/ProductRelationDeleteProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright (c) 2024 TechDivision GmbH <[email protected]> - TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see https://www.techdivision.com
*
* To obtain a valid license for using this software, please contact us at
* [email protected]
*/
declare(strict_types=1);

namespace TechDivision\Import\Product\Variant\Actions\Processors;

use TechDivision\Import\Dbal\Collection\Actions\Processors\AbstractBaseProcessor;
use TechDivision\Import\Product\Variant\Utils\MemberNames;
use TechDivision\Import\Product\Variant\Utils\SqlStatementKeys;

/**
* @copyright Copyright (c) 2024 TechDivision GmbH <[email protected]> - TechDivision GmbH
* @link http://www.techdivision.com
* @author MET <[email protected]>
*/
class ProductRelationDeleteProcessor extends AbstractBaseProcessor
{
/**
* Delete all relations that are not imported.
*
* @param array $row The row to persist
* @param string|null $name The name of the prepared statement that has to be executed
* @param string|null $primaryKeyMemberName The primary key member name of the entity to use
*
* @return void
*/
public function execute($row, $name = null, $primaryKeyMemberName = null): void
{
// load the SKUs from the row
$skus = $row[MemberNames::SKU];

// make sure we've an array
if (!is_array($skus)) {
$skus = [$skus];
}

// all SKUs that should NOT be deleted
$vals = implode(',', $skus);

// concatenate the SKUs as comma separated SQL string
$vals = str_replace(',', "','", sprintf("'%s'", $vals));

// replace the placeholders
$sql = str_replace(
[':parent_id', ':skus'],
[$row[MemberNames::PARENT_ID], $vals],
$this->loadStatement(SqlStatementKeys::DELETE_PRODUCT_RELATION)
);

$this->getConnection()->query($sql);
}
}
42 changes: 41 additions & 1 deletion src/Observers/CleanUpVariantProductRelationObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace TechDivision\Import\Product\Variant\Observers;

use Exception;
use TechDivision\Import\Utils\ProductTypes;
use TechDivision\Import\Observers\StateDetectorInterface;
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
Expand Down Expand Up @@ -154,6 +155,7 @@ protected function cleanUpVariants()
// delete not exists import variants from database
$this->cleanUpVariantChildren($parentId, $actualVariants);
$this->cleanUpVariantAttributes($parentId, $actualAttributes);
$this->cleanUpVariantRelation($parentId, $actualVariants);
} catch (\Exception $e) {
// log a warning if debug mode has been enabled
if ($this->getSubject()->isDebugMode()) {
Expand Down Expand Up @@ -197,7 +199,7 @@ protected function cleanUpVariantChildren($parentProductId, array $childData)
// log a debug message that the image has been removed
$this->getSubject()
->getSystemLogger()
->debug(
->info(
$this->getSubject()->appendExceptionSuffix(
sprintf(
'Successfully clean up variants for product with SKU "%s" except "%s"',
Expand Down Expand Up @@ -261,6 +263,44 @@ protected function cleanUpVariantAttributes($parentProductId, array $actualAttri
);
}

/**
* Delete not exists import relations from database.
*
* @param int $parentProductId The ID of the parent product
* @param array $childData The array of variants
*
* @return void
* @throws Exception
*/
protected function cleanUpVariantRelation(int $parentProductId, array $childData)
{
// we don't want to delete everything
if (empty($childData)) {
return;
}

// load the SKU of the parent product
$parentSku = $this->getValue(ColumnKeys::SKU);

// remove the old variants from the database
$this->getProductVariantProcessor()->deleteProductRelation(
[
MemberNames::PARENT_ID => $parentProductId,
MemberNames::SKU => $childData,
]
);

$subject = $this->getSubject();
$subject->getSystemLogger()->info(
$subject->appendExceptionSuffix(
sprintf(
'Successfully clean up relations for product with SKU "%s"',
$parentSku
)
)
);
}

/**
* Return's the PK to create the product => variant relation.
*
Expand Down
6 changes: 6 additions & 0 deletions src/Repositories/SqlStatementRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ class SqlStatementRepository extends \TechDivision\Import\Product\Repositories\S
WHERE product_id = :product_id
AND attribute_id
NOT IN (:attribute_ids)',
SqlStatementKeys::DELETE_PRODUCT_RELATION =>
'DELETE
FROM ${table:catalog_product_relation}
WHERE parent_id = :parent_id
AND child_id
NOT IN (SELECT `entity_id` FROM ${table:catalog_product_entity} WHERE `sku` IN (:skus))',
);

/**
Expand Down
13 changes: 13 additions & 0 deletions src/Services/ProductVariantProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,4 +651,17 @@ public function deleteProductSuperAttribute(array $row, $name = null)
{
return $this->getProductSuperAttributeAction()->delete($row, $name);
}

/**
* Deletes the passed product relation data.
*
* @param array $row The product relation to be deleted
* @param string|null $name The name of the prepared statement that has to be executed
*
* @return void
*/
public function deleteProductRelation(array $row, string $name = null): void
{
$this->getProductRelationAction()->delete($row, $name);
}
}
10 changes: 10 additions & 0 deletions src/Services/ProductVariantProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,14 @@ public function deleteProductSuperLink(array $row, $name = null);
* @return string The ID of the persisted product super attribute entity
*/
public function deleteProductSuperAttribute(array $row, $name = null);

/**
* Deletes the passed product relation data.
*
* @param array $row The product relation to be deleted
* @param null|string $name The name of the prepared statement that has to be executed
*
* @return void
*/
public function deleteProductRelation(array $row, string $name = null): void;
}
7 changes: 7 additions & 0 deletions src/Utils/SqlStatementKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ class SqlStatementKeys extends \TechDivision\Import\Product\Utils\SqlStatementKe
*/
const DELETE_PRODUCT_SUPER_ATTRIBUTE = 'delete.product_super_attribute';

/**
* The SQL statement to delete a product relation.
*
* @var string
*/
public const DELETE_PRODUCT_RELATION = 'delete.product_relation';

/**
* The SQL statement to create a new product super attribute label.
*
Expand Down
14 changes: 12 additions & 2 deletions symfony/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
<argument type="service" id="import_product_variant.repository.sql.statement"/>
<argument type="service" id="import.util.sql.sanitizers"/>
</service>
<service id="import_product_variant.action.processor.product.relation.delete" class="TechDivision\Import\Product\Variant\Actions\Processors\ProductRelationDeleteProcessor">
<argument type="service" id="connection"/>
<argument type="service" id="import_product_variant.repository.sql.statement"/>
<argument type="service" id="import.util.sql.sanitizers"/>
</service>

<service id="import_product_variant.action.product.super.attribute" class="TechDivision\Import\Dbal\Collection\Actions\GenericIdentifierAction">
<argument type="service" id="import_product_variant.action.processor.product.super.attribute.create"/>
Expand All @@ -132,6 +137,11 @@
<argument type="service" id="import_product_variant.action.processor.product.super.link.update" on-invalid="ignore"/>
<argument type="service" id="import_product_variant.action.processor.product.super.link.delete"/>
</service>
<service id="import_product.action.product.relation.variant" class="TechDivision\Import\Dbal\Collection\Actions\GenericAction">
<argument type="service" id="import_product.action.processor.product.relation.create"/>
<argument type="service" id="import_product.action.processor.product.relation.update" on-invalid="ignore"/>
<argument type="service" id="import_product_variant.action.processor.product.relation.delete"/>
</service>

<service id="import_product_variant.loader.raw.entity" class="TechDivision\Import\Product\Variant\Loaders\RawEntityLoader">
<argument type="service" id="connection"/>
Expand All @@ -147,7 +157,7 @@
<argument type="service" id="import_product_variant.repository.product.super.link"/>
<argument type="service" id="import_product_variant.repository.product.super.attribute"/>
<argument type="service" id="import_product_variant.repository.product.super.attribute.label"/>
<argument type="service" id="import_product.action.product.relation"/>
<argument type="service" id="import_product.action.product.relation.variant"/>
<argument type="service" id="import_product_variant.action.product.super.link"/>
<argument type="service" id="import_product_variant.action.product.super.attribute"/>
<argument type="service" id="import_product_variant.action.product.super.attribute.label"/>
Expand Down Expand Up @@ -234,4 +244,4 @@

</services>

</container>
</container>

0 comments on commit e3275ef

Please sign in to comment.