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 an interceptor


One of the major things that has changed in Magento 2 is that there is no Mage class. To replace this, all objects are passed to the classes with dependency injection.

Dependency injection is a powerful tool that adds a lot of flexibility to add or change behavior in Magento.

Getting ready

To explore the possibilities of dependency injection of Magento 2, we need the module that we created in the previous recipes.

How to do it...

The following steps describe how we can modify the behavior of some classes, which is a new concept in Magento 2 that replaces the rewriting of classes in Magento 1:

  1. Create the app/code/Packt/HelloWorld/etc/di.xml file and paste the following content in it:

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="Magento\Catalog\Model\Product">
            <plugin name="Packt_HelloWorld::productName" type...