Posts

How to Add Product Name as Default Alt Text to All Product Images in Magento 2

 Steps to Add Product Name as Default Alt Text to All Product Images in Magento 2 Hello Magento Friends, Today I will be explaining How to Set Product Name as Default Alt Text to All Product Images in Magento 2. Alt text for product images informs the search engine what the image is about and helps index the product image when the user fires a query related to it. For Magento 2 stores, it is essential to give alt text to all the product images to aid in search engine rankings. Large stores can take advantage of Magento 2 Image Alt Tags Extension to automate the product image alternate text process and save time. You can also set the product name as the default alt text for all the product images in Magento 2. To do that, follow the below-given steps Contents [hide] 1 Steps to Set Product Name as Default Alt Text to All Product Images in Magento 2: 2 Conclusion: Steps to Set Product Name as Default Alt Text to All Product Images in Magento 2: Step 1: First, we need to create a “di.x...

How to Update Product Stock Programmatically in Magento 2

 Steps to Update Product Stock Programmatically in Magento 2: Step 1: We need to create a “Data.php” file inside our extension at the following path app\code\Vendor\Extension\Helper\ Now add the code as follows <?php namespace Vendor\Extension\Helper;   class Data extends \Magento\Framework\App\Helper\AbstractHelper {     protected $_productobj;     protected $_stockRegistry;       public function __construct(         \Magento\Framework\App\Helper\Context $context,         \Magento\Catalog\Model\Product $productobj,         \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,      ) {         $this->_productobj = $productobj;         $this->_stockRegistry = $stockRegistry;         parent::__construct($context);     }       public function updateProductStock($pr...

How to Get Estimated Shipping Methods using REST API in Magento 2

Steps to Get Estimated Shipping Methods using REST API in Magento 2: Step 1: Create the rootscript.php script file and add the below code <?php   $baseUrl = " http://yourdomain/"; // your magento base url //Customer Email id and Password set   $userData = array("username" => "test@gmail.com", "password" => "test@123");   $ch = curl_init($baseUrl."/rest/V1/integration/customer/token"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));   $token = curl_exec($ch);   $shippingData = array("address" =>   array("city" => "THOUSAND OAKS",        "company" => "Test Company",        "country_id...

How to Apply OR Conditions to Collection in Magento 2

 Steps to Apply OR Conditions to Collection in Magento 2: Step 1:  Apply OR condition for Magento 2 addAttributeToFilter collection protected  $_productCollection;   public function __construct(    \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection) {      $this->_productCollection = $productCollection; }      public function getProductCollection() {      $this->_productCollection->addAttributeToFilter(array(      array(           'attribute' => 'sku',           'like' => '24MB%’),           array(                'attribute' => 'type_id',                'eq' => 'simple')           ));        echo $this->_productCollection->getSelect(); } Output: SELECT...

How to Add Custom Text after Billing Address and Shipping Address in Admin Order View Page in magento2

 Steps to Add Custom Text after Billing Address and Shipping Address in Admin Order View Page in Magento 2:  Step 1:  First, we need to create a “sales_order_view.xml“ file inside our extension at the following path app\code\Vendor\Extension\view\adminhtml\layout\ Then add the below code <?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">     <body>          <referenceBlock name="order_info">                 <action method="setTemplate">                     <argument name="template" translate="true" xsi:type="string">Vendor_Extension::order/info.phtml</argument>                 </action>         </...

How to Set Dynamic Email Subject in Magento 2

 Steps to Set Dynamic Email Subject in Magento 2: Step 1: Create an email template configuration file at the below path app\code\Vendor\Extension\etc\email_templates.xml  Then add the below code <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">     <template id="your_email_template_id" label="Label of your template file" file="email_file.html" type="html" module="Vendor_Extension" area="frontend"/> </config> Step 2: Now create an email template file at the following path app\code\Vendor\Extension\view\frontend\email\email_file.html Now add the below code snippet <!--@subject {{var subject|raw }}@--> <!--@vars {"var customerName":"Customer Name", "var customerEmail":"Customer Email", "var customerCom...

How to Automatically Generate CSV File on Every Order in Magento 2,

 Steps to Automatically Generate CSV File on Every Order in Magento 2: Step 1: Go to the below File Path app\code\Vendor\Extension\etc\events.xml Then, add the code as follows <?xml version='1.0'?> <config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='urn:magento:framework/Event/etc/events.xsd'>    <event name='sales_order_place_after'>         <observer                 name='sales_order_place_after'                 instance='Vendor\Extension\Observer\GenerateCSV'         />     </event> </config> Step 2: Next move to the below File Path app\code\Vendor\Extension\Observer\GenerateCSV.php Then add the below code snippet <?php   namespace Vendor\Extension\Observer;   use Magento\Framework\Event\ObserverInterface; use Magento\Framework\App\Filesystem\DirectoryL...

How to Add Section after Image Gallery at Product Detail Page in magento2

 Steps to Add Section after Image Gallery at Product Detail Page in Magento 2: Step 1: First, we need to create a “catalog_product_view.xml” file inside our extension at the following path app\code\Vendor\Extension\view\frontend\layout\catalog_product_view.xml Now add the code block given below <?xml version="1.0"?>   <page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">     <body>         <referenceContainer name="content">             <block class="Magento\Framework\View\Element\Template" name="custom_content" template="Vendor_Extension::custom_content.phtml" after="product.info.media"/>         </referenceContainer>     </body> </page> Step 2:  After that, we need to create a “custom_content.phtml” file inside our exte...

How to Add Extra Category Description on Product Listing Page in magento2

 Steps  Add Extra Category Description on Product Listing Page in Magento 2: Step 1: First we need to create the DescriptionAttribute.php file inside the Setup folder app\code\Vendor\Extension\Setup\Patch\Data Now add the code as follows <?php namespace Vendor\Extension\Setup\Patch\Data;   use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\Patch\PatchRevertableInterface; use Magento\Store\Model\Store;   class DescriptionAttribute implements DataPatchInterface {     private $eavSetupFactory;       /**      * Constructor      *      * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory      */     public function __construct(EavSetupFactory $eavSetupFactory, ModuleDataSetupInterface $moduleDataSetup)     {        $...

How to Add Custom Field and Save Value in Product Attribute Add Form in magento2

  Steps to Add Custom Field and Save Value in Product Attribute Add Form in Magento 2: Step 1: First we need to create a “di.xml” file inside the etc folder app\code\Vendor\Extension\etc\adminhtml Now, add the code as follows <?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\Block\Adminhtml\Product\Attribute\Edit\Tab\Front">         <plugin name="extension_attribute_edit_form" type="Vendor\Extension\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab\Front" sortOrder="1"/>     </type> </config> Step 2: In the next step, we will create the “Front.php” file inside the Plugin folder app\code\Vendor\Extension\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab Then append the below-mentioned code snippet <?php namespace Vendor\Extension\Pl...

How to Add Conditional Statement in Email Template in magento2

Steps to Add Conditional Statement in Email Template in Magento  2: Step 1: Create blog file “Index.php” at the below path app/code/Vendor/Extension/Block/Index Then add the code as follows <?php   namespace Vendor\Extension\Block\Index;   class Index extends \Magento\Framework\View\Element\Template {     public function __construct(\Magento\Catalog\Block\Product\Context $context, array $data = [])     {         parent::__construct($context, $data);     }       protected function _prepareLayout()     {         return parent::_prepareLayout();     } } Step 2: Create controller file “Index.php” at the below-mentioned path app/code/Vendor/Extension/Controller/Index Then add the following code-snippet <?php   namespace Vendor\Extension\Controller\Index;   class Index extends \Magento\Framework\App\Action\Action {     public function execute() ...

Adding Admin Usernames and Action Names via Sales Order Grid Actions in magento2

 Steps to Add Admin User name and Action name in Order Comment Section in Magento 2: Step 1:  First, we need to create an “events.xml“ file inside our extension at the following path app\code\Vendor\Extension\etc\adminhtml\ Then add the code as follows. <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">     <event name="sales_order_save_after">         <observer name="sales_order_save_after_comment_history_add"           instance="\Vendor\Extension\Observer\OrderSaveAfter" />     </event> </config> Step 2:  After that, we need to create an “OrderSaveAfter.php” file inside our extension at the following path app\code\Vendor\Extension\Observer\ And embed the code as given below. <?php  namespace Vendor\Extension\Observer;   use Magento\Framework\Even...

How to Add Image Upload Field in System Configuration in magento2

 Steps to Add Image Upload Field in Magento 2 System Configuration: Step 1: Initially, you need to create a field in the system.xml file, at the path given below. app/code/Vendor/Extension/etc/adminhtml/system.xml Use the following code snippet  <field id="image" translate="label" type="image" sortOrder="20" showInDefault="1" showInWebsite="1">       <label>Upload Image</label>       <backend_model>Vendor\Extension\Model\Config\Backend\Image</backend_model>       <base_url type="media">extensionname/foldername/</base_url> </field> Step 2: Next you need to backend the model file Image.php. For that go to the below path Vendor\Extension\Model\Config\Backend\Image.php And add the code as follows <?php   namespace Vendor\Extension\Model\Config\Backend;   class Image extends \Magento\Config\Model\Config\Backend\Image {        const UPLOAD_DIR = 'yourfolder';...

Get Orders Collection Between a Date Range in Magento 2

Hello Magento Friends, In today’s Magento 2 tutorial, we will find out How to Get Orders Collection Between a Date Range in Magento 2. The admin uses order collection to get order details and smoothly fulfill the orders. Sometimes the admin does not want all order collection but needs only specific orders based on filter options. Suppose you want all orders between a specific date range. You can Get Order Collection Between Specific Date Range in Magento 2 using the below method. Contents [hide] 1 How to Get Orders Collection Between a Date Range in Magento 2? 2 Conclusion: How to Get Orders Collection Between a Date Range in Magento 2? Use the below code in your Root file <?php use Magento\Framework\App\Bootstrap; ini_set('display_errors', TRUE);  ini_set('display_startup_errors', TRUE);   require __DIR__ . '/../app/bootstrap.php';   $params = $_SERVER;   $bootstrap = Bootstrap::create(BP, $params);   $objectManager = $bootstrap->getObjectManager(); ...

How to Add Custom Fields in Invoice Totals in Magento 2 Invoice Email Programmatically?

 Steps to Add Custom Fields in Invoice Totals in Magento 2 Invoice Email Programmatically: Step 1: In the first step you need to create the sales_order_invoice.xml file at the below file path app/code/Vendor/Extension/view/frontend/layout/sales_order_invoice.xml Then add the code as follows <?xml version="1.0"?>   <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">     <body>         <referenceBlock name="invoice_totals">             <block class="Vendor\Extension\Block\CustomFieldinInvoice" name="invoice"/>             <action method="setLabel">                 <argument name="label" xsi:type="string">Custom Field Name</argument>             </acti...

Add Admin User name and Action name in Order Comment Section through Action Performed from Sales Order Grid in Magento 2

  Step 1:  First, we need to create an “events.xml“ file inside our extension at the following path app\code\Vendor\Extension\etc\adminhtml\ Then add the code as follows. <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">     <event name="sales_order_save_after">         <observer name="sales_order_save_after_comment_history_add"           instance="\Vendor\Extension\Observer\OrderSaveAfter" />     </event> </config> Step 2:  After that, we need to create an “OrderSaveAfter.php” file inside our extension at the following path app\code\Vendor\Extension\Observer\ And embed the code as given below. <?php  namespace Vendor\Extension\Observer;   use Magento\Framework\Event\ObserverInterface; use Magento\Framework\App\RequestInterface; use Magento\Backen...

How to Remove JS and CSS using Layout XML in Magento 2

 Step 1: Go to your layout file app\code\Vendor\Extension\view\frontend\layout\your_layout_file.xml  To remove JS and CSS, you need to add <remove> tag in layout XML under the <head> tag. <?xml version="1.0"?>   <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">       <head>         <remove src="Vendor_Extension::css/custom.css"/>         <remove src="Vendor_Extension::js/custom.js"/>     </head>   </page>

How to Add Editable Field in Admin Order View Page in Magento 2

 Step 1: Go to the below file path app\code\Vendor\Extension\view\adminhtml\layout\sales_order_view.xml Then add the below code <?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">     <body>         <referenceBlock name="order_additional_info">             <block class="Vendor\Extension\Block\Adminhtml\Order\Checkoutcustomformedit" name="admin.chekoutcustomfield" template="Vendor_Extension::checkoutcustomformedit.phtml" />         </referenceBlock>     </body> </page> Step 2: Next, move to the following file path app\code\Vendor\Extension\Block\Adminhtml\Order\Checkoutcustomformedit.php Now add the below-mentioned code snippet <?php namespace Vendor\Extension\Block\Adminhtml\Order;   use \Magento\Backend\Block\Templa...