Migration_20221028_105818_App.php
3 years ago
Migration_20230109_144830.php
3 years ago
Migration_20230131_121621.php
3 years ago
Migration_20230419_080000.php
3 years ago
Migration_20230425_211517.php
3 years ago
Migration_20230712_180341.php
3 years ago
Migration_20230803_200413_App.php
2 years ago
Migration_20230825_093531_App.php
1 year ago
Migration_20231128_120355_App.php
1 year ago
Migration_20240202_130053_App.php
2 years ago
Migration_20240207_105912_App.php
1 year ago
Migration_20240322_110443_App.php
2 years ago
Migration_20240730_212419_App.php
2 years ago
Migration_20241015_105511_App.php
1 year ago
Migration_20241128_114257_App.php
1 year ago
Migration_20250120_094614_App.php
1 year ago
Migration_20250501_114655_App.php
11 months ago
Migration_20260421_155908_App.php
3 months ago
Migration_20260515_120000_App.php
3 months ago
Migration_20260623_120000_App.php
1 month ago
Migration_20260805_120000_App.php
1 week ago
index.php
3 years ago
Migration_20240322_110443_App.php
75 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Migrations\App; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\StatisticsWooCommercePurchaseEntity; |
| 9 | use MailPoet\Migrator\AppMigration; |
| 10 | use MailPoet\WooCommerce\Helper; |
| 11 | use MailPoetVendor\Doctrine\DBAL\ArrayParameterType; |
| 12 | |
| 13 | class Migration_20240322_110443_App extends AppMigration { |
| 14 | public function run(): void { |
| 15 | $wooCommerceHelper = $this->container->get(Helper::class); |
| 16 | |
| 17 | // If Woo is not active and the table doesn't exist, we can skip this migration |
| 18 | if (!$wooCommerceHelper->isWooCommerceActive()) { |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | $purchaseStatisticsTable = $this->getTableName(StatisticsWooCommercePurchaseEntity::class); |
| 23 | $purchaseStatistics = $this->entityManager->getConnection()->fetchAllAssociative(" |
| 24 | SELECT order_id |
| 25 | FROM {$purchaseStatisticsTable} |
| 26 | "); |
| 27 | |
| 28 | global $wpdb; |
| 29 | if ($wooCommerceHelper->isWooCommerceCustomOrdersTableEnabled()) { |
| 30 | $ordersTable = $wooCommerceHelper->getOrdersTableName(); |
| 31 | $query = " |
| 32 | SELECT id AS order_id, status AS status |
| 33 | FROM `{$ordersTable}` |
| 34 | WHERE type = 'shop_order' AND id in (:orderIds) |
| 35 | "; |
| 36 | } else { |
| 37 | $query = " |
| 38 | SELECT wpp.id AS order_id, wpp.post_status AS status |
| 39 | FROM `{$wpdb->posts}` wpp |
| 40 | WHERE wpp.post_type = 'shop_order' |
| 41 | AND wpp.ID in (:orderIds) |
| 42 | "; |
| 43 | } |
| 44 | |
| 45 | foreach (array_chunk($purchaseStatistics, 2) as $chunk) { |
| 46 | $orderIds = array_column($chunk, 'order_id'); |
| 47 | |
| 48 | /** @var array{order_id: int, status: string}[] $orders */ |
| 49 | $orders = $this->entityManager->getConnection()->executeQuery( |
| 50 | $query, |
| 51 | ['orderIds' => $orderIds], |
| 52 | ['orderIds' => ArrayParameterType::INTEGER], |
| 53 | )->fetchAllAssociative(); |
| 54 | |
| 55 | foreach ($orders as $order) { |
| 56 | $this->entityManager->getConnection()->executeStatement(" |
| 57 | UPDATE {$purchaseStatisticsTable} |
| 58 | SET status = :status |
| 59 | WHERE order_id = :orderId |
| 60 | ", [ |
| 61 | 'orderId' => $order['order_id'], |
| 62 | 'status' => str_replace('wc-', '', $order['status']), // WC order status in DB is prefixed with 'wc-' |
| 63 | ]); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param class-string $entityClassName |
| 70 | */ |
| 71 | private function getTableName(string $entityClassName): string { |
| 72 | return $this->entityManager->getClassMetadata($entityClassName)->getTableName(); |
| 73 | } |
| 74 | } |
| 75 |