Skip to content

Commit

Permalink
Add sub nav item to open ShipStation from CP
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnynotsolucky committed Oct 25, 2018
1 parent 38e0ede commit 263975c
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 106 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A Craft CMS plugin for integrating Craft Commerce with ShipStation",
"homepage": "https://github.com/fostercommerce/shipstation-connect",
"type": "craft-plugin",
"version": "1.0.9",
"version": "1.0.10",
"keywords": ["craft","plugin","shipstation"],
"license": "proprietary",
"support": {
Expand Down
25 changes: 25 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
namespace fostercommerce\shipstationconnect;

use Craft;
use craft\events\RegisterUrlRulesEvent;
use craft\web\UrlManager;
use yii\base\Event;
use yii\base\Exception;

class Plugin extends \craft\base\Plugin
{
public $hasCpSettings = true;
public $hasCpSection = true;
public $schemaVersion = '1.0.1';

public function init()
Expand All @@ -16,6 +20,15 @@ public function init()
$this->setComponents([
'xml' => \fostercommerce\shipstationconnect\services\Xml::class,
]);

Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_CP_URL_RULES,
function (RegisterUrlRulesEvent $event) {
$event->rules['shipstationconnect/settings'] = 'shipstationconnect/settings/index';
$event->rules['shipstationconnect/settings/save'] = 'shipstationconnect/settings/save';
}
);
}

protected function beforeInstall(): bool
Expand All @@ -39,6 +52,18 @@ protected function beforeInstall(): bool
return true;
}

public function getCpNavItem()
{
$item = parent::getCpNavItem();

$item['label'] = Craft::t('shipstationconnect', 'ShipStation Connect');
$item['subnav'] = [
'open' => ['label' => Craft::t('shipstationconnect', 'Dashboard'), 'url' => 'shipstationconnect/open'],
'settings' => ['label' => Craft::t('shipstationconnect', 'Settings'), 'url' => 'shipstationconnect/settings'],
];
return $item;
}

protected function createSettingsModel()
{
return new \fostercommerce\shipstationconnect\models\Settings();
Expand Down
35 changes: 35 additions & 0 deletions src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace fostercommerce\shipstationconnect\controllers;

use Craft;
use craft\web\Controller;
use craft\db\Query;
use fostercommerce\shipstationconnect\Plugin;
use fostercommerce\shipstationconnect\models\Settings;

class SettingsController extends Controller
{

public function actionIndex()
{
return $this->renderTemplate("shipstationconnect/settings/index", [
'settings' => Plugin::getInstance()->settings,
]);
}

public function actionSave()
{
$this->requirePostRequest();
$postData = Craft::$app->getRequest()->getBodyParam('settings');
$settings = new Settings($postData);

if (!$settings->validate() || !Craft::$app->getPlugins()->savePluginSettings(Plugin::getInstance(), $settings->toArray())) {
Craft::$app->getSession()->setError(Craft::t('shipstationconnect', 'Couldn’t save settings.'));
return $this->renderTemplate('shipstationconnect/settings/index', ['settings' => $settings]);
}

Craft::$app->getSession()->setNotice(Craft::t('shipstationconnect', 'Settings saved.'));

return $this->redirectToPostedUrl();
}
}
1 change: 1 addition & 0 deletions src/templates/index.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% redirect 'shipstationconnect/settings' %}
1 change: 1 addition & 0 deletions src/templates/open.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% redirect 'https://ss.shipstation.com/' %}
105 changes: 0 additions & 105 deletions src/templates/settings.html

This file was deleted.

1 change: 1 addition & 0 deletions src/templates/settings.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% redirect 'shipstationconnect/settings' %}
109 changes: 109 additions & 0 deletions src/templates/settings/index.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{% import "_includes/forms" as forms %}
{% extends "_layouts/cp" %}

{% set title = 'Settings'|t('better-logging') %}
{% set selectedSubnavItem = 'settings' %}
{% set fullPageForm = true %}

