Skip to content
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

Multi-Store #46

Draft
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Draft
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
34 changes: 29 additions & 5 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@
use Craft;
use craft\base\Model;
use craft\base\Plugin as BasePlugin;
use craft\commerce\services\Gateways;
use craft\commerce\services\ProductTypes;
use craft\events\DeleteSiteEvent;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\helpers\UrlHelper;
use craft\services\Elements;
use craft\services\Fields;
use craft\services\Sites;
use craft\services\Utilities;
use craft\shopify\elements\Product;
use craft\shopify\fields\Products as ProductsField;
use craft\shopify\handlers\Product as ProductHandler;
use craft\shopify\models\Settings;
use craft\shopify\services\Api;
use craft\shopify\services\Products;
use craft\shopify\services\Store;
use craft\shopify\services\Stores;
use craft\shopify\utilities\Sync;
use craft\shopify\web\twig\CraftVariableBehavior;
use craft\web\twig\variables\CraftVariable;
Expand All @@ -51,7 +55,7 @@ class Plugin extends BasePlugin
/**
* @var string
*/
public string $schemaVersion = '4.0.4'; // For some reason the 2.2+ version of the plugin was at 4.0 schema version
public string $schemaVersion = '4.1.0'; // For some reason the 2.2+ version of the plugin was at 4.0 schema version

