Skip to content

Commit

Permalink
Merge pull request #3 from BKWLD/support-multi-store
Browse files Browse the repository at this point in the history
Support multiple Stores per Craft Site
  • Loading branch information
weotch authored Apr 22, 2022
2 parents 8cc82ec + d47bd0f commit 00c807f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor
.DS_Store
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,18 @@ Set up FeedMe to query either of the following routes. We're only exposing a mi
},
]
```

#### Multiple Shopify stores

If you have multiple Shopify stores but one Craft instance (like if you are using Craft's multi-site feature to manage multiple Shopify stores), you can should can specify the Shopify ENV credentials using an arbitrary namespace suffix. Like:

```env
SHOPIFY_URL=https://your-store.myshopify.com
SHOPIFY_ADMIN_API_ACCESS_TOKEN=xxxxxxxxxxx
SHOPIFY_URL_CANADA=https://your-canadian-store.myshopify.com
SHOPIFY_ADMIN_API_ACCESS_TOKEN_CANADA=xxxxxxxxxxx
```

Then, you can use that namesapce in all of the feed URLs like so:

`https://cms-domain.com/actions/importable-shopify-feeds/feeds/products?store=CANADA`
26 changes: 22 additions & 4 deletions src/services/ShopifyAdminApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bkwld\ImportableShopifyFeeds\services;

use Craft;
use yii\base\Component;
use Exception;
use GuzzleHttp\Client;
Expand All @@ -18,16 +19,33 @@ class ShopifyAdminApi extends Component
*/
public function __construct()
{
list($url, $token) = $this->getCreds();
$this->client = new Client([
'base_uri' => getenv('SHOPIFY_URL').'/admin/api/2022-01/',
'base_uri' => $url.'/admin/api/2022-01/',
'headers' => [
'X-Shopify-Access-Token' =>
getenv('SHOPIFY_ADMIN_API_ACCESS_TOKEN') ?:
getenv('SHOPIFY_API_PASSWORD'),
'X-Shopify-Access-Token' => $token,
],
]);
}

/**
* Get the Shopify creds to use, supporting switching creds based on store
* query param
*/
private function getCreds()
{
$store = Craft::$app->request->getQueryParam('store');
$suffix = $store ? '_'.$store : '';
if (!$url = getenv('SHOPIFY_URL'.$suffix)) {
throw new Exception('Missing SHOPIFY_URL');
}
if (!$token = (getenv('SHOPIFY_ADMIN_API_ACCESS_TOKEN'.$suffix) ?:
getenv('SHOPIFY_API_PASSWORD'.$suffix))) {
throw new Exception('Missing SHOPIFY_ADMIN_API_ACCESS_TOKEN');
}
return [$url, $token];
}

/**
* Execute a GQL query
*/
Expand Down

0 comments on commit 00c807f

Please sign in to comment.