Skip to content

Commit

Permalink
fix(orders): fix storing data in hpos
Browse files Browse the repository at this point in the history
INT-176
  • Loading branch information
EdieLemoine authored and myparcel-bot[bot] committed Oct 13, 2023
1 parent f9aeb23 commit ffc4f1a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Migration/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Migration/Pdk/OrdersMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
}
}
2 changes: 2 additions & 0 deletions src/Migration/Pdk/ProductSettingsMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/Pdk/Plugin/Repository/PdkOrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Check warning on line 113 in src/Pdk/Plugin/Repository/PdkOrderRepository.php

View check run for this annotation

Codecov / codecov/patch

src/Pdk/Plugin/Repository/PdkOrderRepository.php#L112-L113

Added lines #L112 - L113 were not covered by tests

$wcOrder->save();

Check warning on line 115 in src/Pdk/Plugin/Repository/PdkOrderRepository.php

View check run for this annotation

Codecov / codecov/patch

src/Pdk/Plugin/Repository/PdkOrderRepository.php#L115

Added line #L115 was not covered by tests

return $this->save($order->externalIdentifier, $order);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Pdk/Plugin/Repository/WcOrderNoteRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions src/Pdk/Product/Repository/WcPdkProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Mock/MockWcClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,19 @@ 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
{
return MockWpMeta::toWcMetaData($this->get_id());
}

public function save(): void
{
// do nothing
}

/**
* @param int|string $id
*
Expand All @@ -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
*
Expand Down

0 comments on commit ffc4f1a

Please sign in to comment.