10
10
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
11
11
*/
12
12
13
+ /**
14
+ * @method array<string> getOrderIds()
15
+ */
13
16
final class Matomo_Analytics_Block_Script extends Mage_Core_Block_Template
14
17
{
15
18
/**
@@ -40,22 +43,6 @@ public function getOrdersTrackingCode(): string
40
43
foreach ($ collection as $ order ) {
41
44
/** @var Mage_Sales_Model_Order_Item $item */
42
45
foreach ($ order ->getAllVisibleItems () as $ item ) {
43
-
44
- //get category name
45
- $ productId = $ item ->product_id ;
46
-
47
- /** @var Mage_Catalog_Model_Product $product */
48
- $ product = Mage::getModel ('catalog/product ' )->load ($ productId );
49
- $ categoryName = '' ;
50
- $ categoryIds = $ product ->getCategoryIds ();
51
-
52
- if (!empty ($ categoryIds )) {
53
- $ categoryId = $ categoryIds [0 ];
54
- /** @var Mage_Catalog_Model_Category $category */
55
- $ category = Mage::getModel ('catalog/category ' )->load ($ categoryId );
56
- $ categoryName = $ category ->getName ();
57
- }
58
-
59
46
if ($ item ->getQtyOrdered ()) {
60
47
$ qty = number_format ((float )$ item ->getQtyOrdered (), 0 , '. ' , '' );
61
48
} else {
@@ -64,11 +51,10 @@ public function getOrdersTrackingCode(): string
64
51
$ result [] = sprintf ('_paq.push([ \'addEcommerceItem \', \'%s \', \'%s \', \'%s \', %s, %s]); ' ,
65
52
$ this ->jsQuoteEscape ($ item ->getSku ()),
66
53
$ this ->jsQuoteEscape ($ item ->getName ()),
67
- $ categoryName ,
54
+ $ this -> getFirstCategoryName ( $ item -> getProductId ()) ,
68
55
$ item ->getBasePrice (),
69
56
$ qty
70
57
);
71
-
72
58
}
73
59
74
60
if ($ order ->getGrandTotal ()) {
@@ -97,37 +83,22 @@ public function getEcommerceCartUpdate(): string
97
83
98
84
/** @var Mage_Checkout_Model_Cart $cart */
99
85
$ cart = Mage::getModel ('checkout/cart ' );
100
- /** @var Mage_Sales_Model_Quote $quote */
101
86
$ quote = $ cart ->getQuote ();
102
87
103
88
/** @var Mage_Sales_Model_Quote_Item $cartItem */
104
89
foreach ($ quote ->getAllVisibleItems () as $ cartItem ) {
105
-
106
- //get category name
107
- $ productId = $ cartItem ->product_id ;
108
-
109
- /** @var Mage_Catalog_Model_Product $product */
110
- $ product = Mage::getModel ('catalog/product ' )->load ($ productId );
111
- $ categoryName = '' ;
112
- $ categoryIds = $ product ->getCategoryIds ();
113
- if (!empty ($ categoryIds )) {
114
- $ categoryId = $ categoryIds [0 ];
115
- /** @var Mage_Catalog_Model_Category $category */
116
- $ category = Mage::getModel ('catalog/category ' )->load ($ categoryId );
117
- $ categoryName = $ category ->getName ();
118
- }
119
90
$ productName = $ cartItem ->getName ();
120
91
$ productName = str_replace ('" ' , "" , $ productName );
121
92
122
- if ($ cartItem ->getPrice () == 0 || $ cartItem ->getPrice () < 0.00001 ):
93
+ if ($ cartItem ->getPrice () == 0 || $ cartItem ->getPrice () < 0.00001 ) {
123
94
continue ;
124
- endif ;
95
+ }
125
96
126
97
$ result [] = sprintf (
127
98
"_paq.push(['addEcommerceItem', '%s', '%s', '%s', %s, %s]); " ,
128
99
$ this ->jsQuoteEscape ($ cartItem ->getSku ()),
129
100
$ this ->jsQuoteEscape ($ productName ),
130
- $ categoryName ,
101
+ $ this -> getFirstCategoryName ( $ cartItem -> getProductId ()) ,
131
102
$ cartItem ->getPrice (),
132
103
$ cartItem ->getQty ()
133
104
);
@@ -152,21 +123,11 @@ public function getProductPageview(): string
152
123
$ product = Mage::registry ('current_product ' );
153
124
154
125
if ($ product instanceof Mage_Catalog_Model_Product) {
155
- $ categoryName = '' ;
156
- $ categoryIds = $ product ->getCategoryIds ();
157
- if (!empty ($ categoryIds )) {
158
- $ categoryId = $ categoryIds [0 ];
159
- /** @var Mage_Catalog_Model_Category $category */
160
- $ category = Mage::getModel ('catalog/category ' )->load ($ categoryId );
161
- $ categoryName = $ category ->getName ();
162
- }
163
- $ productName = $ product ->getName ();
164
-
165
126
return sprintf (
166
127
"_paq.push(['setEcommerceView', '%s', '%s', '%s', %s]); " ,
167
128
$ this ->jsQuoteEscape ($ product ->getSku ()),
168
- $ this ->jsQuoteEscape ($ productName ),
169
- $ categoryName ,
129
+ $ this ->jsQuoteEscape ($ product -> getName () ),
130
+ $ this -> getFirstCategoryName (( int ) $ product -> getId ()) ,
170
131
$ product ->getFinalPrice ()
171
132
);
172
133
}
@@ -204,17 +165,17 @@ public function getInstallPath(): string
204
165
205
166
public function getSearchResultCount (): int
206
167
{
207
- $ count = 0 ;
208
-
209
- if ($ this ->getRequest ()->getControllerName () === 'result ' ) {
210
- $ queryText = $ this ->helper ('catalogsearch ' )->getQuery ()->getQueryText ();
211
- $ count = (int )$ this ->helper ('catalogsearch ' )->getEngine ()
212
- ->getResultCollection ()
213
- ->addSearchFilter ($ queryText )
214
- ->getSize ();
168
+ if ($ this ->getRequest ()->getControllerName () !== 'result ' ) {
169
+ return 0 ;
215
170
}
216
171
217
- return $ count ;
172
+ $ queryText = $ this ->helper ('catalogsearch ' )->getQuery ()->getQueryText ();
173
+ /** @var \Mage_CatalogSearch_Model_Resource_Fulltext_Engine $engine */
174
+ $ engine = $ this ->helper ('catalogsearch ' )->getEngine ();
175
+
176
+ return $ engine ->getResultCollection ()
177
+ ->addSearchFilter ($ queryText )
178
+ ->getSize ();
218
179
}
219
180
220
181
public function is404 (): bool
@@ -230,4 +191,21 @@ public function _toHtml(): string
230
191
231
192
return parent ::_toHtml ();
232
193
}
194
+
195
+ private function getFirstCategoryName (int $ productId ): string
196
+ {
197
+ /** @var Mage_Catalog_Model_Product $product */
198
+ $ product = Mage::getModel ('catalog/product ' )->load ($ productId );
199
+
200
+ $ categoryIds = $ product ->getCategoryIds ();
201
+ if (empty ($ categoryIds )) {
202
+ return '' ;
203
+ }
204
+
205
+ $ categoryId = $ categoryIds [0 ];
206
+ /** @var Mage_Catalog_Model_Category $category */
207
+ $ category = Mage::getModel ('catalog/category ' )->load ($ categoryId );
208
+
209
+ return $ category ->getName ();
210
+ }
233
211
}
0 commit comments