Page 6 - Ihor Vansach
- 3 min read
Imagine you are cooking pasta and sit down to play some game. You listen with one ear to see if the "dish" does not boil away. In this case, you are an observer. When the pasta starts to boil away — the event "pasta_began_to_boil_away" is triggered, which causes you (the observer) to rush to the kitchen (perform an action, an algorithm).
In Magento 2, as in real life, there are observers and events that are implemented on the basis of the "Publish-subscribe pattern". We have already described , which allows you to extend and change the functionality of the store. Let's see how you can do this with the help of events and observers in Magento 2.
Events
Events are triggered by Magento 2 modules when an action has occurred or has to occur. Magento has many native events and also allows you to call your own event.
Use the dispatch method of the \Magento\Framework\Event\ Manager class to call an event.
For example:
namespace MyCompany\MyModule;class MyClass{
/** *ihor
- 2 min read
You often need to change the JavaScript code logic located in the .js file. The easiest way is to override the js file is by using a theme. You can learn how to override view files in the article about . This is a quick but not an elegant way.
To change one or more js-file methods, use the mixins available in RequireJS.
To extend this file:
app/code/VendorName/ModuleName/view/%area%/web/js/folder1/folder2/somefile.js
with the following code:
define( [ 'jquery', 'underscore', 'ko', 'uiComponent', 'uiRegistry', ], function ( $, _, ko, Component, registry, ) { 'use strict';
return Component.extend({ // ... method1: function() { /* some code */ }, method2: function() { /* some code */ } // ... });
});
%area% - the area where the file is extended, for example: frontend, adminhtml, base.
To override theihor
- 1 min read
Usually, you get the error bash permission denied when running some script/file that does not have execute permissions. It is one of the . All you need to do to fix it is to change file permissions and add executive one.
To fix the bash permission denied error follow these steps:
1. Open terminal (shell)
2. Navigate to the folder with the script
3. Run the CLI command to change file permission settings:
chmod +x path_to_file/file_name
For example, if you run a :
bin/magento ...
and get the error:
bash: bin/magento: Permission denied
You need to add an execute (x) permission to the bin/magento file.
For this, please run the CLI command:
chmod +x bin/magento
In case of Magento 2 you can also use the next command to avoid the issue (php before bin/magento):
php bin/magento ...
Another issue you might face when running bin/magento commands, like the following ones, is the website breakdown during deployment:
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magentoihor
- 1 min read
All PHP programmers are familiar with the superglobal variables such as $_GET, $_REQUEST, $_POST, but their direct use is not allowed according to Magento2 code standards. So don't use them in your Magento 2 projects, especially if you want to pass the code Technical Review on the Magento Marketplace.
So what methods should be used?
To get data from the query, use the following methods: getParam($key, $default = null);getParams();getPostParam($key, $default = null);getPost();of the \Magento\Framework\App\RequestInterface class, for example:
protected $request;public function __construct( \Magento\Framework\App\RequestInterface $request, ....//інші параметри вашого класу) { $this->request = $request; ...//інший код конструктора}public function example() { // $data = $_REQUEST; $data = $this->request->getParams(); // $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null; $id = $this->request->getParam('id'); // $name = isset($_REQUEST['name'])ihor
- 1 min read
In Magento 2, one of the new properties is Magento 2 Virtual Type. What are they for?
Imagine that you already have a class A available:
namespace VendorName\ModuleName\Model
class A{ protected $arg1; protected $arg2; protected $arg3; public function __construct( Argument1 $arg1, Argument2 $arg2, Argument3 $arg3 ) { $this->arg1 = $arg1; $this->arg2 = $arg2; $this->arg3 = $arg3; }
}
And you need to create a class B, which will be inherited from A. However, it must take another argument as $arg2. To do this, you will create a new PHP file for class B:
namespace Me\MyModule\Model
class B extends A{ public function __construct( Argument1 $arg1, SomeOtherArg $someOtherArg, Argument2 $arg3 ) { parent::__construct($arg1, $someOtherArg, $arg3) }
}
But you don't need to do this in Magento 2. In such cases, virtual types are created. Therefore, create the etc/di.xmlihor
- 1 min read
Use the following code to insert the template block in WYSIWYG (Important! add it in inside double curly braces):
block class="Magento\Framework\View\Element\Template" template="Magefan_Blog::content/block-template-example.phtml"
The template identifier VendorName_ModuleName::some-template.phtml should be changed.
Attention! You can not use the block tag in the WYSIWYG product and category attributes. To make changes, install the magefan/module-catalog module.
- 5 min read
When running a blog, navigation is one of the most important things you need to keep in mind. It's essential that your readers can browse through your publications easily and find information fast.
The offers you an advanced instrument for that — a sidebar. Here you can configure various elements, such as the search form, category tree, related products, tags cloud and more.
You only need to press a few buttons to manage the Magento 2 blog sidebar. And today you'll learn how to do that.
To manage the Magento 2 blog sidebar, navigate to Stores > Configuration > Magefan Extensions > Blog and unfold the Sidebar section, Here you'll find all the available widgets. Let's have a look at each of them in particular.
Note: you need to set the Sort Order to determine the position of each widget. The larger the number, the lower the widget is displayed in the sidebar.
Search Form
Here you just need to Enable the search form and specify its Sort Order. Once done, the search will be displayed in theihor
- 2 min read
When you , you shouldn't forget about their appearance on the and post lists. Especially if you have some short content added for the post. The "read more" is a call to action that encourages visitors to click on the posts.
So, in this guide, you'll learn how to add the "read more" button in Magento 2 blog in two different ways.
Add "read more" via WYSIWYG editor
Navigate to Content > Blog > Posts and select a blog post to which you want to add the "read more" button.
Scroll down to the Content section.
Insert Pagebreak tag wherever you want.
4. Save a post one you finish and go to the checkout to see the result.
Add "read more" via Page Builder
Go to Content > Blog > Posts and select a necessary blog post.
Scroll down to the Content section and start editing using the page builder.
Insert HTML Code block wherever you want to add the page break.
4. Start editing the HTML Code block.
5. Add the "ihor
- 1 min read
Our provides multiple import options for you to move your blog content from one blog extension or platform to Magento 2.
In this article, you will learn about WordPress.
Take the following steps to import WordPress blog to Magento 2:
Set the Database Name where you run the WordPress Blog.
Enter the User Name and Password.
Specify the Database Host.
Enter the Table Prefix.
Choose the Store View you want WordPress Blog to be imported to.
If you want to see the WordPress blog import to Magento 2 in action, check this short video:
Note: only the image links will be imported, so you have to move your WordPress blog images to Magento pub/media/magefan_blog folder for them to be displayed.
Troubleshooting
Due to security reasons, databases are configured to accept requests only from the same server as they are (localhost), and it blocks all requests from outside (another server).
So, if you have issues migrating WordPress blog to Magento 2, you have to move your WordPress database to theihor

