cancel
Showing results for 
Search instead for 
Did you mean: 

How to retrieve all products currently shown on page

SOLVED

How to retrieve all products currently shown on page

Hi, I have a rather simple task and didn't find a working solution.

The task: I need to create a JS dataLayer, which includes an array with all products currently visible on screen - or to be more precise: all products that have an Add-to-cart button on screen.

 

My solution so far only works during the first page render, and afterwards not anymore: I created an Observer for an event:

<event name="catalog_product_collection_load_after">
<observer name="ti_datalayer_product_list" instance="Webtrekk\TagIntegration\Observer\TIDatalayerProductList" />
</event>

 In the corresponding class I do this:

namespace Webtrekk\TagIntegration\Observer;

use Magento\Checkout\Model\Session;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Webtrekk\TagIntegration\Helper\Data;
use Webtrekk\TagIntegration\Model\Data\Product;

class
TIDatalayerProductList implements ObserverInterface
{

/**
* @var Session
*/
protected $_checkoutSession;
/**
* @var Data
*/
protected $_tiHelper;
/**
* @var Product
*/
protected $_product;

/**
* @param Session $checkoutSession
* @param Data $tiHelper
* @param Product $product
*/
public function __construct(Session $checkoutSession, Data $tiHelper, Product $product)
{
$this->_checkoutSession = $checkoutSession;
$this->_tiHelper = $tiHelper;
$this->_product = $product;
}

/**
* @param Observer $observer
*/
public function execute(Observer $observer)
{
if ($this->_tiHelper->isEnabled()) {
$productCollection =$observer->getCollection()->getItems();

$existingData = $this->_checkoutSession->getData('mapp_product_list');
$productListData = $existingData ? $existingData : array();


foreach ($productCollection as $productItem) {
$productListData[] = array(
'productName' => $productItem->getName()
);
}
$this->_checkoutSession->setData('mapp_product_list', $productListData);
}
}
}

 

So basically when the event fires, I write the data into the checkoutSession cache, and in the footer I later call a getDatalayer function that retrives the data from the cache. As I said, it works when loading a page for the first time ever, and I guess after that it is cached because the catalog_product_collection_load_after event actually does not fire anymore (checked with breakpoint & Xdebug).

 

So my question is: is there a better way to retrieve all products on current page? And remember that I cannot use something like 'getCurrentCategory' and then list the products of that category, since there might be pages like the Homepage / Index that has different products from a lot of categories. So what I need is a direct way to retrieve the products or product entity ids that are listed on the current page.

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to retrieve all products currently shown on page

I solved it in a different way. Instead of writing everything in the JS DL, ich write the data I need in session cache, create a controller that outputs the cached data as json. Then on clientside I fetch the data when needed.

View solution in original post

1 REPLY 1

Re: How to retrieve all products currently shown on page

I solved it in a different way. Instead of writing everything in the JS DL, ich write the data I need in session cache, create a controller that outputs the cached data as json. Then on clientside I fetch the data when needed.