Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions src/Jobs/ImportSingleProductJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,7 @@ public function handle()

// meta fields
try {
$metafields = collect(Arr::get($this->data, 'metafields.edges', []))->map(fn ($metafield) => $metafield['node'] ?? [])->filter()->all();

if ($metafields) {
$metafields = $this->parseMetafields($metafields, 'product');

if ($metafields) {
$entry->merge($metafields);
}
}
$this->syncMetafields($entry, Arr::get($this->data, 'metafields.edges', []), 'product');
} catch (\Throwable $e) {
Log::error('Could not retrieve metafields for product '.$this->data['id']);
Log::error($e->getMessage());
Expand Down Expand Up @@ -619,15 +611,7 @@ private function importVariants(array $returnedVariants, string $product_slug, G
$entry->merge($data);

try {
$metafields = collect(Arr::get($variant, 'metafields.edges', []))->map(fn ($metafield) => $metafield['node'] ?? [])->filter()->all();

if ($metafields) {
$metafields = $this->parseMetafields($metafields, 'product-variant');

if ($metafields) {
$entry->merge($metafields);
}
}
$this->syncMetafields($entry, Arr::get($variant, 'metafields.edges', []), 'product-variant');
} catch (\Throwable $e) {
Log::error('Could not retrieve metafields for variant '.$this->data['id']);
}
Expand Down Expand Up @@ -717,6 +701,27 @@ public function failed(Throwable $exception): void
ProductImportFailed::dispatch($this->productId, $this->storeHandle, $exception);
}

/**
* Sync metafields onto an entry, clearing any keys that were previously set
* but are no longer returned by Shopify (i.e. the metafield was deleted).
*/
private function syncMetafields(\Statamic\Contracts\Entries\Entry $entry, array $edges, string $context): void
{
$raw = collect($edges)->map(fn ($m) => $m['node'] ?? [])->filter()->all();
$parsed = $this->parseMetafields($raw, $context);

$previousKeys = $entry->get('shopify_metafield_keys', []);
foreach (array_diff($previousKeys, array_keys($parsed)) as $staleKey) {
$entry->set($staleKey, null);
}

if ($parsed) {
$entry->merge($parsed);
}

$entry->set('shopify_metafield_keys', array_keys($parsed));
}

/**
* Update the purchase history for this item
*/
Expand Down
111 changes: 111 additions & 0 deletions tests/Unit/ImportSingleProductJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,117 @@ public function updates_changed_handle()
$this->assertSame($entry->slug(), 'product-new');
}

#[Test]
public function clears_product_metafields_removed_in_shopify()
{
Facades\Collection::make(config('shopify.collection_handle', 'products'))->save();
Facades\Taxonomy::make()->handle('collections')->save();
Facades\Taxonomy::make()->handle('tags')->save();
Facades\Taxonomy::make()->handle('type')->save();
Facades\Taxonomy::make()->handle('vendor')->save();

$jsonWithMetafield = $this->getProductJson();
$jsonWithoutMetafield = str_replace(
'"edges": [
{
"node": {
"id" : 1,
"key": "some_metafield",
"value": "this is a value"
}
}
]',
'"edges": []',
$jsonWithMetafield
);

$this->mock(Graphql::class, function (MockInterface $mock) use ($jsonWithMetafield, $jsonWithoutMetafield) {
$mock
->shouldReceive('query')
->andReturn(
new HttpResponse(status: 200, body: $jsonWithMetafield),
new HttpResponse(status: 200, body: $jsonWithoutMetafield)
);
});

Jobs\ImportSingleProductJob::dispatch(1072481042);

$entry = Facades\Entry::whereCollection(config('shopify.collection_handle', 'products'))->first();
$this->assertSame($entry->get('some_metafield'), 'this is a value');

Jobs\ImportSingleProductJob::dispatch(1072481042);

$entry = Facades\Entry::whereCollection(config('shopify.collection_handle', 'products'))->first();
$this->assertNull($entry->get('some_metafield'));
$this->assertSame([], $entry->get('shopify_metafield_keys'));
}

#[Test]
public function clears_variant_metafields_removed_in_shopify()
{
Facades\Collection::make(config('shopify.collection_handle', 'products'))->save();
Facades\Taxonomy::make()->handle('collections')->save();
Facades\Taxonomy::make()->handle('tags')->save();
Facades\Taxonomy::make()->handle('type')->save();
Facades\Taxonomy::make()->handle('vendor')->save();

$jsonWithVariantMetafield = str_replace(
'"metafields": {
"edges": []
}',
'"metafields": {
"edges": [
{
"node": {
"id": 2,
"key": "variant_metafield",
"value": "variant value"
}
}
]
}',
$this->getProductJson()
);

$jsonWithoutVariantMetafield = str_replace(
'"metafields": {
"edges": [
{
"node": {
"id": 2,
"key": "variant_metafield",
"value": "variant value"
}
}
]
}',
'"metafields": {
"edges": []
}',
$jsonWithVariantMetafield
);

$this->mock(Graphql::class, function (MockInterface $mock) use ($jsonWithVariantMetafield, $jsonWithoutVariantMetafield) {
$mock
->shouldReceive('query')
->andReturn(
new HttpResponse(status: 200, body: $jsonWithVariantMetafield),
new HttpResponse(status: 200, body: $jsonWithoutVariantMetafield)
);
});

Jobs\ImportSingleProductJob::dispatch(1072481042);

$variant = Facades\Entry::whereCollection('variants')->first();
$this->assertSame($variant->get('variant_metafield'), 'variant value');

Jobs\ImportSingleProductJob::dispatch(1072481042);

$variant = Facades\Entry::whereCollection('variants')->first();
$this->assertNull($variant->get('variant_metafield'));
$this->assertSame([], $variant->get('shopify_metafield_keys'));
}

private function getProductJson(): string
{
return '{
Expand Down
Loading