How To Create Plugin Using Custom Module in Magento / Adobe Commerce 2.x

The following below steps to create custom module using plugin

Step [1] – Nominate Module Name ::

Syntax as below

Namespace_Modulename =Mage2db_John

Magento2 Root Directory / app /code / Mage2db / John

Step [2] – Create a module’s registration file :: This file name is registration.php & used to registered module.

File Path as below

Magento2 Root Directory / app /code / Mage2db / John / registration.php

Write below code in registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Mage2db_John',
    __DIR__
);

Step [3] – Create a module’s naming configuration file :: This file name is module.xml & used to define module name

File Path as below

Magento2 Root Directory / app /code / Mage2db / John / etc / module.xml

Write below code in module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mage2db_John" setup_version="1.0.0"/> 
</config>

Step [4] – Create a di.xml file to define Preference

File Path as below

Magento2 Root Directory / app /code / Mage2db / John / etc / di.xml

Write below code in di.xml to define Preference

<?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="change_product_price"      type="Mage2db\John\Plugin\Product" sortOrder="1" />
    </type>

    <type name="Magento\Checkout\Model\Cart">
        <plugin name="change_product_quantity"   type="Mage2db\John\Plugin\Cart" sortOrder="1" />
    </type>
</config>

Step [5] – After Plugin

Create a product.php file

File Path as below

Magento2 Root Directory / app /code / Mage2db / John / Plugin / Product.php

Write below code in product.php to extend core functionality of Product Model (to increase product price)

<?php
namespace Mage2db\John\Plugin;

class Product
{
	public function afterGetPrice(\Magento\Catalog\Model\Product $productprice, $result){
		$result += 100;
		return $result	; 
	}
}

Step [6] –  Before Plugin

Create a cart.php file

File Path as below

Magento2 Root Directory / app /code / Mage2db / John / Plugin / cart.php

Write below code in cart.php to extend core functionality of cart model (to increase added product quantity)

<?php 

namespace Mage2db\John\Plugin;

class Cart
{
	public function beforeAddProduct(\Magento\Checkout\Model\Cart $productInfo, $productcartqty = null)
	{
		$productcartqty['qty'] = 10; 
		return array($productInfo, $productcartqty);
	}
}

Step [7] – Finally run below CLI command & changes would be reflected as below

How To Create Events & Observer Using Custom Module in Magento / Adobe Commerce 2.x


How To Create Preference Using Custom Module in Magento / Adobe Commerce 2.x


How To Create Plugin Using Custom Module in Magento / Adobe Commerce 2.x


How To Get Base URL in Magento 2.x / Adobe Commerce 2.x


How To Create Custom Module in Magento 2.x / Adobe Commerce 2.x


How To Add Custom Block on Cart Page in Magento 2.x / Adobe Commerce 2.x


How To Create a Custom Log File in Magento 2.x / Adobe Commerce 2.x


How To Create Custom Controller in Magento 2.x / Adobe Commerce 2.x


How To Create a Custom Console Command in Magento 2.x / Adobe Commerce 2.x


How To Add Customer Groups Dropdown in Magento 2.x Admin Form and Grid By UI Component in Magento 2.x / Adobe Commerce 2.


How To Get all Customers Data in Magento 2.x / Adobe Commerce 2.x


How To Set Tier Price With Percentage Value Programmatically in Magento 2.x / Adobe Commerce 2.x


How To Add Tier Price Programmatically in Magento 2.x / Adobe Commerce 2.x


How To Add Websites Dropdown in Admin Form and Grid By UI Component in Magento 2.x / Adobe Commerce 2.x


Magento 2 All Database Tables [500 & more Tables]


How To Set Multi Shipping Settings In Magento 2


How To Set Origin Shipping in Magento 2


Difference Between Offline Shipping Method and Online Shipping Method


Magento 2 Online Customers Options



How To Apply Customer Group Price of Products in Magento 2


How To Add Customer Groups Dropdown in Magento 2 Admin Form and Grid By UI Component


How To Get all Customers Data in Magento 2


How To Create Customer Order in Magento 2 Admin Panel


Magento 2 Login As Customer Not Enabled


How To Configure Customer Account Sharing Options in Magento 2


Magento 2 Redirect To Customer Dashboard After Login


Which Magento 2 database table store customer shipping and billing address


How To Remove Sales Order Data & Customer Data in Magento 2


Which Magento 2 database table store customer’s Email Data


Which Magento 2 Database Table Store Customer Newsletter Data


Which Magento 2 database table store customer’s shipping and billing address


How To Remove Sales Order Data & Customer Data in Magento 2


Which Magento 2 Database Tables Store Customer Rating


Which Magento 2 Database Tables Store Customer Wishlist Products


Magento 2 Increase Customer Session Time


Which Magento 2 Database Table Store Patches

Leave a Reply

Your email address will not be published. Required fields are marked *