init
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Model\MixStepPricing;
|
||||
|
||||
use Esun\Catalog\Setup\Patch\Data\AddMixStepPricesAttribute;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\Product\Action;
|
||||
use Magento\Framework\App\CacheInterface;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Serialize\Serializer\Json;
|
||||
|
||||
class AttributeWriter
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Action $productAction,
|
||||
private readonly ResourceConnection $resourceConnection,
|
||||
private readonly Json $json,
|
||||
private readonly CacheInterface $cache
|
||||
) {
|
||||
}
|
||||
|
||||
public function write(array $groups, bool $delete): array
|
||||
{
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$writtenGroups = 0;
|
||||
$writtenStoreViews = 0;
|
||||
$connection->beginTransaction();
|
||||
|
||||
try {
|
||||
foreach ($groups as $group) {
|
||||
$value = $this->json->serialize($delete ? [] : $group['rules']);
|
||||
foreach ($group['store_ids'] as $storeId) {
|
||||
$this->productAction->updateAttributes(
|
||||
[(int)$group['product_id']],
|
||||
[AddMixStepPricesAttribute::ATTRIBUTE_CODE => $value],
|
||||
(int)$storeId
|
||||
);
|
||||
$writtenStoreViews++;
|
||||
}
|
||||
$writtenGroups++;
|
||||
}
|
||||
$connection->commit();
|
||||
$productIds = array_unique(array_map(
|
||||
static fn(array $group): int => (int)$group['product_id'],
|
||||
$groups
|
||||
));
|
||||
$this->cache->clean(array_map(
|
||||
static fn(int $productId): string => Product::CACHE_TAG . '_' . $productId,
|
||||
$productIds
|
||||
));
|
||||
} catch (\Throwable $exception) {
|
||||
$connection->rollBack();
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
return ['groups' => $writtenGroups, 'store_views' => $writtenStoreViews];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user