Magento 2 Development
Every time you need to do some testing, roll out an update or fix a bug, you need to enable the Magento 2 maintenance mode. It initiates the display of the "Service Temporarily Unavailable" messages across all your website pages.
So, regardless of other Magento modes, the maintenance mode causes the most inconvenience for both merchants and users. Especially if they don't know what that mode is about.
That's why today you'll get all the answers about what is Magento maintenance mode, why it matters and how to use it properly. We'll also share some secrets about enabling maintenance mode in Magento without downtime.
Intrigued? Let's get started!
What is Magento 2 Maintenance Mode?
Magento 2 maintenance mode is a process of temporarily disabling users from accessing your website while you perform updates, customizations, or other improvements. If you enable maintenance mode, website visitors will see a maintenance page with a custom message.
Some exceptions exist when users from certain
As a Magento 2 developer, you know how tedious performance debugging can be. Finding out why Magento is slow is not as easy as it seems. So any tools that help you on the way come in handy. In most cases, the Magento 2 profiler helps with the performance-related debugging best.
While you may use some third-party extensions for the job it takes time to find an effective solution fr your specific case. Thus, we'll look closer at the good old Magento 2 profiler and ways to improve it.
If you only start working with Magento, this is a great overview of the profiling functionality. Yet, adept developers can refresh their knowledge of the subject.
Let's get right to it.
What is Magento 2 Profiler?
Magento 2 profiler is a default tool for performance debugging. It gives insights into the loading time of various blocks as well as the amount of allocated and used memory to perform the task.
The profiler features a table with page blocks and resources used to load them. These details create a clear
Magento operates huge loads of information related to categories, products, prices, search results, etc. As you know, all this info is organized in separate tables so the needed details can be fetched promptly.
That's when Magento 2 reindex comes into play. It's a crucial process that keeps all the data relevant and helps to manage your store effectively both on the frontend and backend. Thus, the more you know about reindex in Magento the better.
In this guide, you'll learn what Magento reindex is, how to run it, how to fix reindex-related errors if any, and more. So, let's get to the point.
What is Magento 2 Reindex?
Magento 2 utilizes indexers to organize all the data it works with. Each data type has its indexer which in turn is responsible for the corresponding index table.
Therefore, there are separate index tables for prices, search terms, and products, to name a few. This way, when the request is sent, Magento fetches the data from these separate tables instead of the main table.
Magento 2 catalog price rules allow you to create encouraging offers and prompt your customers to finalize the purchase. Unlike cart price rules, catalog rules are applied to the entire catalog, not just the shopping cart.
Traditionally, you create catalog price rules via the admin panel. That's where you specify the rule information and conditions for the discounts to be applied. But you can perform all these steps using coding, too.
So, today you'll learn how to create a catalog price rule in Magento 2 programmatically.
Create Magento 2 Catalog Price Rule Using Dependency Injection
One of the safest methods to go for is dependency injection. To apply this method, use the code below:
<?php
declare(strict_types=1);
namespace Vendor\Module;
use Magento\CatalogRule\Api\CatalogRuleRepositoryInterface;
use Magento\CatalogRule\Model\RuleFactory;
use Magento\Framework\Exception\CouldNotSaveException;
use Psr\Log\LoggerInterface;
class CreateCatalogPriceRule
{
protected $ruleFactory;
protected
Magento 2 categories help make your store organized and easy to browse through. They enhance navigation greatly since customers can jump from one category to another without looking for some item in the entire catalogue.
As you know, you can easily create and manage Magento 2 categories in the admin panel. That's the most common way. But there is also a different approach.
You can also create a category in Magento 2 programmatically. In this guide, you'll find two methods to do so.
Create Magento 2 Category Using Dependency Injection
One of the methods to create a category programmatically is to use dependency injection. Here is the code you have to run:
<?php
declare(strict_types=1);
namespace Vendor\Module;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Catalog\Model\CategoryRepository;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\CouldNotSaveException;
use Psr\Log\LoggerInterface;
Providing high-quality customer service is one of the pillars of a successful business. Magento 2 offers you extensive features for customer accounts management out of the box. Thus, you can configure customer name, address, login, password options, and more.
However, the default options are not always sufficient if you need to gather some specific info. That said, you'll need to create custom customer attributes.
Unfortunately, Magento 2 doesn't offer the simple admin panel setup of this functionality. So, you need to add customer attributes programmatically.
Don't know how? No worries, you're about to find out!
1. Create a Magento 2 module
Magento handles customer attributes using the EAV model structure and the Data Patches to alter the info. Thus, this is what we'll apply today.
So, before adding customer attributes, you need to create a basic Magento module. If you already have a suitable one, you can just use it.
The method below requires you to have a module to run the code.
Magento 2 CMS blocks allow you to put specific content in the spotlight. If you share some announcements, discounts, or special offers using CMS blocks. This makes your customers finding and applying them much more probable.
You can create a CMS block in the admin panel, that's one way. However, it's also possible to create new CMS blocks in Magento 2 programmatically. This is exactly what you'll learn today.
First, you need to create a basic Magento 2 module and add the Setup folder to it. Then, create an InstallData.php file in app/code/Vendor/Extension/Setup and add the following code:
<?php
namespace Vendor\Extension\Setup;
use Magento\Cms\Model\BlockFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $blockFactory;
public function __construct(BlockFactory $blockFactory)
{
$this->blockFactory
Magento 2 customer accounts are mostly created on the frontend by customers themselves. However, there may be cases when store managers need to create customer accounts from the admin panel.
Nonetheless, creating customer accounts manually may be time-consuming if you need to create a few. Thus, it may be more convenient to create customer in Magento 2 programmatically.
It's exactly what you'll learn today.
Create Customer Using Dependency Injection
To begin with, you can create a customer programmatically using the dependency injection method. Just use the code below:
<?php
declare(strict_types=1);
namespace Vendor\Module\Model;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Encryption\EncryptorInterface;
class CreateCustomerService
{
/**
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @var CustomerInterfaceFactory
*/
protected $customerFactory;
Making the content of your store responsive and easy to browse through guarantees improved customer experience and seamless shopping. With this thought in mind, product categories will probably be your first resort.
To assign products to categories you need to select them manually in the Products in Category section in the admin panel. However, this may get rather tedious especially if you have an extensive catalog.
Thus, you might want to optimize this process and today you'll learn how. Keep reading to learn how to add products to category programmatically in Magento 2.
Add Products to Category Using Dependency Injection
One of the most common methods of adding products to categories is dependency injection. Just use the code below.
<?php
declare(strict_types=1);
namespace Vendor\Module\Model;
use Magento\Catalog\Api\CategoryLinkManagementInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
class AssignProductsToCategoryService
{
Over the years, Magento proved to be a trustworthy eCommerce platform for successful store development. Thousands of people decided to give it a shot and now continue to grow and scale their businesses.
The area of Magento 2 development has also been expanding. No wonder so many developers work with different Magento branches trying to find their place in this extensive architecture and create a career.
Becoming a certified Magento 2 developer opens more opportunities for you as a professional. The question is, where do you start?
We've prepared all the answers here. You'll learn what Magento certifications are available for you, and what they require and cover. Besides, we'll share some useful tips to help you pass the certification successfully.
Ready to start?
What is Magento 2 Certification?
Magento certification, or Adobe certification, is the verification and validation of your professional knowledge of Magento 2 development and/or management. Once passed, you receive a badge confirming
Although Magento is quite a smooth platform to work with, some common errors may still appear on the way. Thus, as a Magento developer, you need to know what is happening behind the scenes to fix the inconsistencies promptly.
In any case, when something is wrong, the first thing to check is the Magento 2 error log. The logs seem quite confusing, for both — those who only started working with Magento and even experienced developers.
So today we'll try to make things clearer for you to understand what are Magento error logs and how to work with them.
What is Error Log in Magento 2?
Magento error log is a place where error messages, notices, and warnings are stored. The error is displayed right away, on the page, if you work in developer mode. However, that's not the case with the production mode.
Sometimes, various inconsistencies happen without you noticing. Or you may receive a message, like "An error has happened during application run...", without clear details as to what happened
is one of the most essential steps your customers take in their journey. Thus, it's in the best interest of every store owner to make it as seamless as possible.
That said, the default checkout fields in Magento 2 are not always enough for the requirements of your store. You might need to gather some additional info about customers to improve your service. In this case, you should know how to add custom fields in Magento 2 checkout.
If you haven't figured out how to do it yet, this is exactly what you'll learn today.
By default, Magento 2 doesn't provide an option to add custom checkout fields via the admin panel. That's why you'll need to do it programmatically by following the below steps. So, let's get right to them.
1. Create a New Module
For starters, make up your mind as to what field you want you add. For instance, you may go for the "delivery_date" field. The next step is to create a new module using the following method.
Create a Magefan/DeliveryDate/registration.php
Magento 2 static content deploy is the Magento feature that helps to keep the user experience at the top levels. Why? It ensures your store pages are built correctly, static view files are downloaded by the browser, and static content is displayed according to user requests.
In this comprehensive guide, you will find everything you need to know about static content deploy in Magento 2. What it is, reasons to use it, how to deploy static content in Magento 2 in three different ways and how to reduce deployment time to zero.
Moreover, we’ll cover the Magento deployment best practices, strategies and examples to help you comprehend the topic better.
Whether you’re just starting with Magento development or have some knowledge already, this article will help hone your skills. You'll discover something new about the static content deploy in Magento.
We’ve got a lot to cover, but let’s start with the basics first.
Static View Files Deployment Overview
The static view file deployment allows you
One of the many steps in the order processing workflow is creating an order. In most cases, customers place orders from the frontend. Yet it happens sometimes that this task falls on the store managers. They might need to assist customers, apply custom prices or just merely test the process.
That said, you can create orders from the admin panel. That's one option. Yet it is also possible to create orders in Magento 2 programmatically. In this article, you'll learn two ways to do that.
So, let's get right to them.
Create Order Using Dependency Injection
One of the safest ways to go is to use the dependency injection. The code below will help you create an order programmatically in Magento 2.
<?php
namespace Magefan\OrderEdit\Model;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Quote\Model\QuoteFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
As much as store owners would like to have no refunds, this is not entirely possible. Customers may purchase the wrong size, colour, etc., receive incorrect items or change their minds unexpectedly. Merchants too may oversee various order-related details. Thus, a refund policy is a necessity to ensure reliable store-customer relations.
In Magento 2, a refund is referred to as a credit memo. It defines the amount of money a customer is supposed to receive back. Usually, credit memos are created in the admin panel. Yet, you can also do this using code.
So in this article, you'll learn how to create a credit memo in Magento 2 programmatically.
Create Credit Memo Using Dependency Injection
One of the recommended options to go for is the dependency injection method. The following code will help you create a credit memo programmatically.
<?php
namespace Venodr\MyModule\Model;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Sales\Model\RefundOrder;
Having your customers divided into groups can benefit your sales greatly. This helps to find a personalized approach and target the right customers with the right messages. To say nothing of the enhanced store performance and shopping experience.
You can certainly create Magento 2 customer group in the admin panel. Yet, if you'd like to manage this task through coding, there is an option for you too. And in this article, you'll learn how to create a customer group in Magento 2 programmatically.
Create Customer Group Using Dependency Injection
Using dependency injection, create a file in your module, and then call the execute function of that class CreateCustomerGroup in any place of your code to create a customer group.
<?php namespace Vendor\Extension\Model;
use Magento\Customer\Model\GroupFactory;
class CreateCustomerGroup
{
protected $groupFactory;
public function __construct(GroupFactory $groupFactory)
{
$this->groupFactory = $groupFactory;
}
public
The process of order management in Magento 2 consists of numerous steps. One of the last ones is creating a shipment. It is a relatively simple task, but it's useful to know how to cope with it as quickly as possible.
On one hand, you can create a shipment from the admin panel. This method is entirely manageable and not time-consuming. On the other hand, you might prefer an alternative — create a shipment in Magento programmatically. And in this article, you'll learn more about it.
Create a Shipment Using Dependency Injection
It is one of the most common ways to create a shipment. So, in order to create a shipment in Magento 2 programmatically using dependency injection, use the below code:
<?php
namespace Vendor\ModuleName;
class ClassName
{
public $orderRepository;
public $convertOrder;
public $shipmentNotifier;
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
$this->productRepository = $productRepository;
As a Magento 2 developer, you know that some coding is inevitable for successful store operations. There are many tasks you need to do programmatically, especially when it comes to getting some information like customer data and customer ID or config value.
One of them also is getting product by SKU in Magento 2. And in this article, you'll learn more about two possible ways of doing that.
Get Product by SKU Using Dependency Injection
Dependency injection is one of the most common solutions when it comes to getting product by SKU in Magento. It's quite simple, yet no less effective.
So, to apply the dependency injection method, go to your phtml block file and create a _construct function:
<?php
namespace Vendor\ModuleName;
class ClassName
{
public $productRepository;
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
$this->productRepository = $productRepository;
}
public function getProductBySky($sku)
Magento 2 is a flexible ECommerce platform that provides you with various options to customize your store to your needs. Thus, as a developer, you have to be aware of the coding matters to make everything works as flawlessly as possible.
There may be dozens of tasks that have to be solved programmatically, for instance, getting current URL or getting product by ID. However, it always goes down to configuration. So, it might also be useful to know how to get config value in Magento 2. And this is exactly what you'll learn today.
There are two ways to get config value in Magento 2. Let's have a look at both in more detail.
Get Config Value Using Dependency Injection
One of the most common methods to get configuration value in Magento is dependency injection.
In order to apply it, you need to go to the phtml block file and create a _construct function:
<?php
namespace Vendor\ModuleName;
class ClassName
{
public $scopeConfig;
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface
Running a Magento 2 store may not be as easy as it seems. There are lots of things you have to take care of, such as sales, orders, clients, etc., to say nothing of the development part. You should know how to run static content deploy or reindex Magento 2, as well as be aware of some other commands.
It's impossible to keep in mind all the commands for every single situation when you just start. So, that's why we created a list of useful Magento 2 commands that will save time and lessen the possibility of any errors.
Want to know more about Magento commands? Keep reading, this article is just what you need.
SSH
Command | Description |
---|---|
php bin/magento list | Generates a list of commands |
help | Shows help for a command |
Cache
Command | Description |
---|---|
php bin/magento cache:clean | Cleanes the cache types |
php bin/magento cache:enable | Enables the cache types |
php bin/magento cache:disable | Disables the cache types |
php bin/magento cache:flush | Flushes the cache types |
php bin/magento cache:status | Displays |
While there is a multitude of Magento 2 themes available on the market, you might still want to create custom theme for your store. Why? Templates, layouts, graphics and styles offered in a theme don't always satisfy your business requirements. Besides, the default Magento 2 themes Luma and Blank won't suffice if you're starting an Ecommerce website.
So, to create custom Magento 2 theme that conveys your brand's "feel" you need to resort to custom development.
In this guide, we'll walk you through all steps you need to take to create a theme for Magento and cover the basics of how to override basic and theme layouts.
Ready to start?
In case you're looking for a ready-to-use speed-optimized Magento 2 theme, we already have one — Optimized Magento 2 Theme.
Before Custom Theme Development in Magento
The first important thing to remember before starting theme development in Magento is that you shouldn't change or edit the default Luma and Blank theme files. If you customize the default