Skip to content

Commit

Permalink
Added Order Status setting to the Top Products Widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
bymayo committed Jan 7, 2019
1 parent 5e030eb commit be10524
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 34 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Commerce Widgets Changelog

## 2.0.11 - 2019-01-07
### Added
- Added `Order Status` setting to the Top Products Widget.

## 2.0.10 - 2019-01-04
### Fixed
- `groupBy` issue with MySQL 5.7 on some widgets
- Week was showing incorrectly in the Goal widget
- Week was showing incorrectly in the Goal widget
- Installing the plugin now populates the cache setting by default

### Changed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "bymayo/commerce-widgets",
"description": "Insightful dashboard widgets for your Craft Commerce 2 store.",
"type": "craft-plugin",
"version": "2.0.10",
"version": "2.0.11",
"keywords": [
"craft",
"cms",
Expand Down
2 changes: 1 addition & 1 deletion src/assetbundles/commercewidgets/CommerceWidgetsAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function init()

$this->js = [
'js/plugins/Chart.min.js',
'js/CommerceWidgets.js',
'js/CommerceWidgets.js',
];

$this->css = [
Expand Down
48 changes: 48 additions & 0 deletions src/assetbundles/commercewidgets/dist/js/CommerceWidgets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
if (typeof CommerceWidgets === typeof undefined) {
CommerceWidgets = {};
}

/**
* Class CommerceWidgets.OrderStatuses
*/
CommerceWidgets.OrderStatuses = Garnish.Base.extend(
{
init: function(id, settings) {
this.$container = $('#' + id);
this.$menuBtn = $('.menubtn', this.$container);
this.$statusInput = $('.status-input', this.$container);

this.menuBtn = new Garnish.MenuBtn(this.$menuBtn, {
onOptionSelect: $.proxy(this, 'onSelectStatus')
});

var statusId = this.$statusInput.val();

var $currentStatus = $('[data-id="' + statusId + '"]', this.menuBtn.menu.$container);

$currentStatus.trigger('click');

},

onSelectStatus: function(status) {
this.deselectStatus();

var $status = $(status);
$status.addClass('sel');

this.selectedStatus = $status;

this.$statusInput.val($status.data('id'));

// clone selected status item to menu menu
var $label = $('.commerceStatusLabel', $status);
this.$menuBtn.empty();
$label.clone().appendTo(this.$menuBtn);
},

deselectStatus: function() {
if (this.selectedStatus) {
this.selectedStatus.removeClass('sel');
}
}
});
78 changes: 55 additions & 23 deletions src/templates/widgets/ProductsTop/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,59 @@

{% do view.registerAssetBundle("bymayo\\commercewidgets\\assetbundles\\commercewidgets\\CommerceWidgetsAsset") %}

{{
forms.selectField(
{
label: 'Order By',
name: 'orderBy',
value: widget['orderBy'],
options: [
{ label: 'Ordered Total', value: 'totalOrdered' },
{ label: 'Revenue Total', value: 'totalRevenue' }
]
}
)
}}
<div id="{{ id }}">

{{
forms.textField(
{
label: 'Limit',
id: 'limit',
name: 'limit',
value: widget['limit']
}
)
}}
<div class="field">
<div class="heading">
<label>Order Status</label>
</div>
<div class="input ltr">
<input type="hidden" class="status-input" name="orderStatusId" value="{{ widget['orderStatusId'] }}">
<a class="btn menubtn">
<span class="status"></span> {{ "All"|t('commerce') }}
</a>
<div class="menu">
<ul class="padded">
<li>
<a>
<span class="commerceStatusLabel">
<span class="status"></span> {{ "All"|t('commerce') }}
</span>
</a>
</li>
{% for status in orderStatuses %}
<li>
<a data-id="{{ status.id }}">{{ status.labelHtml|raw }}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>

{{
forms.selectField(
{
label: 'Order By',
name: 'orderBy',
value: widget['orderBy'],
options: [
{ label: 'Ordered Total', value: 'totalOrdered' },
{ label: 'Revenue Total', value: 'totalRevenue' }
]
}
)
}}

{{
forms.textField(
{
label: 'Limit',
id: 'limit',
name: 'limit',
value: widget['limit']
}
)
}}

</div>
37 changes: 29 additions & 8 deletions src/widgets/ProductsTop.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use craft\base\Widget;
use craft\helpers\StringHelper;
use craft\db\Query;
use craft\commerce\Plugin as CommercePlugin;

class ProductsTop extends Widget
{
Expand All @@ -26,6 +27,7 @@ class ProductsTop extends Widget

public $limit;
public $orderBy;
public $orderStatusId;

// Static Methods
// =========================================================================
Expand Down Expand Up @@ -69,10 +71,19 @@ public function getProducts()
->join(
'LEFT JOIN', '{{%commerce_variants}} variants', 'variants.id = purchasables.id'
)
->join(
'LEFT JOIN', '{{%commerce_orders}} orders', 'orders.id = items.orderId'
)
->groupBy(['items.purchasableId'])
->orderBy($this->orderBy . ' desc')
->limit($this->limit);

if($this->orderStatusId != null)
{
$query
->where(['orders.orderStatusId' => $this->orderStatusId]);
}

$result = $query->cache(CommerceWidgets::$plugin->getSettings()->cacheDuration)->all();

return $result;
Expand All @@ -95,9 +106,10 @@ public function rules()
$rules,
[
['orderBy', 'string'],
['limit', 'integer'],
[['limit', 'orderStatusId'], 'integer'],
['limit', 'default', 'value' => 5],
['orderBy', 'default', 'value' => 'totalRevenue']
['orderBy', 'default', 'value' => 'totalRevenue'],
['orderStatusId', 'default', 'value' => null]
]
);

Expand All @@ -106,12 +118,21 @@ public function rules()

public function getSettingsHtml()
{
return Craft::$app->getView()->renderTemplate(
'commerce-widgets/widgets/' . StringHelper::basename(get_class($this)) . '/settings',
[
'widget' => $this
]
);

// Credit - craft/vendor/craftcms/commerce/src/widgets/Orders.php
$id = StringHelper::basename(get_class($this)) . '-' . StringHelper::randomString();
$namespaceId = Craft::$app->getView()->namespaceInputId($id);

Craft::$app->getView()->registerJs("new CommerceWidgets.OrderStatuses('" . $namespaceId . "');");

return Craft::$app->getView()->renderTemplate(
'commerce-widgets/widgets/' . StringHelper::basename(get_class($this)) . '/settings',
[
'id' => $id,
'widget' => $this,
'orderStatuses' => CommercePlugin::getInstance()->getOrderStatuses()->getAllOrderStatuses()
]
);
}

public function getBodyHtml()
Expand Down

1 comment on commit be10524

@bymayo
Copy link
Owner Author

@bymayo bymayo commented on be10524 Jan 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#15

Please sign in to comment.