20
20
21
21
namespace Meta \Conversion \Block \Pixel ;
22
22
23
+ use Exception ;
24
+ use Magento \Catalog \Helper \Data as CatalogHelper ;
25
+ use Magento \Catalog \Model \Product ;
23
26
use Meta \BusinessExtension \Helper \FBEHelper ;
24
27
use Meta \Conversion \Helper \MagentoDataHelper ;
25
28
use Meta \BusinessExtension \Model \System \Config as SystemConfig ;
@@ -35,12 +38,17 @@ class ViewContent extends Common
35
38
/**
36
39
* @var FBEHelper
37
40
*/
38
- private $ fbeHelper ;
41
+ private FBEHelper $ fbeHelper ;
39
42
40
43
/**
41
44
* @var MagentoDataHelper
42
45
*/
43
- private $ magentoDataHelper ;
46
+ private MagentoDataHelper $ magentoDataHelper ;
47
+
48
+ /**
49
+ * @var CatalogHelper
50
+ */
51
+ private CatalogHelper $ catalogHelper ;
44
52
45
53
/**
46
54
* ViewContent constructor
@@ -51,6 +59,7 @@ class ViewContent extends Common
51
59
* @param SystemConfig $systemConfig
52
60
* @param Escaper $escaper
53
61
* @param CheckoutSession $checkoutSession
62
+ * @param CatalogHelper $catalogHelper
54
63
* @param array $data
55
64
*/
56
65
public function __construct (
@@ -60,6 +69,7 @@ public function __construct(
60
69
SystemConfig $ systemConfig ,
61
70
Escaper $ escaper ,
62
71
CheckoutSession $ checkoutSession ,
72
+ CatalogHelper $ catalogHelper ,
63
73
array $ data = []
64
74
) {
65
75
parent ::__construct (
@@ -73,6 +83,7 @@ public function __construct(
73
83
);
74
84
$ this ->fbeHelper = $ fbeHelper ;
75
85
$ this ->magentoDataHelper = $ magentoDataHelper ;
86
+ $ this ->catalogHelper = $ catalogHelper ;
76
87
}
77
88
78
89
/**
@@ -108,12 +119,16 @@ public function getContentName()
108
119
/**
109
120
* Returns content type
110
121
*
111
- * @return string
122
+ * @return string|null
112
123
*/
113
124
public function getContentType ()
114
125
{
115
- /** @var \Magento\Catalog\Model\ Product $product */
126
+ /** @var Product $product */
116
127
$ product = $ this ->getCurrentProduct ();
128
+ if (!$ product ) {
129
+ return null ;
130
+ }
131
+
117
132
return $ this ->magentoDataHelper ->getContentType ($ product );
118
133
}
119
134
@@ -125,8 +140,12 @@ public function getContentType()
125
140
public function getContentCategory ()
126
141
{
127
142
$ product = $ this ->getCurrentProduct ();
143
+ if (!$ product ) {
144
+ return null ;
145
+ }
146
+
128
147
$ categoryIds = $ product ->getCategoryIds ();
129
- if (count ($ categoryIds ) > 0 ) {
148
+ if (count ($ categoryIds )) {
130
149
$ categoryNames = [];
131
150
$ categoryModel = $ this ->fbeHelper ->getObject (\Magento \Catalog \Model \Category::class);
132
151
foreach ($ categoryIds as $ category_id ) {
@@ -143,7 +162,7 @@ public function getContentCategory()
143
162
/**
144
163
* Return currency
145
164
*
146
- * @return string
165
+ * @return string|null
147
166
*/
148
167
public function getValue ()
149
168
{
@@ -174,17 +193,23 @@ public function getEventToObserveName()
174
193
*/
175
194
public function getProductId ()
176
195
{
177
- return $ this ->getCurrentProduct ()->getId ();
196
+ $ product = $ this ->getCurrentProduct ();
197
+ return $ product ? $ product ->getId () : null ;
178
198
}
179
199
180
200
/**
181
201
* Returns current product
182
202
*
183
- * @return mixed
184
- * @throws \Magento\Framework\Exception\LocalizedException
203
+ * @return Product|null
185
204
*/
186
205
public function getCurrentProduct ()
187
206
{
188
- return $ this ->getLayout ()->getBlock ('product.info ' )->getProduct ();
207
+ try {
208
+ $ block = $ this ->getLayout ()->getBlock ('product.info ' );
209
+ return $ block ? $ block ->getProduct () : $ this ->catalogHelper ->getProduct ();
210
+ } catch (Exception $ e ) {
211
+ $ this ->fbeHelper ->logException ($ e );
212
+ return null ;
213
+ }
189
214
}
190
215
}
0 commit comments