Skip to content

Commit

Permalink
Added a schema for products (#100)
Browse files Browse the repository at this point in the history
* Added a schema for products
* schema now uses the currency code set in shopify
  - stored in site config
  - updated automatically when a fetch occurs
  • Loading branch information
mak001 committed Jul 23, 2021
1 parent 280eab2 commit a94cef6
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
6 changes: 6 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Dynamic\Shopify\Page\ShopifyProduct:
- LittleGiant\CatalogManager\Extensions\CatalogPageExtension
parent_classes:
- Dynamic\Shopify\Page\ShopifyCollection
active_schema:
- Dynamic\Shopify\SEO\ProductSchemaBuilder
sort_column: false
automatic_live_sort: false

Expand All @@ -17,6 +19,10 @@ PageController:
extensions:
- Dynamic\Shopify\Extension\ShopifyExtension

SilverStripe\SiteConfig\SiteConfig:
extensions:
- Dynamic\Shopify\Extension\ShopifySiteConfigExtension

SilverStripe\Core\Injector\Injector:
SilverStripe\Security\MemberAuthenticator\LoginHandler:
class: Dynamic\Shopify\Extension\ShopifyLoginHandler
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"littlegiant/silverstripe-catalogmanager": "^5.2",
"silverstripe/recipe-cms": "^4.5",
"symbiote/silverstripe-gridfieldextensions": "^3.0",
"osiset/basic-shopify-api": "^10.0"
"osiset/basic-shopify-api": "^10.0",
"bramdeleeuw/silverstripe-schema": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
Expand Down
31 changes: 31 additions & 0 deletions src/Client/ShopifyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dynamic\Shopify\Client;

use Dynamic\Shopify\Extension\ShopifySiteConfigExtension;
use Exception;
use GuzzleHttp\Promise\Promise;
use Osiset\BasicShopifyAPI\BasicShopifyAPI;
Expand All @@ -12,6 +13,7 @@
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Dev\Debug;
use SilverStripe\ORM\ArrayList;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\View\ArrayData;

/**
Expand Down Expand Up @@ -126,6 +128,7 @@ protected function setClient()

$this->client = $client;

$this->updateLocalCache();
return $this;
}

Expand Down Expand Up @@ -153,6 +156,34 @@ public static function get_domain()
return $domain;
}

/**
* Updates locally stored config options set in shopify
*/
protected function updateLocalCache()
{
/** @var SiteConfig|ShopifySiteConfigExtension $config */
$config = SiteConfig::current_site_config();
$config->ShopCurrencyCode = $this->currencyCode();

if ($config->isChanged()) {
$config->write();
}
}

/**
* Gets the shop's currency code
* @return array|Promise
* @throws Exception
*/
public function currencyCode()
{
$result = $this->getClient()->graph('query {shop{currencyCode}}');
if ($result && $result['body']) {
return $result['body']->data->shop->currencyCode;
}
return '';
}

/**
* @param array $options
* @return array|Promise
Expand Down
21 changes: 21 additions & 0 deletions src/Extension/ShopifySiteConfigExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Dynamic\Shopify\Extension;

use SilverStripe\ORM\DataExtension;

/**
* Class ShopifySiteConfigExtension
* @package Dynamic\Shopify\Extension
*
* @property string ShopCurrencyCode
*/
class ShopifySiteConfigExtension extends DataExtension
{
/**
* @var string[]
*/
private static $db = [
'ShopCurrencyCode' => 'Varchar',
];
}
3 changes: 3 additions & 0 deletions src/Page/ShopifyProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
use SilverStripe\ORM\FieldType\DBCurrency;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\HasManyList;
use SilverStripe\View\Requirements;

/**
* Class ShopifyProduct
* @package Dynamic\Shopify\Page
*
* @property string ShopifyID
*
* @method HasManyList|ShopifyFile[] Files
*/
class ShopifyProduct extends \Page
{
Expand Down
46 changes: 46 additions & 0 deletions src/SEO/ProductSchemaBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Dynamic\Shopify\SEO;

use Broarm\Schema\Builder\SchemaBuilder;
use Broarm\Schema\Type\OfferSchema;
use Broarm\Schema\Type\ProductSchema;
use Dynamic\Shopify\Model\ShopifyFile;
use Dynamic\Shopify\Page\ShopifyProduct;
use SilverStripe\SiteConfig\SiteConfig;

/**
* Class ProductSchemaBuilder
* @package Dynamic\Shopify\SEO
*/
class ProductSchemaBuilder extends SchemaBuilder
{

/**
* @inheritDoc
*/
public function getSchema($page)
{
/** @var ShopifyProduct $page */
$images = [];
foreach ($page->Files() as $file) {
if ($file->Type === ShopifyFile::IMAGE) {
$images[] = $file->getURL();
}
}

return new ProductSchema(
$page->Title,
$page->Content,
new OfferSchema(
number_format($page->getPrice()->getValue(), 2),
SiteConfig::current_site_config()->ShopCurrencyCode,
$page->ProductActive ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock'
),
$page->getSKU(),
null,
null,
$images
);
}
}

0 comments on commit a94cef6

Please sign in to comment.