Skip to content

Commit be10524

Browse files
committed
Added Order Status setting to the Top Products Widget.
1 parent 5e030eb commit be10524

File tree

6 files changed

+139
-34
lines changed

6 files changed

+139
-34
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Commerce Widgets Changelog
22

3+
## 2.0.11 - 2019-01-07
4+
### Added
5+
- Added `Order Status` setting to the Top Products Widget.
6+
37
## 2.0.10 - 2019-01-04
48
### Fixed
59
- `groupBy` issue with MySQL 5.7 on some widgets
6-
- Week was showing incorrectly in the Goal widget
10+
- Week was showing incorrectly in the Goal widget
711
- Installing the plugin now populates the cache setting by default
812

913
### Changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "bymayo/commerce-widgets",
33
"description": "Insightful dashboard widgets for your Craft Commerce 2 store.",
44
"type": "craft-plugin",
5-
"version": "2.0.10",
5+
"version": "2.0.11",
66
"keywords": [
77
"craft",
88
"cms",

src/assetbundles/commercewidgets/CommerceWidgetsAsset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function init()
3737

3838
$this->js = [
3939
'js/plugins/Chart.min.js',
40-
'js/CommerceWidgets.js',
40+
'js/CommerceWidgets.js',
4141
];
4242

4343
$this->css = [
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
if (typeof CommerceWidgets === typeof undefined) {
2+
CommerceWidgets = {};
3+
}
4+
5+
/**
6+
* Class CommerceWidgets.OrderStatuses
7+
*/
8+
CommerceWidgets.OrderStatuses = Garnish.Base.extend(
9+
{
10+
init: function(id, settings) {
11+
this.$container = $('#' + id);
12+
this.$menuBtn = $('.menubtn', this.$container);
13+
this.$statusInput = $('.status-input', this.$container);
14+
15+
this.menuBtn = new Garnish.MenuBtn(this.$menuBtn, {
16+
onOptionSelect: $.proxy(this, 'onSelectStatus')
17+
});
18+
19+
var statusId = this.$statusInput.val();
20+
21+
var $currentStatus = $('[data-id="' + statusId + '"]', this.menuBtn.menu.$container);
22+
23+
$currentStatus.trigger('click');
24+
25+
},
26+
27+
onSelectStatus: function(status) {
28+
this.deselectStatus();
29+
30+
var $status = $(status);
31+
$status.addClass('sel');
32+
33+
this.selectedStatus = $status;
34+
35+
this.$statusInput.val($status.data('id'));
36+
37+
// clone selected status item to menu menu
38+
var $label = $('.commerceStatusLabel', $status);
39+
this.$menuBtn.empty();
40+
$label.clone().appendTo(this.$menuBtn);
41+
},
42+
43+
deselectStatus: function() {
44+
if (this.selectedStatus) {
45+
this.selectedStatus.removeClass('sel');
46+
}
47+
}
48+
});

src/templates/widgets/ProductsTop/settings.twig

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,59 @@
22

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

5-
{{
6-
forms.selectField(
7-
{
8-
label: 'Order By',
9-
name: 'orderBy',
10-
value: widget['orderBy'],
11-
options: [
12-
{ label: 'Ordered Total', value: 'totalOrdered' },
13-
{ label: 'Revenue Total', value: 'totalRevenue' }
14-
]
15-
}
16-
)
17-
}}
5+
<div id="{{ id }}">
186

19-
{{
20-
forms.textField(
21-
{
22-
label: 'Limit',
23-
id: 'limit',
24-
name: 'limit',
25-
value: widget['limit']
26-
}
27-
)
28-
}}
7+
<div class="field">
8+
<div class="heading">
9+
<label>Order Status</label>
10+
</div>
11+
<div class="input ltr">
12+
<input type="hidden" class="status-input" name="orderStatusId" value="{{ widget['orderStatusId'] }}">
13+
<a class="btn menubtn">
14+
<span class="status"></span> {{ "All"|t('commerce') }}
15+
</a>
16+
<div class="menu">
17+
<ul class="padded">
18+
<li>
19+
<a>
20+
<span class="commerceStatusLabel">
21+
<span class="status"></span> {{ "All"|t('commerce') }}
22+
</span>
23+
</a>
24+
</li>
25+
{% for status in orderStatuses %}
26+
<li>
27+
<a data-id="{{ status.id }}">{{ status.labelHtml|raw }}</a>
28+
</li>
29+
{% endfor %}
30+
</ul>
31+
</div>
32+
</div>
33+
</div>
34+
35+
{{
36+
forms.selectField(
37+
{
38+
label: 'Order By',
39+
name: 'orderBy',
40+
value: widget['orderBy'],
41+
options: [
42+
{ label: 'Ordered Total', value: 'totalOrdered' },
43+
{ label: 'Revenue Total', value: 'totalRevenue' }
44+
]
45+
}
46+
)
47+
}}
48+
49+
{{
50+
forms.textField(
51+
{
52+
label: 'Limit',
53+
id: 'limit',
54+
name: 'limit',
55+
value: widget['limit']
56+
}
57+
)
58+
}}
59+
60+
</div>

src/widgets/ProductsTop.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use craft\base\Widget;
1818
use craft\helpers\StringHelper;
1919
use craft\db\Query;
20+
use craft\commerce\Plugin as CommercePlugin;
2021

2122
class ProductsTop extends Widget
2223
{
@@ -26,6 +27,7 @@ class ProductsTop extends Widget
2627

2728
public $limit;
2829
public $orderBy;
30+
public $orderStatusId;
2931

3032
// Static Methods
3133
// =========================================================================
@@ -69,10 +71,19 @@ public function getProducts()
6971
->join(
7072
'LEFT JOIN', '{{%commerce_variants}} variants', 'variants.id = purchasables.id'
7173
)
74+
->join(
75+
'LEFT JOIN', '{{%commerce_orders}} orders', 'orders.id = items.orderId'
76+
)
7277
->groupBy(['items.purchasableId'])
7378
->orderBy($this->orderBy . ' desc')
7479
->limit($this->limit);
7580

81+
if($this->orderStatusId != null)
82+
{
83+
$query
84+
->where(['orders.orderStatusId' => $this->orderStatusId]);
85+
}
86+
7687
$result = $query->cache(CommerceWidgets::$plugin->getSettings()->cacheDuration)->all();
7788

7889
return $result;
@@ -95,9 +106,10 @@ public function rules()
95106
$rules,
96107
[
97108
['orderBy', 'string'],
98-
['limit', 'integer'],
109+
[['limit', 'orderStatusId'], 'integer'],
99110
['limit', 'default', 'value' => 5],
100-
['orderBy', 'default', 'value' => 'totalRevenue']
111+
['orderBy', 'default', 'value' => 'totalRevenue'],
112+
['orderStatusId', 'default', 'value' => null]
101113
]
102114
);
103115

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

107119
public function getSettingsHtml()
108120
{
109-
return Craft::$app->getView()->renderTemplate(
110-
'commerce-widgets/widgets/' . StringHelper::basename(get_class($this)) . '/settings',
111-
[
112-
'widget' => $this
113-
]
114-
);
121+
122+
// Credit - craft/vendor/craftcms/commerce/src/widgets/Orders.php
123+
$id = StringHelper::basename(get_class($this)) . '-' . StringHelper::randomString();
124+
$namespaceId = Craft::$app->getView()->namespaceInputId($id);
125+
126+
Craft::$app->getView()->registerJs("new CommerceWidgets.OrderStatuses('" . $namespaceId . "');");
127+
128+
return Craft::$app->getView()->renderTemplate(
129+
'commerce-widgets/widgets/' . StringHelper::basename(get_class($this)) . '/settings',
130+
[
131+
'id' => $id,
132+
'widget' => $this,
133+
'orderStatuses' => CommercePlugin::getInstance()->getOrderStatuses()->getAllOrderStatuses()
134+
]
135+
);
115136
}
116137

117138
public function getBodyHtml()

0 commit comments

Comments
 (0)