Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

price-formation-changes #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\CatalogPriceDataExporter\Model\Indexer;

use Magento\CatalogDataExporter\Model\Provider\Product\ProductOptions\CustomizableEnteredOptionValueUid;
use Magento\CatalogDataExporter\Model\Provider\Product\ProductOptions\CustomizableSelectedOptionValueUid;
use Magento\CatalogDataExporter\Model\Provider\Product\ProductOptions\DownloadableLinksOptionUid;

/**
* Class responsible for indexing product price build event data
*/
class PriceBuilder
{
/**
* @var CustomizableEnteredOptionValueUid
*/
private $optionValueUid;

/**
* @var DownloadableLinksOptionUid
*/
private $downloadableLinksOptionUid;

/**
* @param CustomizableEnteredOptionValueUid $optionValueUid
* @param DownloadableLinksOptionUid $downloadableLinksOptionUid
*/
public function __construct(
CustomizableEnteredOptionValueUid $optionValueUid,
DownloadableLinksOptionUid $downloadableLinksOptionUid
) {
$this->optionValueUid = $optionValueUid;
$this->downloadableLinksOptionUid = $downloadableLinksOptionUid;
}

/**
* Build event data for product custom option price
*
* @param array $data
*
* @return array
*
* @throws \InvalidArgumentException
*/
public function buildCustomOptionPriceEventData(array $data): array
{
$id = $this->optionValueUid->resolve([CustomizableEnteredOptionValueUid::OPTION_ID => $data['option_id']]);

return [
'id' => $id,
'value' => $data['price'],
'price_type' => $data['price_type'],
];
}

/**
* Build complex product event data.
*
* @param string $parentId
* @param string $variationId
* @param string $linkType
*
* @return array
*/
public function buildComplexProductEventData(string $parentId, string $variationId, string $linkType): array
{
return [
'id' => $parentId,
'variation_id' => $variationId,
'price_type' => $linkType,
];
}

/**
* Build custom option type price event data
*
* @param array $data
*
* @return array
*
* @throws \InvalidArgumentException
*/
public function buildCustomOptionTypePriceEventData(array $data): array
{
$id = $this->optionValueUid->resolve(
[
CustomizableSelectedOptionValueUid::OPTION_ID => $data['option_id'],
CustomizableSelectedOptionValueUid::OPTION_VALUE_ID => $data['option_type_id'],
]
);

return [
'id' => $id,
'value' => $data['price'],
'price_type' => $data['price_type'],
];
}

/**
* Build downloadable link price event data.
*
* @param string $entityId
* @param string $value
*
* @return array
*
* @throws \InvalidArgumentException
*/
public function buildDownloadableLinkPriceEventData(string $entityId, string $value): array
{
$id = $this->downloadableLinksOptionUid->resolve([DownloadableLinksOptionUid::OPTION_ID => $entityId]);
return [
'id' => $id,
'value' => $value,
];
}

/**
* Build product price event data.
*
* @param string $entityId
* @param string $attributeCode
* @param string|null $attributeValue
*
* @return array
*/
public function buildProductPriceEventData(string $entityId, string $attributeCode, ?string $attributeValue): array
{
return [
'id' => $entityId,
'attribute_code' => $attributeCode,
'value' => $attributeValue,
];
}

/**
* Build tier price event data.
*
* @param string $entityId
* @param string $qty
* @param string|null $priceType
* @param string|null $value
* @return array
*/
public function buildTierPriceEventData(string $entityId, string $qty, ?string $priceType, ?string $value): array
{
return [
'id' => $entityId,
'attribute_code' => 'tier_price',
'qty' => $qty,
'price_type' => $priceType,
'value' => $value,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\CatalogPriceDataExporter\Model\Provider\FullReindex;

use Magento\CatalogPriceDataExporter\Model\EventKeyGenerator;
use Magento\CatalogPriceDataExporter\Model\Indexer\PriceBuilder;
use Magento\CatalogPriceDataExporter\Model\Query\ComplexProductLink;
use Magento\DataExporter\Exception\UnableRetrieveData;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Api\Data\WebsiteInterface;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;

/**
* Class responsible for providing complex product variation change events for full indexation
*/
class ComplexProductEvent implements FullReindexPriceProviderInterface
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var ComplexProductLink
*/
private $complexProductLink;

/**
* @var EventKeyGenerator
*/
private $eventKeyGenerator;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var PriceBuilder
*/
private $priceBuilder;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var string
*/
private $linkType;

/**
* @param ResourceConnection $resourceConnection
* @param ComplexProductLink $complexProductLink
* @param EventKeyGenerator $eventKeyGenerator
* @param StoreManagerInterface $storeManager
* @param PriceBuilder $priceBuilder
* @param LoggerInterface $logger
* @param string $linkType
*/
public function __construct(
ResourceConnection $resourceConnection,
ComplexProductLink $complexProductLink,
EventKeyGenerator $eventKeyGenerator,
StoreManagerInterface $storeManager,
PriceBuilder $priceBuilder,
LoggerInterface $logger,
string $linkType
) {
$this->resourceConnection = $resourceConnection;
$this->complexProductLink = $complexProductLink;
$this->eventKeyGenerator = $eventKeyGenerator;
$this->storeManager = $storeManager;
$this->priceBuilder = $priceBuilder;
$this->logger = $logger;
$this->linkType = $linkType;
}

/**
* @inheritdoc
*/
public function retrieve(): \Generator
{
$continue = true;
$lastKnownId = 0;
try {
while ($continue === true) {
$result = [];
$select = $this->complexProductLink->getQuery(null, null, (int)$lastKnownId, self::BATCH_SIZE);
$cursor = $this->resourceConnection->getConnection()->query($select);
while ($row = $cursor->fetch()) {
$result[$row['parent_id']][] = $row['variation_id'];
$lastKnownId = $row['link_id'];
}
if (empty($result)) {
$continue = false;
} else {
yield $this->getEventsData($result);
}
}
} catch (\Throwable $exception) {
$this->logger->error($exception->getMessage());
throw new UnableRetrieveData('Unable to retrieve complex product link data for full sync.');
}
}

/**
* Retrieve prices event data
*
* @param array $data
*
* @return array
*
* @throws LocalizedException
*/
private function getEventsData(array $data): array
{
$events = [];
$websiteId = (string)$this->storeManager->getWebsite(WebsiteInterface::ADMIN_CODE)->getWebsiteId();
$key = $this->eventKeyGenerator->generate(self::EVENT_VARIATION_CHANGED, $websiteId, null);
$linkType = $this->linkType;
foreach ($data as $parentId => $children) {
foreach ($children as $variationId) {
$events[$key][] = $this->priceBuilder->buildComplexProductEventData(
(string)$parentId,
$variationId,
$linkType
);
}
}
return $events;
}
}