In this post, you will know about how to customize the mini-cart of a Magento 2 store by using the plugin. Let’s see and follow this step-by-step guide with examples.
Step 1: First we create a registration.php file under
app/code/Webiators/CustomChanges/registration.php and add the following code
1 2 3 4 5 6 7 |
<?php use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Webiators_CustomChanges', __DIR__); |
Step 2: After creating the registration file we need to create module.xml file under
app/code/Webiators/CustomChanges/etc/module.xml and add the following code
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Webiators_CustomChanges" setup_version="1.0.0"> <sequence> <module name="Magento_Checkout"/> </sequence> </module> </config> |
Step 3: We create catalog_attributes.xml file under
1 2 3 4 5 6 7 8 9 10 11 |
app/code/Webiators/CustomChanges/etc/catalog_attributes.xml and add the following code <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd"> <group name="quote_item"> <attribute name="manufacturer"/> <attribute name="custom_attribute_name"/> </group> </config> |
Also Read: How To Add Product Attribute Programmatically In Magento 2?
Step 4: We create a di.xml file under
app/code/Webiators/CustomChanges/etc/di.xml and add the following code
1 2 3 4 5 6 7 8 |
<?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\Checkout\CustomerData\DefaultItem"> <plugin name="Webiators_CustomChanges_Plugin_Magento_Checkout_CustomerData_DefaultItem" type="Webiators\CustomChanges\Plugin\Checkout\CustomerData\DefaultItem" /> </type> </config> |
Step 5: Now we need to create a plugin file DefaultItem.php file under
app/code/Webiators/CustomChanges/Plugin/Checkout/CustomerData/DefaultItem.php and add the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php namespace Webiators\CustomChanges\Plugin\Checkout\CustomerData; use Magento\Quote\Model\Quote\Item; class DefaultItem { public function aroundGetItemData($subject, \Closure $proceed, Item $item) { $data = $proceed($item); $product = $item->getProduct(); $atts = [ "product_manufacturer" => $product->getAttributeText('manufacturer'), "product_custom_attribute_name" => $product->getAttributeText(custom_attribute_name) ]; return array_merge($data, $atts); } } |