Skip to content

Commit

Permalink
MDEE-597: Prices are exported in "admin" website scope (#351)
Browse files Browse the repository at this point in the history
* MDEE-597: Prices are exported in "admin" website scope
  • Loading branch information
le0n4ik authored Oct 31, 2023
1 parent 00e1a84 commit 21dad30
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
28 changes: 24 additions & 4 deletions DataExporter/Model/FeedHashBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\DataExporter\Model;

use Magento\DataExporter\Model\Indexer\FeedIndexMetadata;
use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Serialize\SerializerInterface;

Expand All @@ -18,17 +19,21 @@ class FeedHashBuilder
{
private SerializerInterface $serializer;
private ResourceConnection $resourceConnection;
private CommerceDataExportLoggerInterface $logger;

/**
* @param SerializerInterface $serializer
* @param ResourceConnection $resourceConnection
* @param CommerceDataExportLoggerInterface $logger
*/
public function __construct(
SerializerInterface $serializer,
ResourceConnection $resourceConnection
ResourceConnection $resourceConnection,
CommerceDataExportLoggerInterface $logger
) {
$this->serializer = $serializer;
$this->resourceConnection = $resourceConnection;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -71,7 +76,15 @@ public function buildIdentifierFromFeedItem(array $feedItem, FeedIndexMetadata $
{
$identifier = [];
foreach ($metadata->getFeedIdentifierMappingFields() as $field) {
$this->addValue($identifier, array_key_exists($field, $feedItem) ? (string)$feedItem[$field] : '');
$value = array_key_exists($field, $feedItem) ? (string)$feedItem[$field] : null;
if ($value === null) {
$this->logger->error(
"Cannot build identifier for '$field' from feed item: " . var_export($feedItem, true)
);
continue;
}

$this->addValue($identifier, $value);
}
return $this->convertToString($identifier);
}
Expand All @@ -87,7 +100,14 @@ public function buildIdentifierFromFeedTableRow(array $row, FeedIndexMetadata $m
{
$identifier = [];
foreach (array_keys($metadata->getFeedIdentifierMappingFields()) as $columnName) {
$this->addValue($identifier, array_key_exists($columnName, $row) ? (string)$row[$columnName] : '');
$value = array_key_exists($columnName, $row) ? (string)$row[$columnName] : null;
if ($value === null) {
$this->logger->error(
"Cannot build identifier for '$columnName' from feed table: " . var_export($row, true)
);
continue;
}
$this->addValue($identifier, $value);
}
return $this->convertToString($identifier);
}
Expand All @@ -114,4 +134,4 @@ private function convertToString(array $identifier): string
{
return implode(',', $identifier);
}
}
}
15 changes: 14 additions & 1 deletion DataExporter/Model/Indexer/FeedIndexProcessorCreateUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ private function truncateIndexTable(FeedIndexMetadata $metadata): void
*/
private function filterFeedItems(array $feedItems, FeedIndexMetadata $metadata, &$processedHashes = null) : array
{
if (empty($feedItems)) {
return [];
}
$connection = $this->resourceConnection->getConnection();
$primaryKeyFields = $this->getFeedTablePrimaryKey($metadata);
$primaryKeys = \array_keys($feedItems);
Expand Down Expand Up @@ -235,6 +238,7 @@ private function addHashes(array $data, FeedIndexMetadata $metadata, bool $delet
if ($deleted) {
if (!isset($row[FeedIndexMetadata::FEED_TABLE_FIELD_FEED_HASH])) {
$this->logger->error("Feed hash is not set for the product id: ". $row['productId']);
unset($data[$key]);
continue ;
}
$identifier = $this->hashBuilder->buildIdentifierFromFeedTableRow($row, $metadata);
Expand All @@ -247,7 +251,16 @@ private function addHashes(array $data, FeedIndexMetadata $metadata, bool $delet
$identifier = $this->hashBuilder->buildIdentifierFromFeedItem($row, $metadata);
}
unset($data[$key]);

if (empty($identifier)) {
$this->logger->error(
'Identifier for feed item is empty. Skip sync for entity',
[
'feed' => $metadata->getFeedName(),
'item' => var_export($row, true)
]
);
continue;
}
$hash = $this->hashBuilder->buildHash($row, $metadata);
$this->addModifiedAtField($row, $metadata);
$data[$identifier] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ private function getCatalogRulePricesSelect(array $productIds): Select
->joinLeft(
['tier' => $this->resourceConnection->getTableName('catalog_product_entity_tier_price')],
\sprintf('product.%1$s = tier.%1$s', $this->getLinkField()) .
' AND tier.qty=1 AND tier.website_id in (0, product_website.website_id)',
' AND tier.qty=1 AND tier.all_groups = 0 AND tier.customer_group_id = rule.customer_group_id '
. 'AND tier.website_id in (0, product_website.website_id)',
[]
)
->columns([
Expand Down
3 changes: 2 additions & 1 deletion ProductPriceDataExporter/Model/Query/ProductPricesQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Magento\ProductPriceDataExporter\Model\Query;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product\Type;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Select;
Expand Down Expand Up @@ -131,6 +130,8 @@ public function getQuery(array $productIds, array $priceAttributes = []): Select
)
->where('product.entity_id IN (?)', $productIds)
->where('product.type_id NOT IN (?)', self::IGNORED_TYPES)
// exclude "admin" website
->where('store_website.website_id != ?', 0)
->order('product.entity_id')
->order('product_website.website_id')
->order('eav_store.attribute_id')
Expand Down

0 comments on commit 21dad30

Please sign in to comment.