It will be advisable to add an index to a database field for better performance and quick selection of records from the database table. Use the below snippet to add an index to database field in Magento 2.
Magento 2
How To Redirect To Previous Page in Magento 2?
Today’s tutorial we are showing how to redirect to previous page from a custom action in magento 2. This will be useful when you have an error message to show after a form submission. namespace Vendor\Module\Controller\Adminhtml\Index; class Fetch extends \Magento\Backend\App\Action { public function execute() { // TODO: Implement execute() method. $this->_redirect($this->_redirect->getRefererUrl()); } }
How to Apply the Magento 2 patch?
Change your Magento store to Developer mode php bin/magento deploy:mode:set developer Download the security patch git apply MAGENTO-9090-2020-010-1-10-04-03.patch After the above run below command to apply the patch php bin/magento setup:upgrade
How To Programatically Cancel The Order Using Order Id in Magento 2 ?
It’s very common that we need a feature to cancel an order Programatically in Magento 2. Instead of factory method, we use Magento/Sales/Api/OrderManagementInterface as it’s advisable to use service contracts in Magento 2. Below snippet will help you cancel an order programatically in Magento 2. protected $orderManagement; public function __construct( … \Magento\Sales\Api\OrderManagementInterface $orderManagement, …. ) […]
How To Load Stock Item Using product_id In Magento 2?
In some cases you have to load stock item using product_id in Magento2. Today’s snippet we are showing how to do that. For illustration purpose we use a model example. namespace Vendor\Module\Model; use Magento\CatalogInventory\Model\Stock\StockItemRepository; class HelloWorld extends extends \Magento\Framework\Model\AbstractModel { protected $stockItemRepository; public function __construct( StockItemRepository $stockItemRepository ) { $this->stockItemRepository = $stockItemRepository; } public function […]
How to get GMT date time in Magento 2?
Magento 2 usually saves the time in GMT in database, but display on admin or frontend will be based on the selected timezone. Use the below code to get the time with GMT timezone from Magento 2 code. Magento comes with DateTime php class, Magento\Framework\Stdlib\DateTime\DateTime is used for Forms GMT date. <?php public function __construct( […]
How To Get The Collection Of Records From A Custom Module In Magento 2?
If you are working on a custom Magento 2 module, and wondering how to get the collection of records from your custom database table, this article is for you. This is very common that your Magento 2 website needs more database tables than what Magento shipped with. So in a listing page such as blog […]
Disable versions in Magento 2
By default Magento 2 static urls have a version string attached to it. For example, if you take the view source of the page, you will see urls contains the following string -version1490119662.
How to disable or allow guest checkout in Magento 2?
Default Magento 2 setup allows users to checkout as guest in your storefront. But one must say gathering max customer information will be advantageous for both customer and shop owner. Today’s tutorial we are showing how to disable or allow guest checkout in Magento 2. Step 1. Log in to Magento 2 admin panel. Step […]
“Your session has expired” On Clicking Add To Cart – Magento 2.3.x Localhost
In your Magento 2 journey, you might have greeted with this error message – “Your session has expired”, when clicking on the add to cart button.
How to Use Magento 2 code in external File/Script
This script allows you to call Magento 2 functions run outside Magento 2 setup. You can create any PHP file in Magento 2 root folder and run Magento 2 from there.
How to Add Store Codes to the URLs in Magento 2?
If you have a multi-language store and want to add Store Codes to this is the tutorial for you. So your example urls will look like this yourdomain.com/en, yourdomain.com/us, yourdomain.com/uk. Follow the below steps. Login to the Magento 2 admin panel Navigate to Stores > Configuration > General > Web and find the Url Options […]
Product Sort By Price: Low to High and High to Low Magento 2.3
Today’s tutorial we are showing, how to add a new filter option in the product listing area of Magento 2.3. Here we are adding price sorting options such as Low to High and High to Low.
Reset “Use Secure in Front End or Admin” in Database – Magento 2
If you ran into a problem like you have changed the secure admin URL back to non secure URL and you forgot to change the or Admin to zero -means disable. It’s total mess that you cannot access the admin side again as all the JavaScript and CSS urls will have https prefix and all […]
How To Change The Magento 2 Admin Session Timeout
It’s quite annoying that every time page goes to login page when we try to refresh a page or saving a product in Magento 2 admin side. So this can be achieved by changing the admin session timeout in Magento 2.
How to Install Magento 2.3 & Create a Web Server-LAMP
This tutorial demonstrates how to install Magento 2.3 and build a web server on operating system Ubuntu 16.04 LTS. In this tutorial I have used Digital Ocean droplet (2GB instance), you may please create the same or you can install one in your PC. First we create a user apart from root user, as it […]