1717use craft \base \Widget ;
1818use craft \helpers \StringHelper ;
1919use craft \db \Query ;
20+ use craft \commerce \Plugin as CommercePlugin ;
2021
2122class 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