Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions Block/Catalog/Product/ViewedProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\Serialize\Serializer\Json;

class ViewedProduct extends Template
{
const PRODUCT_CATEGORY_LIMIT = '50';

protected $_klaviyoScopeSetting;
protected $_registry;
protected $_categoryFactory;
Expand All @@ -23,28 +27,44 @@ class ViewedProduct extends Template
*/
private $imageHelper;

/**
* @var Json
*/
private $json;

/**
* @var CollectionFactory
*/
private $categoryCollectionFactory;

/**
* ViewedProduct constructor.
* @param Context $context
* @param ScopeSetting $klaviyoScopeSetting
* @param Json $json
* @param Registry $registry
* @param CategoryFactory $categoryFactory
* @param CollectionFactory $categoryCollectionFactory
* @param Image $imageHelper
* @param array $data
*/
public function __construct(
Context $context,
ScopeSetting $klaviyoScopeSetting,
Json $json,
Registry $registry,
CategoryFactory $categoryFactory,
CollectionFactory $categoryCollectionFactory,
Image $imageHelper,
array $data = []
) {
parent::__construct($context, $data);
$this->json = $json;
$this->_klaviyoScopeSetting = $klaviyoScopeSetting;
$this->_registry = $registry;
$this->_categoryFactory = $categoryFactory;
$this->imageHelper = $imageHelper;
$this->categoryCollectionFactory = $categoryCollectionFactory;
}

/**
Expand Down Expand Up @@ -90,9 +110,18 @@ public function getProduct()
public function getProductCategories()
{
if (empty($this->categories)) {
foreach ($this->getProduct()->getCategoryIds() as $category_id) {
$category = $category = $this->_categoryFactory->create()->load($category_id);
$this->categories[] = $category->getName();
$categoryIds = $this->getProduct()->getCategoryIds();
$categoryCollection = $this->categoryCollectionFactory->create()
->addAttributeToSelect('name')
->addAttributeToFilter('entity_id', $categoryIds);
$categoryCollection->addIsActiveFilter();
$categoryCollection->setPageSize(self::PRODUCT_CATEGORY_LIMIT);

foreach ($categoryCollection as $category) {
$categoryName = $category->getName();
if (!in_array($categoryName, $this->categories)) {
$this->categories[] = $categoryName;
}
}
}

Expand All @@ -107,7 +136,7 @@ public function getProductCategories()
*/
public function getProductCategoriesAsJson()
{
return json_encode($this->getProductCategories());
return $this->json->serialize($this->getProductCategories());
}

/**
Expand Down Expand Up @@ -191,7 +220,7 @@ public function getViewedProductJson()
$result['ImageURL'] = $this->getProductImage();
}

return json_encode($result);
return $this->json->serialize($result);
}

/**
Expand All @@ -215,6 +244,6 @@ public function getViewedItemJson()
$result['ImageURL'] = $this->getProductImage();
}

return json_encode($result);
return $this->json->serialize($result);
}
}