Skip to content

Commit 8d1df16

Browse files
authored
Make PDP more stable (#314)
1 parent b100079 commit 8d1df16

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

app/code/Meta/Conversion/Block/Pixel/ViewContent.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
namespace Meta\Conversion\Block\Pixel;
2222

23+
use Exception;
24+
use Magento\Catalog\Helper\Data as CatalogHelper;
25+
use Magento\Catalog\Model\Product;
2326
use Meta\BusinessExtension\Helper\FBEHelper;
2427
use Meta\Conversion\Helper\MagentoDataHelper;
2528
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
@@ -35,12 +38,17 @@ class ViewContent extends Common
3538
/**
3639
* @var FBEHelper
3740
*/
38-
private $fbeHelper;
41+
private FBEHelper $fbeHelper;
3942

4043
/**
4144
* @var MagentoDataHelper
4245
*/
43-
private $magentoDataHelper;
46+
private MagentoDataHelper $magentoDataHelper;
47+
48+
/**
49+
* @var CatalogHelper
50+
*/
51+
private CatalogHelper $catalogHelper;
4452

4553
/**
4654
* ViewContent constructor
@@ -51,6 +59,7 @@ class ViewContent extends Common
5159
* @param SystemConfig $systemConfig
5260
* @param Escaper $escaper
5361
* @param CheckoutSession $checkoutSession
62+
* @param CatalogHelper $catalogHelper
5463
* @param array $data
5564
*/
5665
public function __construct(
@@ -60,6 +69,7 @@ public function __construct(
6069
SystemConfig $systemConfig,
6170
Escaper $escaper,
6271
CheckoutSession $checkoutSession,
72+
CatalogHelper $catalogHelper,
6373
array $data = []
6474
) {
6575
parent::__construct(
@@ -73,6 +83,7 @@ public function __construct(
7383
);
7484
$this->fbeHelper = $fbeHelper;
7585
$this->magentoDataHelper = $magentoDataHelper;
86+
$this->catalogHelper = $catalogHelper;
7687
}
7788

7889
/**
@@ -108,12 +119,16 @@ public function getContentName()
108119
/**
109120
* Returns content type
110121
*
111-
* @return string
122+
* @return string|null
112123
*/
113124
public function getContentType()
114125
{
115-
/** @var \Magento\Catalog\Model\Product $product */
126+
/** @var Product $product */
116127
$product = $this->getCurrentProduct();
128+
if (!$product) {
129+
return null;
130+
}
131+
117132
return $this->magentoDataHelper->getContentType($product);
118133
}
119134

@@ -125,8 +140,12 @@ public function getContentType()
125140
public function getContentCategory()
126141
{
127142
$product = $this->getCurrentProduct();
143+
if (!$product) {
144+
return null;
145+
}
146+
128147
$categoryIds = $product->getCategoryIds();
129-
if (count($categoryIds) > 0) {
148+
if (count($categoryIds)) {
130149
$categoryNames = [];
131150
$categoryModel = $this->fbeHelper->getObject(\Magento\Catalog\Model\Category::class);
132151
foreach ($categoryIds as $category_id) {
@@ -143,7 +162,7 @@ public function getContentCategory()
143162
/**
144163
* Return currency
145164
*
146-
* @return string
165+
* @return string|null
147166
*/
148167
public function getValue()
149168
{
@@ -174,17 +193,23 @@ public function getEventToObserveName()
174193
*/
175194
public function getProductId()
176195
{
177-
return $this->getCurrentProduct()->getId();
196+
$product = $this->getCurrentProduct();
197+
return $product ? $product->getId() : null;
178198
}
179199

180200
/**
181201
* Returns current product
182202
*
183-
* @return mixed
184-
* @throws \Magento\Framework\Exception\LocalizedException
203+
* @return Product|null
185204
*/
186205
public function getCurrentProduct()
187206
{
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+
}
189214
}
190215
}

0 commit comments

Comments
 (0)