-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added the ability to sync variant meta fields #103
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e6e2842
Fixed #99 syncing variant metafields
nfourtythree 125c25e
Tidy readme
nfourtythree a56e98f
Migration to add `metafields` property to variants
nfourtythree e4772aa
fix cs
nfourtythree bf86cde
Fix readme table alignment
nfourtythree 5eb77d1
Dedicated settings section
AugustMiller 1c928d0
Merge remote-tracking branch 'origin/feature/variant-metafield-synchr…
AugustMiller a0bba48
No linebreak
AugustMiller 8a22415
Document other valid settings keys
AugustMiller 51f5155
Using environment variables
AugustMiller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,16 +117,16 @@ Products from your Shopify store are represented in Craft as product [elements]( | |
|
||
### Synchronization | ||
|
||
Once the plugin has been configured, you can perform an initial synchronization of all products via the **Shopify Sync** utility. | ||
Once the plugin has been configured, you can perform an initial synchronization of all products via the command line. | ||
|
||
> [!NOTE] | ||
> Larger stores with 100+ products should perform the initial synchronization via the command line instead: | ||
> | ||
> ```sh | ||
> php craft shopify/sync/products | ||
> ``` | ||
```sh | ||
php craft shopify/sync/products | ||
``` | ||
|
||
The [`syncProductMetafields` and `syncVariantMetafields` settings](#settings) govern what data is synchronized via this process. Going forward, your products will be automatically kept in sync via [webhooks](#set-up-webhooks). | ||
|
||
Going forward, your products will be automatically kept in sync via [webhooks](#set-up-webhooks). | ||
> [!NOTE] | ||
> Smaller stores with only a few products can perform synchronization via the **Shopify Sync** utility. | ||
### Native Attributes | ||
|
||
|
@@ -426,7 +426,8 @@ You can get an array of variant objects for a product by calling [`product.getVa | |
|
||
Unlike products, variants in Craft… | ||
|
||
- …are represented exactly as [the API](https://shopify.dev/api/admin-rest/2023-10/resources/product-variant#resource-object) returns them; | ||
- …are represented as [the API](https://shopify.dev/api/admin-rest/2023-10/resources/product-variant#resource-object) returns them; | ||
- …the `metafields` property is accessible in addition to the API’s returned properties; | ||
- …use Shopify’s convention of underscores in property names instead of exposing [camel-cased equivalents](#native-attributes); | ||
- …are plain associative arrays; | ||
- …have no methods of their own; | ||
|
@@ -441,6 +442,8 @@ Once you have a reference to a variant, you can output its properties: | |
|
||
> **Note** | ||
> The built-in [`currency`](https://craftcms.com/docs/4.x/dev/filters.html#currency) Twig filter is a great way to format money values. | ||
> | ||
> The `metafields` property will only be populated if the `syncVariantMetafields` setting is enabled. | ||
### Using Options | ||
|
||
|
@@ -723,7 +726,7 @@ It’s safe to remove the old plugin package (`nmaier95/shopify-product-fetcher` | |
For each legacy Shopify Product field in your project, do the following: | ||
|
||
1. Create a _new_ [Shopify Products](#product-field) field, giving it a a new handle and name; | ||
1. Create a _new_ [Shopify Products](#product-field) field, giving it a new handle and name; | ||
2. Add the field to any layouts where the legacy field appeared; | ||
|
||
### Re-saving Data | ||
|
@@ -782,6 +785,24 @@ There is no need to query the Shopify API to render product details in your temp | |
|
||
## Going Further | ||
|
||
### Settings | ||
|
||
The following settings can be controlled by creating a `shopify.php` file in your `config/` directory. | ||
|
||
| Setting | Type | Default | Description | | ||
|-------------------------|--------|---------|-------------| | ||
| `apiKey` | `string` | — | Shopify API key. | | ||
| `apiSecretKey` | `string` | — | Shopify API secret key. | | ||
| `accessToken` | `string` | — | Shopify API access token. | | ||
| `hostName` | `string` | — | Shopify [host name](#store-hostname). | | ||
| `uriFormat` | `string` | — | Product element URI format. | | ||
| `template` | `string` | — | Product element template path. | | ||
| `syncProductMetafields` | `bool` | `true` | Whether product metafields should be included when syncing products. This adds an extra API request per product. | | ||
| `syncVariantMetafields` | `bool` | `false` | Whether variant metafields should be included when syncing products. This adds an extra API request per variant. | | ||
|
||
> [!NOTE] | ||
> Setting `apiKey`, `apiSecretKey`, `accessToken`, and `hostName` via `shopify.php` will override Project Config values set via the control panel during [app setup](#create-a-shopify-app). You can still reference environment values from the config file with `craft\helpers\App::env()`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Collected |
||
### Events | ||
|
||
#### `craft\shopify\services\Products::EVENT_BEFORE_SYNCHRONIZE_PRODUCT` | ||
|
@@ -852,4 +873,4 @@ return [ | |
|
||
## Rate Limiting | ||
|
||
The Shopify API implements [rate limiting rules](https://shopify.dev/docs/api/usage/rate-limits) the plugin makes its best effort to avoid hitting these limits. | ||
The Shopify API implements [rate limiting rules](https://shopify.dev/docs/api/usage/rate-limits) the plugin makes its best effort to avoid hitting these limits. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/migrations/m240402_105857_add_metafields_property_to_variants.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace craft\shopify\migrations; | ||
|
||
use craft\db\Migration; | ||
use craft\db\Query; | ||
|
||
/** | ||
* m240402_105857_add_metafields_property_to_variants migration. | ||
*/ | ||
class m240402_105857_add_metafields_property_to_variants extends Migration | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function safeUp(): bool | ||
{ | ||
$productRows = (new Query()) | ||
->select(['shopifyId', 'variants']) | ||
->from('{{%shopify_productdata}}') | ||
->all(); | ||
|
||
foreach ($productRows as $product) { | ||
$variants = json_decode($product['variants'], true); | ||
foreach ($variants as &$variant) { | ||
if (isset($variant['metafields'])) { | ||
continue; | ||
} | ||
|
||
$variant['metafields'] = []; | ||
} | ||
|
||
$this->update('{{%shopify_productdata}}', ['variants' => json_encode($variants)], ['shopifyId' => $product['shopifyId']]); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function safeDown(): bool | ||
{ | ||
echo "m240402_105857_add_metafields_property_to_variants cannot be reverted.\n"; | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Smart inversion here. CLI is going to be more reliable for 99% of users!