Programming

Programming? Aren’t 80% of the articles programming related? Well, yes. However, there are some that are really related to programming style, best practices, etc or ones that we couldn’t place anywhere else.


How to programmatically create customers in Magento 2.3.x

How to programmatically create customers in Magento 2.3.x

There are several ways to create customers in Magento 2. A customer can create an account on their own using the sign-up form, a customer can be created through the admin interface, and there is even a built-in Magento 2 import feature that can mass import a huge number of customers from a CSV file, provided that the customer data in the CSV is import-ready.

But what if we have thousands of customers whose data still needs to be processed before they can be created? The best way to do this would be to create the customers programmatically.

In this article, we are going to cover the topic of creating customers programmatically, and to this purpose, we are going to create a simple Magento 2 module which is going to have a custom console command and a couple of models that are going to be used to read, process and create customers.

Read more

Magento 2 logging

Magento 2 logging

Logging is an important part of every development process. Logs provide insight into system processes of Magento 2 and are a great tool for tracking errors, significant events, exceptions and many more. In order for the logging library to be properly implemented with Magento 2, it must comply with PSR-3 standard.

Magento 2 comes with built-in logging solution based on Monolog Library which will be analyzed further in this article.

Read more

How to save custom data in cache in Magento 2

How to save custom data in cache in Magento 2

Recently I was working on rendering images on a custom template. More precisely, I created a widget for inserting pictures that serve as a picture loader. And by default, the administrator has to input some parameters (width, height, alt) and if he decides, he could upload another image.

Reference for creating a widget tutorial could be seen here. The exciting part of the task was when the widget is saved on some of the Magento pages and loaded on i.e. ‘About Us’, image or images and parameter values have to be saved in the cache.

So when you visit ‘About Us’ another time, image content will be served instantly.

In this article, we’re going to save simple custom data inside the cache and load it when Magento renders the template.

Read more

How to improve usability for Magento 2 add to cart process

How to improve usability for Magento 2 add to cart process

Magento 2 introduced a new and improved way for adding products to the cart. The system now offers complete asynchronous (ajax) process, although the process itself is not activated in default application state. It requires some manual adjustments in the script call inside the templates. Someone would think, ok, we will simply modify the template script calls and that’s it! We successfully improved the process. Unfortunately, not so fast! There is still more room left for improvement. If you’re interested, let’s find out what’s left on the table.

Read more

Pagination with rel=”next” and rel=”prev” in Magento 2

Pagination with rel=”next” and rel=”prev” in Magento 2

Out of the box, Magento 2 offers fair amount of search engine optimization options but when it comes to category pages, we only have option to add canonical meta tags. In today’s blog post, we will try to spice that up a bit by implementing our own variation of canonical tag and by adding rel=”next” and rel=”prev” tags to help bots with paginated content.

Read more

Create Admin Menu Item in Magento 2

Create Admin Menu Item in Magento 2

When it comes to Magento customization, every now and then, there’s a need for a custom configuration option that needs to be placed somewhere in the administration menu. Magento 2 comes with well organized admin menu, but what if newly created configuration option doesn’t fit anywhere?

Read more

Overriding classes in Magento 2

Overriding classes in Magento 2

Compared to its previous version, Magento 2 came out with a new concept of dependency injection where classes inject dependencies (different objects) for an object instead of that object manually creating them internally. That way overriding and manipulating with classes is much easier and allows us more ways of extending the native functionalities.

Read more

Magento 2 custom widget

Magento 2 custom widget

In one of our previous articles we learned how to create a widget. Now we will see how we can create a custom one, or even better, how to extend the core one. For this example I picked default catalog product listing widget that I will extend with sorting fields for better customization of this widget.

Read more

File upload in Magento 2 store configuration

File upload in Magento 2 store configuration

One of the best parts in Magento is its admin interface. There is support for all kinds of input types (text fields, radio buttons, dropdowns, multiple selects, etc.), that can be stored in various ways (plain, encrypted, serialized), and displayed in numerous ways (grids, forms, simple fields, images).

What makes Magento great is the possibility to extend this default interface (especially store configuration part), and add our own fields easily. Let’s extend it with a custom file upload field.

Read more

Add new console command in Magento 2

Add new console command in Magento 2

Remember that time when you wanted to create a Magento shell or php script to perform some action, but didn’t know where to put it?
Remember the /shell folder in root Magento installation (or root folder, for that matter)?
Remember how it used to have, like, 50 shell and php scripts all mixed up, and you get a headache just by looking at them?
Pepperidge Farm remembers.

Those were good old times, and like all good times, they have now come to pass. There’s a new kid on the block, more powerful and way cooler, and the best part is – it’s integrated into Magento 2.

Read more

Interactive Bash script for managing themes in Magento 2

Interactive Bash script for managing themes in Magento 2

Do you remember, how many times you did something that was time consuming and/or required certain amount of steps to achieve your goal? After some time you had to repeat the whole process… And again… And again… I am pretty sure you came to an idea, “Wouldn’t it be really useful if I can somehow simplify the process?” Yes, you are correct, the same question troubled me when trying to figure out how the make my life easier when creating a custom theme. Unlike Magento 1, Magento 2 has a different (and more complex) way of setting up themes, so I started developing a Bash script that will make my life easier. Hopefully, yours as well.

Read more

Magento 2 Controllers

Magento 2 Controllers

Controllers in Magento 2, like on other PHP MVC frameworks, are important part of Mvc flow. In Magento 2 there are lots of changes in controllers, for example; how they are structured and how they work compared with Magento 1. If you are familiar with Magento 1 controllers, then you know they can have multiple actions (class methods). In Magento 2 controllers have only one method (execute) that will be called by front controller. This article covers controller basics, matching flow, controller types (admin and frontend), changes on existing controllers, instructions to create custom controllers and an example how to create a few controllers.

Read more

Routing in Magento 2

Routing in Magento 2

We can say that routing in Magento is one of the most important parts. Complete application (Magento 2) flow depends on processing URL request and router classes which are responsible for matching and processing that requests. This article covers routers flow in Magento 2 and analyses some routers that come with default installation. Also it will be shown how to create one custom router. It will be mentioned how routers match action (Controller class), and more info about Controllers will be covered in a separated article.

Read more

Implementing payment gateway in Magento 2

Implementing payment gateway in Magento 2

This is a revised article originally written in September 2014 when I started to play with Magento 2 for the first time. Since there were a lot of changes in Magento 2, this article also needed some refreshment. We will glance over the backend, and proceed with the development of a simple module. For this task, I picked up the payment gateway API integration. This relatively simple task will help us demonstrate some key changes in Magento 2.

More precisely, we will be focusing on implementation of Stripe payment gateway. Even though Stripe has a rich set of features, here we will be focusing only on most basic functionalities to get you started with Magento 2 extension development.

Read more

Basics of dependency injection and its usage in Magento 2

Basics of dependency injection and its usage in Magento 2

One of the biggest changes in Magento 2 is the usage of dependency injection design pattern. With this pattern, a lot has been changed inside codebase, and many new things have been introduced. In this article, I will try to explain the very basics of this design pattern, and its implementation in Magento 2 to help those who are beginners in the field of dependency injection.

Read more