Book Image

Magento 2 Development Cookbook

Book Image

Magento 2 Development Cookbook

Overview of this book

With the challenges of growing an online business, Magento 2 is an open source e-commerce platform with innumerable functionalities that gives you the freedom to make on-the-fly decisions. It allows you to customize multiple levels of security permissions and enhance the look and feel of your website, and thus gives you a personalized experience in promoting your business.
Table of Contents (18 chapters)
Magento 2 Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding a block of new products


In the previous recipes, we prepared the module for the real work. We added the most common features to the module so that we can easily extend it with the new functionality.

In this recipe, we will create a block of new products to the page we created in the previous recipes.

Getting ready

In our module, we will create a block that will load a product collection. This product collection will be used in the template, which will show the newest products of the shop.

Ensure that you have the module of the previous recipe installed.

How to do it...

The following steps demonstrate how to start with adding the block with new products:

  1. To create the block class, we have to create the Newproducts.php file in the app/code/Packt/HelloWorld/Block/ folder.

  2. Add the following content to that file:

    <?php
    namespace Packt\HelloWorld\Block;
    
    use Magento\Framework\View\Element\Template;
    
    class Newproducts extends Template
    {
    
    }
  3. Create a template in the module folder. We can do this...