Skip to main content

Posts

Showing posts with the label Magento2

Magento Cloud - Export Data from Production Database

When working for an organization, there are times when you need to export data from the production database. Ideally, you should build a report or export option in Magento admin for this information. However, these are one-time data for which developing a module or backend functionality is not worthwhile. So, for this circumstance, follow the procedures below to retrieve data from the database. Assuming you already know how to do ssh login to the production server. So let's start with logging into your server. Once you log in follow the below steps: Get Database credentials Once you log in, open your env.php file and get the database credentials stored on the server. You will find an array in the env file and get the credentials from the key with the name "db". Prepare SQL Query Prepare your query whatever you want to export and then save it in a SQL file. You can prepare this file directly on the server or locally. If you want to move this file from local to production t...

Disable Hover on main menu and make it work on click

The main menu in an eCommerce website holds significant importance as it serves as the primary navigational tool for users to explore and access various sections, products, and features of the online store. The main menu helps users discover products, services, and content they might not have been aware of. By presenting various categories and sections prominently, the menu can drive users to explore different parts of the website, increasing the chances of making a sale. The style and layout of the main menu contribute to the website's overall aesthetic & attractiveness. Magento essentially makes use of a jQuery menu widget to deliver all of its default features. By default, the menu appears when you mouse over it. I had a requirement in which I needed to open the menu only when the customer clicked. Because we were using Megamenu, and when it opened, it took up half of the screen. And this might be annoying for customers at times.  So to solve this, I have created a JS ...

How caching is used in Magento

What is Caching? In computing, a cache is a high-speed data storage layer that stores a subset of data, typically transient in nature, so that future requests for that data are served up faster than is possible by accessing the data’s primary storage location. Caching allows you to efficiently reuse previously retrieved or computed data. How caching is implemented in Magento? The Magento page cache library contains a simple PHP reverse proxy that enables full page caching out of the box. A reverse proxy acts as an intermediary between visitors and your application and can reduce the load on your server. Magento's default caching mechanism provides different storage options that store cached data in 3 different sources: File System Database Redis There are different types of cache available by default in Magento. You can check here about different types. Any page new page added to the website is by default cachable. But a developer can make any page uncacheable by adding a small ch...

How to call a Phtml file in another Phtml file?

Sometimes while developing new pages we need to use some blocks repetitively and for this, there are multiple options in Magento to call a block in another block. But here we are going to discuss a specific way to call a Phtml file from another Phtml file. For this, you need to open the Phtml file in which you want to call another Phtml and the below code: $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("MageInsight_ModuleName::template.phtml")->toHtml(); In this, you can change the block class with your block class OR you can use the same one if you don't have any block class to be included in it. If you want to pass a variable or call any function of your block in phtml then you can use the below code: $customBlock = $this->getLayout()->createBlock("Magento\Framework\View\Element\Template"); $customBlock->functionName(); $customBlock->setTemplate("MageInsight_ModuleName::template.ph...

What is Declarative Schema in Magento?

Declarative schema is the new feature in Magento to do database operations of creating and updating database tables. This is one of the major changes introduced by the Magento team in 2.3 and it replaces the old approach of creating InstallSchema & UpgradeSchema scripts. Why Declarative Schema? Before declarative schema, developers need to write different schema scripts in each version of a module. And when a customer upgrades Magento to a version several releases ahead of the installed version, the upgrade scripts between those two versions will still be executed. This increases the complexity to track changes with every upgrade script. And new declarative schema approach allows developers to declare the final desired state of the database and has the system adjust to it automatically. What is the use of db_schema.xml? Before Magento 2.3, whatever database operations developers were writing in InstallSchema & UpgradeSchema scripts are replaced by the db_schema.xml file. So now...

How to use Postman for Magento GraphQL APIs?

In this blog, we are going to see how we can use Postman for  GraphQL  APIs. And we will learn this by implementing a complete checkout process on a Magento eCommerce website. Before moving forward, let's see what is GraphQL. What is  GraphQL? GraphQL  is a data query language developed internally by Facebook in 2012 before being publicly released in 2015. Magento implements GraphQL to provide an alternative to REST and SOAP web APIs for front-end development. How to access Magento's GraphQL APIs? You can use any GraphQL IDE like GraphiQL or Postman . Or you can install chrome extensions ( GraphiQL extension ) for accessing GraphQL APIs. Among all the above options we are going to learn how to access GraphQL using Postman. How to use Postman for GraphQL APIs? Let's start learning a fresh tab in Postman. Once you install postman, you will be able to see as below screenshot.    Now please follow the below steps to use GraphQL in Postman. Get the GraphQL endp...

How to create custom router in Magento 2?

In this blog, we will discuss Magento routing and how to create a custom router for any existing router. The route defines the name of the module which is responsible for the requested URL.  The FrontController class searches through a list of routers, provided by the RouterList class until it matches one that can process a request. When the FrontController finds a matching router, it dispatches the request to an action class returned by the router. Standard Router For example, if URL http://localhost/demo/request/path is called from the browser then it defines  demo as the module_name/front_name, request as the controller_name & path as the action_name.  When we create a new module, routes are defined in the  routes.xml file maps which module to use for a URL with a specific frontName and area. The location of the routes.xml file in a module, either etc/frontend or etc/adminhtml , specifies where those routes are active. <config xmlns:xsi="http:/...

How to create a custom Rest API?

REST stands for representational state transfer and API stands for an application programming interface. REST API is basically used for performing some operations and getting responses based on the request using the HTTP protocol. Magento provides a huge list of REST APIs which you can check here . Another approach to check and play with the existing APIs in the Magento system is Swagger. You can access Swagger by any browser and the URL will be your website's baseUrl/swagger . Now let's see how we can add a new custom API in Magento. Let's learn this by creating a custom API to get all the store configurations using API.  Step - 1: Create a webApi.xml file and add the route for your API <?xml version="1.0"?> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd"> <route method="GET" url="/V1/custom/storeConfigurations/"...

How to apply the Security Patch in Magento?

Magento is a flexible, customizable open-source platform that has a huge number of inbuilt functionalities. Apart from having these advantages of open-source software, Magento has some drawbacks related to security threats and vulnerabilities. To resolve these threats Magento releases security patches from time to time. Security patches contain security fixes only. They are designed to make the upgrade process faster and easier. There are two ways to apply a security patch in Magento using: Command Line Composer Follow the below steps to apply security patches using Composer: Enable Maintenance Mode : It will prevent from customers placing unfulfillable orders. php bin/magento maintenance:enable Check running Cron Jobs : Wait for the currently working cron jobs. Once they are completed, turn off the services. Execute Composer Command : Security patches use the Composer naming convention  2.3.3-px . Use Composer to specify a patch. composer require magento/product-commun...

How to add Extension Attributes for customers in Magento 2?

Magento provides two types of attributes for entities. And here we going to learn how to add a new extension attribute in Magento. Please follow the below steps to add the extension attribute: Step 1: Add extension attribute Here we will add an extension attribute in extension_attributes.xml which resides under the etc folder of your custom module. For scalar attributes: <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd"> <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface"> <attribute code="stripe_id" type="boolean" /> </extension_attributes> </config> Here, the scalar attributes indicate the simple form of attribute representation, such as an integer, a string, or a boolean. Specify the class or interface of the extension attributes insi...