I have created a Magento (2.4) extension, which creates a custom product listing page in a specific route.
It works well and loads a product listing with all products.
Then I added following plugin with intention to filter the product list by a specific attribute and an value.
With that I could access the before load event. When products collection passed as it is it works fine. Custom product page loads fine.
However, it fails to load after I tried to iterate through products collection the respective custom product page, and actually the entire magento failes to load. It gives HTTP 503 error.
Then I tried print_r($products);
– only the output portion before print_r got printed, rest of the output not sent.
Then I tried var_dump($products);
also it gives HTTP 500 error.
I have enabled developer mode and usually errors are getting displayed. However with above incidents no error is displayed or even logged in to log files.
Below the plugin code, I am pasting chatnotes with my webhost as well. Can someone please guide me to resolve this.
<?php
namespace IwadatCatalogExtPluginCatalogModelResourceModelProduct;
class CollectionPlugin
{
/**
* @param Collection $products
* @param bool $printQuery
* @param bool $logQuery
*/
public function beforeLoad(MagentoCatalogModelResourceModelProductCollection $products, $printQuery = false, $logQuery = false)
{
if (!$products->isLoaded()) {
// you can do your customzation/custom logic with $products object
}
echo 'This gets printed - no error .';
/*
foreach($products as $product) {
echo "<h2>" . $product->getName() . "</h2>";
echo "<h3>" . $product->getSku() . "</h3><hr>";
}*/ // <-- This gives 503 error
//print_r($products);// <-- This gives 503 error
//var_dump($products); // <-- This gives 500 error
echo 'This gets printed - no error . Only when above are commented';
return $products;//[$printQuery, $logQuery];
}
}
Feedback by web host:
We weren’t able to find anything specific. The only thing that we found is that you’re getting these service unavailable errors because the php-fpm processes are getting killed by the linux kernel because they are trying to access a part of the server memory which isn’t supposed to be accessed by them. This doesn’t happen when we try to prevent the website from executing it’s code which means that what ever is causing these segmentation errors is located somewhere in the code of your magento installation.
Well the reason for which there’s no error show is most likely that the kernel kills the process before an error gets thrown.
Thanks and Best Regards
Indunil