{% block actionButton %}
<div class="btngroup">
<input type="submit" class="btn submit" value="{{ "Save"|t('shipstationconnect') }}">
</div>
{% endblock %}

{% set content %}
<input type="hidden" name="action" value="shipstationconnect/settings/save">
{{ redirectInput('shipstationconnect/settings') }}
<div>
<h4>Custom Store</h4>
<p>
ShipStation's custom store integration works by requesting orders from ShipStation Connect (shipped and unshipped),
and then by notifying ShipStation Connect when an order has been shipped.
</p>
<p>
Once you've configured ShipStation Connects's settings below, create a <i>custom store</i> in ShipStation.
You'll need to provide ShipStation with:
</p>
<ul style="list-style-type:disc;margin-left:20px;">
<li>Username for ShipStation to retrieve/update orders</li>
<li>Password for ShipStation to retrieve/update orders</li>
<li>XML Page URL</li>
<li>Order Statuses (optional)</li>
</ul>

<h4>Order Statuses</h4>
<p>
If you have defined custom order statuses, you will need to define them in ShipStation accordingly.
By default, Craft Commerce uses "processing" for "Awaiting Shipment Statuses" in ShipStation, and "shipped" for "Shipped Statuses".
Other ShipStation statuses can be left as is, as Commerce doesn't have any corresponding statuses. See below for the statuses currently enabled.
</p>
</div>

<hr />

<h3>Settings</h3>

{{ forms.textField({
label: 'Username'|t('shipstationconnect'),
instructions: 'The username associated with your custom store, not your Craft nor ShipStation usernames.'|t('shipstationconnect'),
id: 'shipstation_username',
name: 'settings[shipstationUsername]',
value: settings.shipstationUsername,
errors: settings.getErrors('shipstationUsername'),
}) }}

{{ forms.textField({
label: 'Password'|t('shipstationconnect'),
instructions: 'The password associated with your custom store, not your Craft nor ShipStation passwords.'|t('shipstationconnect'),
id: 'shipstation_password',
type: 'password',
name: 'settings[shipstationPassword]',
value: settings.shipstationPassword,
errors: settings.getErrors('shipstationPassword'),
}) }}

{{ forms.textField({
label: 'Page Size for Orders'|t('shipstationconnect'),
instructions: 'The number of orders returned for each request from ShipStation. Too many orders at once will take too long to respond, while too few will require many requests.'|t('shipstationconnect'),
id: 'orders_page_size',
name: 'settings[ordersPageSize]',
value: settings.ordersPageSize,
errors: settings.getErrors('ordersPageSize'),
}) }}

{{ forms.textField({
label: 'Order ID Prefix'|t('shipstationconnect'),
instructions: 'Order IDs sent to ShipStation will be integers (eg <code>1234</code>). Optionally add a prefix to the ID to guarantee unique IDs (eg a prefix of <code>commerce_</code> would yield an Order ID of <code>commerce_1234</code>).'|raw|t('shipstationconnect'),
id: 'order_id_prefix',
name: 'settings[orderIdPrefix]',
value: settings.orderIdPrefix,
errors: settings.getErrors('orderIdPrefix'),
}) }}

<div class="field">
<div class="heading">
<label>Commerce Order Statuses</label>
<div class="instructions">
<p>Set your Custom Store's Order Statuses according to your business process and the statuses enabled in Craft Commerce</p>
</div>
</div>
<ul style="list-style-type:disc;margin-left:20px;">
{% for status in craft.commerce.orderStatuses.getAllOrderStatuses %}
<li>{{ status.name }} &mdash; <code>{{ status.handle }}</code></li>
{% endfor %}
</ul>
</div>

<div class="field">
<div class="heading">
<label>XML Page URL</label>
<div class="instructions">
<p>When setting up your ShipStation Custom Store, enter this URL so ShipStation can retrieve and update orders.</p>
</div>
</div>
<div class="input">
<br />
<pre><code>{{ siteUrl }}actions/shipstationconnect/orders/process</code></pre>
</div>
</div>
{% endset %}

0 comments on commit 263975c

Please sign in to comment.