0

Generally when we hit http://127.0.0.1/magento2/rss/feed/index/type/new_products/ Feed shows data in XML format like this :

<rss xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">
   <channel>
   <language>en_US</language>
   <title>New Products from Main Website Store</title>
   <description>New Products from Main Website Store</description>
   <generator>Laminas_Feed_Writer 2 (https://getlaminas.org)</generator>
   <link>http://127.0.0.1/magento2/rss/feed/index/store_id/1/type/new_products/</link>
   <item>
     <title>Simple Door</title>
     <description>
        <![CDATA[ <table><tr> <td><a href="http://127.0.0.1/magento2/simple-door.html"> 
      <img src="http://127.0.0.1/magento2/pub/static/version1696945400/frontend/Magento/luma/en_US/Magento_Catalog/images/product/placeholder/thumbnail.jpg" border="0" align="left" height="75" width="75"></a></td> <td style="text-decoration:none;"> <div class="price-box price-final_price" data-role="priceBox" data-product-id="45" data-price-box="product-id-45"> <span class="price-container price-final_price&#x20;tax&#x20;weee" > <span id="product-price-45" data-price-amount="0" data-price-type="finalPrice" class="price-wrapper " ><span class="price">$0.00</span></span> </span> </div></td> </tr></table> ]]>
    </description>
    <link>http://127.0.0.1/magento2/simple-door.html</link>
    <guid>http://127.0.0.1/magento2/simple-door.html</guid>
    <slash:comments>0</slash:comments>
    </item>
 </channel>
</rss>

Now, my query is, I want to add new tag weight and sku after description tag. So how can I do that?

1 Answer 1

0

Here is a lead for what you need to do.

In your case, you want to update the "new_products" feed.

Add the attributes to the RSS "New Products" collection

The collection is loaded in this method \Magento\Catalog\Model\Rss\Product\NewProducts::getProductsCollection. As it returns the collection still unloaded, you can create an after plugin on it in order to add your attributes to the "attributes to Select". (Official doc about after plugin).

Use those attributes in the RSS feed

This collection is used in the following method : \Magento\Catalog\Block\Rss\Product\NewProducts::getRssData.

You can see at the end of the foreach, that Magento hard coded the information to show (title, link and description). To add your own additional attributes, I see 2 choices :

  1. Override the method using a preference, so you can add your own data to the array (Official doc about method overriding).
  2. Create an after plugin on the method, in which you can loop again on the product collection, to add your additional attributes to the array.

Overriding a class is generally discouraged in favor of plugins, but in this case, it might be a little tricky to match the existing data in the array to match the current products your looping on.

In that case, as the plugin needs you to perform another loop on the product collection, this means the collection is looped on twice.

I will let you analyze this information according to your case (how many products for example), and choose the methods which suits you best.

3
  • Yes I tried this.. I added SKU and weight on that file but when XML is generated it shows only above tags. And Skua weight is not consider.
    – Sifina
    Oct 11, 2023 at 5:15
  • Did you modified both files (Block and Model) ? While your testing, also try to add a plain value in the entries array (like 'foo' => 'bar') to be sure the problem does not come from setting an empty value to the array
    – Cladiuss
    Oct 11, 2023 at 7:27
  • @Cladiuss I am trying add hard coded values 'foo' => 'bar' but it is not showing in xml. I also cleared cache Nov 6, 2023 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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