-
Notifications
You must be signed in to change notification settings - Fork 26
fix!: update product.sales during migration #190
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
Merged
Dennis Garding (DennisGarding)
merged 6 commits into
trunk
from
12748/update-product-sales
May 26, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
65d696d
fix: update sales during migration
DennisGarding 33eb492
add changelog
DennisGarding ce32ac6
consider other orderstates
DennisGarding 73b1495
add entry to upgrade.md
DennisGarding 2abd2b0
Fix style upgrade.md
DennisGarding fe8569f
Fix phpstan issues
DennisGarding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| <?php declare(strict_types=1); | ||
| /* | ||
| * (c) shopware AG <info@shopware.com> | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace SwagMigrationAssistant\Migration\Service; | ||
|
|
||
| use Doctrine\DBAL\ArrayParameterType; | ||
| use Doctrine\DBAL\Connection; | ||
| use Shopware\Core\Checkout\Cart\LineItem\LineItem; | ||
| use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates; | ||
| use Shopware\Core\Checkout\Order\OrderStates; | ||
| use Shopware\Core\Defaults; | ||
| use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery; | ||
| use Shopware\Core\Framework\Log\Package; | ||
| use Shopware\Core\Framework\Uuid\Uuid; | ||
|
|
||
| #[Package('fundamentals@after-sales')] | ||
| final readonly class ProductSalesUpdater | ||
| { | ||
| public function __construct( | ||
| private Connection $connection, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string> $orderIds | ||
| * | ||
| * @return array<string> | ||
| */ | ||
| public function getProductIdsForOrders(array $orderIds): array | ||
| { | ||
| $orderIds = $this->filterIds($orderIds); | ||
|
|
||
| if ($orderIds === []) { | ||
| return []; | ||
| } | ||
|
|
||
| $sql = <<<'SQL' | ||
| SELECT DISTINCT order_line_item.product_id | ||
| FROM order_line_item | ||
| WHERE order_line_item.order_id IN (:orderIds) | ||
| AND order_line_item.version_id = :liveVersion | ||
| AND order_line_item.order_version_id = :liveVersion | ||
| AND order_line_item.product_version_id = :liveVersion | ||
| AND order_line_item.type = :lineItemType | ||
| AND order_line_item.product_id IS NOT NULL | ||
| SQL; | ||
|
|
||
| $productIds = $this->connection->fetchFirstColumn( | ||
| $sql, | ||
| [ | ||
| 'orderIds' => Uuid::fromHexToBytesList($orderIds), | ||
| 'liveVersion' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION), | ||
| 'lineItemType' => LineItem::PRODUCT_LINE_ITEM_TYPE, | ||
| ], | ||
| [ | ||
| 'orderIds' => ArrayParameterType::BINARY, | ||
| ] | ||
| ); | ||
|
|
||
| return \array_map( | ||
| static fn (string $productId): string => Uuid::fromBytesToHex($productId), | ||
| $productIds | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Recalculates the denormalized product.sales value from persisted live order line items. | ||
| * | ||
| * @param array<string> $productIds | ||
| */ | ||
| public function updateProducts(array $productIds): void | ||
| { | ||
| $productIds = $this->filterIds($productIds); | ||
|
|
||
| if ($productIds === []) { | ||
| return; | ||
| } | ||
|
|
||
| $productByteIds = Uuid::fromHexToBytesList($productIds); | ||
|
|
||
| $parameters = [ | ||
| 'productIds' => $productByteIds, | ||
| 'outerProductIds' => $productByteIds, | ||
| 'liveVersion' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION), | ||
| 'lineItemType' => LineItem::PRODUCT_LINE_ITEM_TYPE, | ||
| 'orderStates' => [ | ||
| OrderStates::STATE_OPEN, | ||
| OrderStates::STATE_COMPLETED, | ||
| ], | ||
| 'transactionStates' => [ | ||
| OrderTransactionStates::STATE_CANCELLED, | ||
| OrderTransactionStates::STATE_REFUNDED, | ||
| ], | ||
| ]; | ||
|
|
||
| $types = [ | ||
| 'productIds' => ArrayParameterType::BINARY, | ||
| 'outerProductIds' => ArrayParameterType::BINARY, | ||
| 'orderStates' => ArrayParameterType::STRING, | ||
| 'transactionStates' => ArrayParameterType::STRING, | ||
| ]; | ||
|
|
||
| $sql = <<<'SQL' | ||
| UPDATE product | ||
| LEFT JOIN ( | ||
| SELECT order_line_item.product_id, SUM(order_line_item.quantity) AS sales | ||
| FROM order_line_item | ||
| INNER JOIN `order` | ||
| ON `order`.id = order_line_item.order_id | ||
| AND `order`.version_id = order_line_item.order_version_id | ||
| AND `order`.version_id = :liveVersion | ||
| INNER JOIN state_machine_state order_state | ||
| ON order_state.id = `order`.state_id | ||
| INNER JOIN order_transaction primary_transaction | ||
| ON primary_transaction.id = `order`.primary_order_transaction_id | ||
| AND primary_transaction.version_id = `order`.primary_order_transaction_version_id | ||
| AND primary_transaction.order_id = `order`.id | ||
| AND primary_transaction.order_version_id = `order`.version_id | ||
| INNER JOIN state_machine_state transaction_state | ||
| ON transaction_state.id = primary_transaction.state_id | ||
| WHERE order_line_item.product_id IN (:productIds) | ||
| AND order_line_item.version_id = :liveVersion | ||
| AND order_line_item.product_version_id = :liveVersion | ||
| AND order_line_item.type = :lineItemType | ||
| AND order_state.technical_name IN (:orderStates) | ||
| AND transaction_state.technical_name NOT IN (:transactionStates) | ||
| GROUP BY order_line_item.product_id | ||
| ) product_sales | ||
| ON product_sales.product_id = product.id | ||
| SET product.sales = COALESCE(product_sales.sales, 0), | ||
| product.updated_at = NOW() | ||
| WHERE product.id IN (:outerProductIds) | ||
| AND product.version_id = :liveVersion | ||
| SQL; | ||
|
|
||
| RetryableQuery::retryable($this->connection, function () use ($sql, $parameters, $types): void { | ||
| $this->connection->executeStatement($sql, $parameters, $types); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string> $ids | ||
| * | ||
| * @return array<string> | ||
| */ | ||
| private function filterIds(array $ids): array | ||
| { | ||
| $filteredIds = \array_filter($ids, static fn (string $id): bool => Uuid::isValid($id)); | ||
|
|
||
| return \array_unique($filteredIds); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.