1

How can I echo the SKU list of the items in the search results page, but in the header of Magento? Currently using M1. I'm using the code below to echo the list of SKUs of a specific category page to the header, but I don't know how to get the list of SKUs in the search results page.

if (Mage::registry('current_category')) 
{

$category_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->load($category_id);
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addCategoryFilter($category)
    ->setOrder('price', 'ASC')
    ->load();

$skuArray = array();
foreach ($products as $item) {
                        $skuArray[] = "\"" . $item->getSku() . "\"";
} 

echo Mage::registry('current_category')->getID();

echo implode( ', ', $skuArray);

I tried the solution in this link, but it doesn't seem to work: How to get Search result Product collection outside result.phtml

Any suggestion would be greatly appreciated. :D

  • So what is the issue have you got an error showing when you try that solution mentioned in your question? – harri Aug 12 '17 at 15:59
  • I got no result. Just blank actually. :D – jehzlau Aug 12 '17 at 16:04
  • Is this the sku or ID that you want? – harri Aug 12 '17 at 16:24
  • I just need the SKU. :) I need something like this in my header or head.phtml: ListOfItemsinMagentoSearchResults = ["SKU1","SKU2", "SKU3"]; – jehzlau Aug 12 '17 at 16:25
  • Ok i just put a bit of code like yours but based on the loaded product collection in search see my answer – harri Aug 12 '17 at 16:34
1

Place this code within header.phtml or head.phtml

if($listBlock = $this->getLayout()->getBlock('search_result_list')) {
  $productCollection = $listBlock->getLoadedProductCollection();

  $skuArray = array();
  foreach ($productCollection as $item) {
     $skuArray[] = "\"" . $item->getSku() . "\"";
  } 

  echo 'ListOfItemsinMagentoSearchResults = [' . implode( ', ', $skuArray) .']';
}
  • You... are... a GENIUS! It works as I wanted. Thank you so much! :D – jehzlau Aug 12 '17 at 17:03
  • No worries! Glad you got it sorted! – harri Aug 12 '17 at 17:08
  • You actually sorted it out. Hahaha! I actually tried the search_result_list block because I found it somewhere and it gets the number of results when using productCollection->getSize, so I tried getSku, but I forgot to include the productCollection in the foreach loop. That's why it didn't work Hahaha! Thanks again! :D – jehzlau Aug 12 '17 at 17:15
  • haha yeah i saw the same thing much simpler than that other solution in your question aswell – harri Aug 12 '17 at 17:16
  • Yeah. The one I linked is too long. hahaha! I think this is the one that we saw: magento.stackexchange.com/questions/89223/… but you figured out how to make it work for SKUs. Haha! :D I wish I could give you more upvotes. :D Thanks again Harri! :) – jehzlau Aug 12 '17 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.