diff --git a/src/Migration/AbstractMigration.php b/src/Migration/AbstractMigration.php index b077f6b49..abc8663f8 100644 --- a/src/Migration/AbstractMigration.php +++ b/src/Migration/AbstractMigration.php @@ -61,7 +61,7 @@ protected function markMigrated(WC_Data $object): void return; } - update_post_meta($object->get_id(), $migratedKey, array_merge($executedMigrations, [$this->getVersion()])); + $object->update_meta_data($migratedKey, array_merge($executedMigrations, [$this->getVersion()])); } /** diff --git a/src/Migration/Pdk/OrdersMigration.php b/src/Migration/Pdk/OrdersMigration.php index af1fe7f76..aca5bb369 100644 --- a/src/Migration/Pdk/OrdersMigration.php +++ b/src/Migration/Pdk/OrdersMigration.php @@ -379,8 +379,7 @@ private function savePdkData(WC_Order $wcOrder): void { $fulfilmentData = $wcOrder->get_meta(self::LEGACY_META_PPS_EXPORTED); - update_post_meta( - $wcOrder->get_id(), + $wcOrder->update_meta_data( Pdk::get('metaKeyOrderData'), Utils::filterNull([ 'apiIdentifier' => $fulfilmentData['pps_uuid'] ?? null, @@ -389,14 +388,15 @@ private function savePdkData(WC_Order $wcOrder): void ]) ); - update_post_meta($wcOrder->get_id(), Pdk::get('metaKeyOrderShipments'), $this->getShipments($wcOrder)); + $wcOrder->update_meta_data(Pdk::get('metaKeyOrderShipments'), $this->getShipments($wcOrder)); - update_post_meta( - $wcOrder->get_id(), + $wcOrder->update_meta_data( Pdk::get('metaKeyVersion'), $wcOrder->get_meta(self::LEGACY_META_ORDER_VERSION) ); $this->markMigrated($wcOrder); + + $wcOrder->save(); } } diff --git a/src/Migration/Pdk/ProductSettingsMigration.php b/src/Migration/Pdk/ProductSettingsMigration.php index 4a3f6de0c..8e6218618 100644 --- a/src/Migration/Pdk/ProductSettingsMigration.php +++ b/src/Migration/Pdk/ProductSettingsMigration.php @@ -365,6 +365,8 @@ private function migrateProduct(WC_Product $wcProduct): void $this->debug(sprintf('Settings for product %s migrated', $wcProduct->get_id())); $this->markMigrated($wcProduct); + + $wcProduct->save(); } /** diff --git a/src/Pdk/Plugin/Repository/PdkOrderRepository.php b/src/Pdk/Plugin/Repository/PdkOrderRepository.php index 03878188e..82cc5ef36 100644 --- a/src/Pdk/Plugin/Repository/PdkOrderRepository.php +++ b/src/Pdk/Plugin/Repository/PdkOrderRepository.php @@ -109,12 +109,10 @@ public function update(PdkOrder $order): PdkOrder ->getShipments($wcOrder) ->mergeByKey($order->shipments, 'id'); - update_post_meta($wcOrder->get_id(), Pdk::get('metaKeyOrderData'), $order->toStorableArray()); - update_post_meta( - $wcOrder->get_id(), - Pdk::get('metaKeyOrderShipments'), - $order->shipments->toStorableArray() - ); + $wcOrder->update_meta_data(Pdk::get('metaKeyOrderData'), $order->toStorableArray()); + $wcOrder->update_meta_data(Pdk::get('metaKeyOrderShipments'), $order->shipments->toStorableArray()); + + $wcOrder->save(); return $this->save($order->externalIdentifier, $order); } diff --git a/src/Pdk/Plugin/Repository/WcOrderNoteRepository.php b/src/Pdk/Plugin/Repository/WcOrderNoteRepository.php index c7bd79426..5187e37e1 100644 --- a/src/Pdk/Plugin/Repository/WcOrderNoteRepository.php +++ b/src/Pdk/Plugin/Repository/WcOrderNoteRepository.php @@ -141,7 +141,10 @@ protected function getKeyPrefix(): string */ private function saveNotes(string $orderId, array $notes): void { - update_post_meta((int) $orderId, Pdk::get('metaKeyOrderNotes'), $notes); + $wcOrder = $this->wcOrderRepository->get($orderId); + + $wcOrder->update_meta_data(Pdk::get('metaKeyOrderNotes'), $notes); + $wcOrder->save(); // Invalidate cache $this->storage->delete($this->getKeyPrefix() . $orderId); diff --git a/src/Pdk/Product/Repository/WcPdkProductRepository.php b/src/Pdk/Product/Repository/WcPdkProductRepository.php index 1916a6f3a..232fffd11 100644 --- a/src/Pdk/Product/Repository/WcPdkProductRepository.php +++ b/src/Pdk/Product/Repository/WcPdkProductRepository.php @@ -96,11 +96,10 @@ public function getProducts(array $identifiers = []): PdkProductCollection */ public function update(PdkProduct $product): void { - update_post_meta( - (int) $product->externalIdentifier, - Pdk::get('metaKeyProductSettings'), - $product->settings->toStorableArray() - ); + $wcProduct = $this->getWcProduct($product->externalIdentifier); + + $wcProduct->update_meta_data(Pdk::get('metaKeyProductSettings'), $product->settings->toStorableArray()); + $wcProduct->save(); $this->save($product->externalIdentifier, $product); } diff --git a/tests/Mock/MockWcClass.php b/tests/Mock/MockWcClass.php index c9c446e80..dcc154377 100644 --- a/tests/Mock/MockWcClass.php +++ b/tests/Mock/MockWcClass.php @@ -96,6 +96,7 @@ public function get_meta($key = '', $single = true, $context = 'view') /** * @return array of objects. + * @throws \Throwable * @see \WC_Product::get_meta_data() */ public function get_meta_data(): array @@ -103,6 +104,11 @@ public function get_meta_data(): array return MockWpMeta::toWcMetaData($this->get_id()); } + public function save(): void + { + // do nothing + } + /** * @param int|string $id * @@ -113,6 +119,18 @@ public function set_id($id): void $this->attributes['id'] = $id; } + /** + * @param string $key + * @param array|string $value + * @param int $meta_id + * + * @return void + */ + public function update_meta_data($key, $value, $meta_id = 0): void + { + MockWpMeta::update($this->get_id(), $key, $value); + } + /** * @param array $data *