Skip to content

Commit

Permalink
Merge branch 'v4' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
  • Loading branch information
nfourtythree committed Apr 15, 2024
2 parents 64fdc51 + fcdd6e6 commit 91f4099
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Release Notes for Shopify

## 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))
- Fixed a bug where syncing meta fields would cause Shopify API rate limiting.
- Fixed a bug where variant meta fields weren’t being unpacked.

## 5.1.0 - 2024-04-03

- Added support for syncing variant meta fields. ([#99](https://github.com/craftcms/shopify/issues/99))
Expand All @@ -11,6 +18,16 @@

- Shopify now requires Craft CMS 5.0.0-beta.10 or later.

## 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))

## 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.

## 4.1.0 - 2024-04-03

- Added support for syncing variant meta fields. ([#99](https://github.com/craftcms/shopify/issues/99))
Expand Down
10 changes: 10 additions & 0 deletions src/elements/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
22 changes: 16 additions & 6 deletions src/services/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

Expand All @@ -99,7 +99,7 @@ public function getMetafieldsByProductId(int $id): array
return [];
}

return $this->getMetafieldsByIdAndOwnerResource($id, 'product');
return $this->getMetafieldsByIdAndOwnerResource($id, 'products');
}

/**
Expand All @@ -124,15 +124,25 @@ public function getMetafieldsByVariantId(int $id): array
*/
public function getMetafieldsByIdAndOwnerResource(int $id, string $ownerResource): array
{
/** @var ShopifyMetafield[] $metafields */
$metafields = $this->getAll(ShopifyMetafield::class, [
/** @var array $metafields */
$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;
}

/**
Expand All @@ -144,7 +154,7 @@ public function getVariantsByProductId(int $id): array
{
$variants = $this->get("products/{$id}/variants");

return $variants['variants'];
return $variants['variants'] ?? [];
}

/**
Expand Down
14 changes: 8 additions & 6 deletions src/services/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use craft\base\Component;
use craft\events\ConfigEvent;
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;
Expand Down Expand Up @@ -68,7 +70,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);
Expand Down Expand Up @@ -143,19 +145,19 @@ 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,
'createdAt' => $product->created_at,
'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,
'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,
Expand Down

0 comments on commit 91f4099

Please sign in to comment.