Skip to content

Commit

Permalink
Merge branch 'feature/async-sync-flag' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Sep 22, 2023
2 parents cd90f05 + 2e6ac37 commit e84e923
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
23 changes: 22 additions & 1 deletion src/console/controllers/SyncController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,32 @@
*/
class SyncController extends Controller
{
/** @var string $defaultAction */
public $defaultAction = 'products';

/**
* @var bool Whether to sync product and other associated data in the queue.
* @since 3.3.0
*/
public bool $async = true;

/**
* @inheritdoc
*/
public function options($actionID): array
{
$options = parent::options($actionID);
$options[] = 'async';
return $options;
}

/**
* Sync all Shopify data.
*/
public function actionAll()
{
$this->_syncProducts();
return ExitCode::OK;
}

/**
Expand All @@ -39,7 +60,7 @@ public function actionProducts(): int
private function _syncProducts(): void
{
$this->stdout('Syncing Shopify products…' . PHP_EOL . PHP_EOL, Console::FG_GREEN);
Plugin::getInstance()->getProducts()->syncAllProducts();
Plugin::getInstance()->getProducts()->syncAllProducts($this->async);
$this->stdout('Finished' . PHP_EOL . PHP_EOL, Console::FG_GREEN);
}
}
6 changes: 4 additions & 2 deletions src/controllers/ProductsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ public function actionProductIndex(): Response
*/
public function actionSync(): Response
{
Plugin::getInstance()->getProducts()->syncAllProducts();
return $this->asSuccess(Craft::t('shopify','Products successfully synced'));
$async = (bool)Craft::$app->getRequest()->getParam('async', false);

Plugin::getInstance()->getProducts()->syncAllProducts($async);
return $this->asSuccess(Craft::t('shopify', 'Products successfully synced'));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/elements/db/ProductQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ protected function beforePrepare(): bool
}

if (isset($this->productType)) {
$this->subQuery->andWhere(Db::parseParam(['shopify_productdata.productType' => $this->productType]));
$this->subQuery->andWhere(Db::parseParam('shopify_productdata.productType', $this->productType));
}

if (isset($this->publishedScope)) {
$this->subQuery->andWhere(Db::parseParam(['shopify_productdata.publishedScope' => $this->publishedScope]));
$this->subQuery->andWhere(Db::parseParam('shopify_productdata.publishedScope', $this->publishedScope));
}

if (isset($this->shopifyStatus)) {
$this->subQuery->andWhere(Db::parseParam('shopify_productdata.shopifyStatus', $this->shopifyStatus));
}
Expand Down
25 changes: 15 additions & 10 deletions src/services/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,24 @@ class Products extends Component
* @throws \Throwable
* @throws \yii\base\InvalidConfigException
*/
public function syncAllProducts(): void
public function syncAllProducts(bool $asynchronous = true): void
{
$api = Plugin::getInstance()->getApi();
$products = $api->getAllProducts();

foreach ($products as $product) {
$this->createOrUpdateProduct($product);
Craft::$app->getQueue()->push(new UpdateProductMetadata([
'description' => Craft::t('shopify', 'Updating product metadata for “{title}”', [
'title' => $product->title,
]),
'shopifyProductId' => $product->id,
]));
if ($asynchronous) {
$this->createOrUpdateProduct($product);
Craft::$app->getQueue()->push(new UpdateProductMetadata([
'description' => Craft::t('shopify', 'Updating product metafields for “{title}”', [
'title' => $product->title,
]),
'shopifyProductId' => $product->id,
]));
} else {
$metaFields = $api->getMetafieldsByProductId($product->id);
$this->createOrUpdateProduct($product, $metaFields);
}
}

// Remove any products that are no longer in Shopify just in case.
Expand All @@ -92,9 +97,9 @@ public function syncProductByShopifyId($id): void
$api = Plugin::getInstance()->getApi();

$product = $api->getProductByShopifyId($id);
$metafields = $api->getMetafieldsByProductId($id);
$metaFields = $api->getMetafieldsByProductId($id);

$this->createOrUpdateProduct($product, $metafields);
$this->createOrUpdateProduct($product, $metaFields);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/templates/utilities/_sync.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
<h2>{{ "Shopify Sync"|t('shopify') }}</h2>
{% import "_includes/forms" as forms %}

<form method="POST">
{{ actionInput('') }}
{{ csrfInput() }}

{{ forms.lightswitchField({
label: "Use queue for syncing related objects like meta fields."|t('shopify'),
warning: 'If disabled timeouts may occur, or API rate limits may be hit.'|t('commerce'),
id: 'async',
name: 'async',
value: 1,
on: true
}) }}

<a class="btn submit formsubmit" data-icon="refresh"
data-action="shopify/products/sync"
data-confirm="{{ 'Are you sure you want run a complete sync of all products?'|t('shopify') }}">
Expand Down

0 comments on commit e84e923

Please sign in to comment.