From d8f447cb12767ea3339572b252ba49103fe76e70 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Tue, 9 Apr 2024 08:49:52 +0100 Subject: [PATCH 1/9] Fixed unpacking variant meta fields --- CHANGELOG.md | 4 ++++ src/services/Products.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e2a01e..76c8804 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes for Shopify +## Unreleased + +- Fixed a bug where variant meta fields weren’t being unpacked. + ## 4.1.0 - 2024-04-03 - Added support for syncing variant meta fields. ([#99](https://github.com/craftcms/shopify/issues/99)) diff --git a/src/services/Products.php b/src/services/Products.php index 85320d0..d686f53 100644 --- a/src/services/Products.php +++ b/src/services/Products.php @@ -68,7 +68,7 @@ private function _updateProduct(ShopifyProduct $product): void foreach ($variants as &$variant) { $variantMetafields = $api->getMetafieldsByVariantId($variant['id']); - $variant['metafields'] = $variantMetafields; + $variant['metafields'] = MetafieldsHelper::unpack($variantMetafields); } $this->createOrUpdateProduct($product, $productMetafields, $variants); From 85682816679d07472f20a692d27307ab777110e9 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Tue, 9 Apr 2024 08:51:13 +0100 Subject: [PATCH 2/9] Fixed meta fields causing API rate limiting --- CHANGELOG.md | 1 + src/services/Api.php | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76c8804..e42b541 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Fixed a bug where syncing meta fields would cause Shopify API rate limiting. - Fixed a bug where variant meta fields weren’t being unpacked. ## 4.1.0 - 2024-04-03 diff --git a/src/services/Api.php b/src/services/Api.php index d6c595a..5e2b3ae 100644 --- a/src/services/Api.php +++ b/src/services/Api.php @@ -80,7 +80,7 @@ public function getProductIdByInventoryItemId($id): ?int 'inventory_item_id' => $id, ]); - if ($variant['variants']) { + if (isset($variant['variants'])) { return $variant['variants'][0]['product_id']; } @@ -99,7 +99,7 @@ public function getMetafieldsByProductId(int $id): array return []; } - return $this->getMetafieldsByIdAndOwnerResource($id, 'product'); + return $this->getMetafieldsByIdAndOwnerResource($id, 'products'); } /** @@ -125,14 +125,24 @@ public function getMetafieldsByVariantId(int $id): array public function getMetafieldsByIdAndOwnerResource(int $id, string $ownerResource): array { /** @var ShopifyMetafield[] $metafields */ - $metafields = $this->getAll(ShopifyMetafield::class, [ + $metafields = $this->get("{$ownerResource}/{$id}/metafields", [ 'metafield' => [ 'owner_id' => $id, 'owner_resource' => $ownerResource, ], ]); - return $metafields; + if (empty($metafields) || !isset($metafields['metafields'])) { + return []; + } + + $return = []; + + foreach ($metafields['metafields'] as $metafield) { + $return[] = new ShopifyMetafield($this->getSession(), $metafield); + } + + return $return; } /** @@ -144,7 +154,7 @@ public function getVariantsByProductId(int $id): array { $variants = $this->get("products/{$id}/variants"); - return $variants['variants']; + return $variants['variants'] ?? []; } /** From 78b31870ad7802aa59965c0dd99fed970559e73a Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Tue, 9 Apr 2024 08:52:13 +0100 Subject: [PATCH 3/9] Finish 4.1.1 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e42b541..c645645 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Release Notes for Shopify -## Unreleased +## 4.1.1 - 2024-04-09 - Fixed a bug where syncing meta fields would cause Shopify API rate limiting. - Fixed a bug where variant meta fields weren’t being unpacked. From 3cc23f5339d9380aadae53d5e66748f07816dfef Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Tue, 9 Apr 2024 08:52:53 +0100 Subject: [PATCH 4/9] Update ci workflow for v4 --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 040cfe1..b771feb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,6 +3,7 @@ on: workflow_dispatch: push: branches: + - v4 - develop - main pull_request: From 4aac71be2e50f4e71848f0155eca79907878614e Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Tue, 9 Apr 2024 08:55:59 +0100 Subject: [PATCH 5/9] fix phpstan --- src/services/Api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/Api.php b/src/services/Api.php index 5e2b3ae..10760e9 100644 --- a/src/services/Api.php +++ b/src/services/Api.php @@ -124,7 +124,7 @@ public function getMetafieldsByVariantId(int $id): array */ public function getMetafieldsByIdAndOwnerResource(int $id, string $ownerResource): array { - /** @var ShopifyMetafield[] $metafields */ + /** @var array $metafields */ $metafields = $this->get("{$ownerResource}/{$id}/metafields", [ 'metafield' => [ 'owner_id' => $id, From 72482b3021362d94a0b7136c088aeb1365389564 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Thu, 11 Apr 2024 14:00:37 +0100 Subject: [PATCH 6/9] Fixed date importing --- CHANGELOG.md | 4 ++++ src/services/Products.php | 7 ++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c645645..d830ad1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes for Shopify +## Unreleased + +- Fixed a PHP error that could occur when syncing products. ([#105](https://github.com/craftcms/shopify/issues/105)) + ## 4.1.1 - 2024-04-09 - Fixed a bug where syncing meta fields would cause Shopify API rate limiting. diff --git a/src/services/Products.php b/src/services/Products.php index d686f53..5e5b235 100644 --- a/src/services/Products.php +++ b/src/services/Products.php @@ -6,6 +6,7 @@ use craft\base\Component; use craft\events\ConfigEvent; use craft\helpers\ArrayHelper; +use craft\helpers\Db; use craft\helpers\ProjectConfig; use craft\models\FieldLayout; use craft\shopify\elements\Product; @@ -145,17 +146,17 @@ public function createOrUpdateProduct(ShopifyProduct $product, array $metafields 'shopifyId' => $product->id, 'title' => $product->title, 'bodyHtml' => $product->body_html, - 'createdAt' => $product->created_at, + 'createdAt' => Db::prepareDateForDb($product->created_at), 'handle' => $product->handle, 'images' => $product->images, 'options' => $product->options, 'productType' => $product->product_type, - 'publishedAt' => $product->published_at, + 'publishedAt' => Db::prepareDateForDb($product->published_at), 'publishedScope' => $product->published_scope, 'shopifyStatus' => $product->status, 'tags' => $product->tags, 'templateSuffix' => $product->template_suffix, - 'updatedAt' => $product->updated_at, + 'updatedAt' => Db::prepareDateForDb($product->updated_at), 'variants' => $variants ?? $product->variants, 'vendor' => $product->vendor, 'metaFields' => $metaFields, From 8829fca462d7d87adf8e7ad3c81c5b39ebf3bb4c Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Mon, 15 Apr 2024 09:30:24 +0100 Subject: [PATCH 7/9] Fixed #107 syncing products with emojis --- CHANGELOG.md | 1 + src/elements/Product.php | 10 ++++++++++ src/services/Products.php | 5 +++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d830ad1..1b29ecb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Fixed a PHP error that could occur when syncing products with emojis. ([#107](https://github.com/craftcms/shopify/issues/107)) - Fixed a PHP error that could occur when syncing products. ([#105](https://github.com/craftcms/shopify/issues/105)) ## 4.1.1 - 2024-04-09 diff --git a/src/elements/Product.php b/src/elements/Product.php index 299101b..9166aed 100644 --- a/src/elements/Product.php +++ b/src/elements/Product.php @@ -138,6 +138,16 @@ class Product extends Element */ public ?string $vendor = null; + /** + * @inheritdoc + */ + public function init(): void + { + $this->title = $this->title ? StringHelper::shortcodesToEmoji($this->title) : null; + $this->bodyHtml = $this->bodyHtml ? StringHelper::shortcodesToEmoji($this->bodyHtml) : null; + parent::init(); + } + /** * @inheritdoc */ diff --git a/src/services/Products.php b/src/services/Products.php index 5e5b235..aea33ae 100644 --- a/src/services/Products.php +++ b/src/services/Products.php @@ -8,6 +8,7 @@ use craft\helpers\ArrayHelper; use craft\helpers\Db; use craft\helpers\ProjectConfig; +use craft\helpers\StringHelper; use craft\models\FieldLayout; use craft\shopify\elements\Product; use craft\shopify\elements\Product as ProductElement; @@ -144,8 +145,8 @@ public function createOrUpdateProduct(ShopifyProduct $product, array $metafields // Build our attribute set from the Shopify product data: $attributes = [ 'shopifyId' => $product->id, - 'title' => $product->title, - 'bodyHtml' => $product->body_html, + 'title' => $product->title ? StringHelper::emojiToShortcodes($product->title) : null, + 'bodyHtml' => $product->body_html ? StringHelper::emojiToShortcodes($product->body_html) : null, 'createdAt' => Db::prepareDateForDb($product->created_at), 'handle' => $product->handle, 'images' => $product->images, From fcdd6e6c4f962b12ce435efe76138394f8eddab0 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Mon, 15 Apr 2024 14:57:30 +0100 Subject: [PATCH 8/9] Finish 4.1.2 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b29ecb..b538783 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Release Notes for Shopify -## Unreleased +## 4.1.2 - 2024-04-15 - Fixed a PHP error that could occur when syncing products with emojis. ([#107](https://github.com/craftcms/shopify/issues/107)) - Fixed a PHP error that could occur when syncing products. ([#105](https://github.com/craftcms/shopify/issues/105)) From 6739ff2da8796e5000cdb53aa3c61f15da52abd7 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Mon, 15 Apr 2024 15:01:03 +0100 Subject: [PATCH 9/9] Finish 5.1.1 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31a61db..bc8df82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Release Notes for Shopify -## Unreleased +## 5.1.1 - 2024-04-15 - Fixed a PHP error that could occur when syncing products with emojis. ([#107](https://github.com/craftcms/shopify/issues/107)) - Fixed a PHP error that could occur when syncing products. ([#105](https://github.com/craftcms/shopify/issues/105))