/**
* @inheritdoc
Expand All @@ -72,11 +76,30 @@ public static function config(): array
'components' => [
'api' => ['class' => Api::class],
'products' => ['class' => Products::class],
'store' => ['class' => Store::class],
'store' => ['class' => Stores::class],
],
];
}

/**
* Register Shopify’s project config event listeners
*/
private function _registerProjectConfigEventListeners(): void
{
$projectConfigService = Craft::$app->getProjectConfig();

$storesService = $this->getStores();
$projectConfigService->onAdd(Stores::CONFIG_STORES_KEY . '.{uid}', [$storesService, 'handleChangedProductType'])
->onUpdate(Stores::CONFIG_STORES_KEY . '.{uid}', [$storesService, 'handleChangedProductType'])
->onRemove(Stores::CONFIG_STORES_KEY . '.{uid}', [$storesService, 'handleDeletedProductType']);

Event::on(Sites::class, Sites::EVENT_AFTER_DELETE_SITE, function(DeleteSiteEvent $event) use ($productTypeService) {
if (!Craft::$app->getProjectConfig()->getIsApplyingExternalChanges()) {
$productTypeService->pruneDeletedSite($event);
}
});
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -104,6 +127,7 @@ public function init()
$this->_registerUtilityTypes();
$this->_registerFieldTypes();
$this->_registerVariables();
$this->_registerProjectConfigEventListeners();

if (!$request->getIsConsoleRequest()) {
if ($request->getIsCpRequest()) {
Expand Down Expand Up @@ -146,11 +170,11 @@ public function getProducts(): Products
/**
* Returns the API service
*
* @return Store The Store service
* @return Stores The Store service
* @throws InvalidConfigException
* @since 3.0
*/
public function getStore(): Store
public function getStore(): Stores
{
return $this->get('store');
}
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public function actionSaveSettings(): ?Response

$fieldLayout = Craft::$app->getFields()->assembleLayoutFromPost();
$fieldLayout->type = Product::class;
Craft::$app->fields->saveLayout($fieldLayout);
if(Craft::$app->fields->saveLayout($fieldLayout)){

};

$pluginSettings->setProductFieldLayout($fieldLayout);

Expand Down
96 changes: 96 additions & 0 deletions src/controllers/StoresController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\shopify\controllers;

use Craft;
use craft\shopify\elements\Product;
use craft\shopify\models\Settings;
use craft\shopify\Plugin;
use craft\web\Controller;
use craft\web\Response;

/**
* The StoresController handles modifying and saving the general settings.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0
*/
class StoresController extends Controller
{

public function actionIndex(){
$stores = Plugin::getInstance()->getStores()->getAllStores();
}

/**
* Display a form to allow an administrator to update plugin settings.
*
* @return Response
*/
public function actionIndex(?Settings $settings = null): Response
{
if ($settings == null) {
$settings = Plugin::getInstance()->getSettings();
}

$tabs = [
'apiConnection' => [
'label' => Craft::t('shopify', 'API Connection'),
'url' => '#api',
],
'products' => [
'label' => Craft::t('shopify', 'Products'),
'url' => '#products',
],
];

return $this->renderTemplate('shopify/settings/index', compact('settings', 'tabs'));
}

/**
* Save the settings.
*
* @return ?Response
*/
public function actionSaveSettings(): ?Response
{
$settings = Craft::$app->getRequest()->getParam('settings');
$plugin = Plugin::getInstance();
/** @var Settings $pluginSettings */
$pluginSettings = $plugin->getSettings();

// Remove from editable table namespace
$settings['uriFormat'] = $settings['routing']['uriFormat'];
$settings['template'] = $settings['routing']['template'];
unset($settings['routing']);

$settingsSuccess = Craft::$app->getPlugins()->savePluginSettings($plugin, $settings);

$fieldLayout = Craft::$app->getFields()->assembleLayoutFromPost();
$fieldLayout->type = Product::class;
if(Craft::$app->fields->saveLayout($fieldLayout)){

};

$pluginSettings->setProductFieldLayout($fieldLayout);

if (!$settingsSuccess) {
return $this->asModelFailure(
$pluginSettings,
Craft::t('shopify', 'Couldn’t save settings.'),
'settings',
);
}

return $this->asModelSuccess(
$pluginSettings,
Craft::t('shopify', 'Settings saved.'),
'settings',
);
}
}
1 change: 1 addition & 0 deletions src/db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ abstract class Table
{
public const PRODUCTDATA = '{{%shopify_productdata}}';
public const PRODUCTS = '{{%shopify_products}}';
public const STORES = '{{%shopify_stores}}';
}
30 changes: 30 additions & 0 deletions src/events/StoreEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\shopify\events;

use craft\shopify\models\Store;
use yii\base\Event;

/**
* Class StoreEvent
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.1
*/
class StoreEvent extends Event
{
/**
* @var Store The store
*/
public Store $store;

/**
* @var bool Whether the store is brand new.
*/
public bool $isNew = false;
}
21 changes: 21 additions & 0 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function createTables(): void
$this->archiveTableIfExists(Table::PRODUCTS);
$this->createTable(Table::PRODUCTS, [
'id' => $this->integer()->notNull(),
'storeId' => $this->string()->notNull(),
'shopifyId' => $this->string(),
'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->notNull(),
Expand Down Expand Up @@ -66,6 +67,22 @@ public function createTables(): void
'uid' => $this->string(),
'PRIMARY KEY(shopifyId)',
]);

$this->archiveTableIfExists(Table::STORES);
$this->createTable(Table::STORES, [
'id' => $this->primaryKey(),
'name' => $this->string(),
'hostName' => $this->string(),
'apiKey' => $this->string(),
'apiSecretKey' => $this->string(),
'accessToken' => $this->string(),
'uriFormat' => $this->string(),
'template' => $this->string(),
'productFieldLayoutId' => $this->integer(),
'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->notNull(),
'uid' => $this->string(),
]);
}

/**
Expand All @@ -74,6 +91,8 @@ public function createTables(): void
public function createIndexes(): void
{
$this->createIndex(null, Table::PRODUCTDATA, ['shopifyId'], true);
$this->createIndex(null, Table::STORES, ['id'], true);
$this->createIndex(null, Table::STORES, ['productFieldLayoutId'], true);
}

/**
Expand All @@ -83,6 +102,8 @@ public function addForeignKeys(): void
{
$this->addForeignKey(null, Table::PRODUCTS, ['shopifyId'], Table::PRODUCTDATA, ['shopifyId'], 'CASCADE', 'CASCADE');
$this->addForeignKey(null, Table::PRODUCTS, ['id'], CraftTable::ELEMENTS, ['id'], 'CASCADE', 'CASCADE');
$this->addForeignKey(null, Table::PRODUCTS, ['storeId'], Table::STORES, ['id'], 'CASCADE', 'CASCADE');
$this->addForeignKey(null, Table::STORES, ['productFieldLayoutId'], '{{%fieldlayouts}}', ['id'], 'SET NULL');
}

/**
Expand Down
85 changes: 85 additions & 0 deletions src/migrations/m221118_073752_multi_store.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace craft\shopify\migrations;

use Craft;
use craft\db\Migration;
use craft\helpers\ArrayHelper;
use craft\shopify\db\Table;
use craft\shopify\elements\Product;

/**
* m221118_073752_multi_store migration.
*/
class m221118_073752_multi_store extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
$projectConfig = Craft::$app->getProjectConfig();
$schemaVersion = $projectConfig->get('plugins.shopify.schemaVersion', true);

// create store table
if (!$this->db->tableExists('{{%shopify_stores}}')) {
$this->createTable(Table::STORES, [
'id' => $this->primaryKey(),
'name' => $this->string(),
'hostName' => $this->string(),
'apiKey' => $this->string(),
'apiSecretKey' => $this->string(),
'accessToken' => $this->string(),
'uriFormat' => $this->string(),
'template' => $this->string(),
'productFieldLayoutId' => $this->integer(),
'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->notNull(),
'uid' => $this->string(),
]);
}

$currentFieldLayout = Craft::$app->getFields()->getLayoutByType(Product::class);

$this->insert('{{%shopify_stores', [
'name' => 'Default Store',
'hostName' => Craft::$app->getProjectConfig()->get('plugins.shopify.settings.hostName', true),
'accessToken' => Craft::$app->getProjectConfig()->get('plugin.shopify.settings.accessToken', true),
'apiKey' => Craft::$app->getProjectConfig()->get('plugin.shopify.settings.apiKey', true),
'apiSecretKey' => Craft::$app->getProjectConfig()->get('plugin.shopify.settings.apiSecretKey', true),
'template' => Craft::$app->getProjectConfig()->get('plugin.shopify.settings.template', true),
'uriFormat' => Craft::$app->getProjectConfig()->get('plugin.shopify.settings.uriFormat', true),
'productFieldLayoutId' => $currentFieldLayout?->id,
]);

$migratableSettings = [];
// If this is the first time running this migration
if (version_compare($schemaVersion, '4.1.0', '<')) {
$migratableSettings = $projectConfig->get('plugins.shopify.settings');
if ($migratableSettings) {
$projectConfig->set('plugins.shopify.settings', []);
$projectConfig->set('plugins.shopify.stores', [
'default' => $migratableSettings,
]);
}

}else{
$migratableSettings = $projectConfig->get('shopify.settings');
}

if (!$this->db->columnExists('{{%shopify_products}}', 'storeId')) {
$this->addColumn('{{%shopify_products}}', 'storeId', $this->string()->after('id'));
}

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
echo "m221118_073752_multi_store cannot be reverted.\n";
return false;
}
}
Loading