Magento 2 – Create system configuration – system.xml

In this blog post we will see how to create System Configuration for your module in Magento 2

System configuration in Magento 2 is very similar to Magento 1

System configuration is divided into different parts; tab, section, group and field which is described in magento1 blog. This same concepts are used in Magento2 as well.

1. Create VendorName/ModuleName/etc/adminhtml/system.xml file

with content

2. Set default values for these configuration item

Create file: VendorName/ModuleName/etc/config.xml

3. Create ACL file

Create file: VendorName/ModuleName/etc/acl.xml

Now when you open Magento admin, you see the “Test” tab and sections.
You might get an error 404 Not Found first time, just logout and login again to fix this.

That’s how to create store configuration in Magento 2. See you in other tutorials.

 

Magento 2 Fix Issue “No Region Found within the Locale…”

In Magento 2 sometimes, an error displays: No region found within the locale “de” (or other locale code). In this topic we will give you a solution for this issue.

How to get bug “No Region Found within the Locale…”

You have to setup language for your browser. Example with google chrome: Setting → Language → Choose your language → Done No Region Found within the Locale Magento 2 No Region Found within the Locale Magento 2

Then please refresh your browser you can get the bug if your extension use \Magento\Framework\CurrencyInterface

How to fix it?

We should use try catch function when define CurrencyInterface

Refresh your browser and this issue has gone.

That is the shortest process for you to  Fix Issue “No Region Found within the Locale…” in Magento 2 . With this guide, you can manage the Issue “No Region Found within the Locale…” in Magento 2 easily.

Thank you for reading this post and see you in other posts

Magento 2 Compile error: “No tokens were provided”

Magento 2 Compile error: “No tokens were provided”

In Magento 2, when you run “setup:di:compile” you may get error message:

“No tokens were provided”

That error message is caused by an empty file. You can find it using command:

It usually comes from an additional extension, just delete the empty file and run compilation again using:

I hope this will help. If you have any question, leave a comment or contact us.

Thanks.

Magento 2 Render WYSIWYG content

Render WYSIWYG Magento 2 – In this tutorial, we’re going to tell you how to render WYSIWYG Magento 2.

Some cases, you save your data using WYSIWYG editor then you want to render it in frontend.

With Magento 1, you can do it in this way:

However, in Magento 2, there is no BlockTemplateProcessor. We need to use this method instead:

Hope this could help.

Magento 2 Send Email Programmatically

Magento 2 send email programmatically – In this post, let’s find out how to send email programmatically in Magento 2. It will be useful for you in your custom module or new feature. What we need to do:

  • Create your email template in a html file.
  • Declare your email template.
  • Create a function to send your email.

We’re doing step by step:

Create new html file

/app/code/VendorName/ModuleName/view/frontend/email/modulename/test.html

with simple content:

Declare your email template, create xml file

/app/code/VendorName/ModuleName/etc/email_templates.xml

with content:

Finally, create a function to send your email.

In this tutorial, we’re using a controller action. Create file:

/app/code/VendorName/ModuleName/Controller/Index/Sendemail.php

with content:

You also can create this function in helper, model …

If you have any question, just leave a comment or contact us.

Thanks.

Magento 2 How to add link on my account navigation

How to add link on my account navigation in Magento 2 – In this tutorial, we’re going to add a new link to my account navigation in Magento 2.
You only need to create a layout file:

/app/code/YourVendor/YourModule/view/frontend/layout/customer_account.xml

with content:

In this file “mymodule/index/index” is url path of your link and “My Custom Link” is the label.

After flushing cache, your account navigation should look like this:

How to add link on my account navigation in Magento 2
How to add link on my account navigation in Magento 2

Upload image in Magento 2 programmatically

Upload image in Magento 2 programmatically – In this article, we’re going to tell you how to upload image in Magento 2 using a form. We have a post about this feature in Magento 1, you can take a look here.

What we need to do:

  • a form with a file input.
  • a controller to get request and save the images.
  • Assuming that we save all images in “/media” directory.

1. Create a form with content:

That’s just a simple form with a file input. You can add some elements as labels if you want.

2. Create controller and upload action

Create controller file:

/app/code/VendorName/ModuleName/Controller/Index/Upload.php with content

In this file, please note:

  • ‘test_image’ is the name of form input.
  • protected property $_objectManager is already declared in class \Magento\Framework\App\Action\Action
  • we only need to declare one more property $_fileSystem which is an instance of \Magento\Framework\Filesystem
  • $result  will be an array.

So we can use that form to upload image in Magento 2. After uploading the image, you will get it in ‘pub/media’ directory. Make sure that folder is writable.

Upload images in Magento programmatically

In our custom Magento modules, sometimes we need to be able to upload, save some images to our server. As we know, in Magento we can upload images for product, store logo … So we also can create a form which can be used to upload images and save them to the server. Let’s find out how to upload images in Magento.

What we need:

  • a form with a file input.
  • a controller to get request and save the images.
  • Assuming that we save all images in “/media” directory.

 

1. Create a form with content:

It’s simple, right ? Now we will create a controller with an action to get request from this form and process.

 

2. Create upload action in your controller class like this:

 

You can get uploaded file name and path in $result which is an array. You also can look into “/media” directory and see if the image is saved. Hope this post could help you upload images in Magento.

In the next post, we will tell you how to create this feature work in Magento 2.

 

Magento 2 Registry object

Registry object in Magento 2 is one of the most useful objects.

In Magento 1, we can simply use register and registry using static method of Mage class:

In Magento 2, it supports this method also. However, we need to use class  \Magento\Framework\Registry. You can init an instance of this class in class constructor.

 

To save a variable, we use this code:

To retrieve this variable:

Registry object in Magento 2 need more steps to be usable but it’s still very helpful. So please note this, you will need it in your Magento 2 module.

 

Magento 2 – admin page 404 after installation.

Magento 2 – admin page 404 after installation. In this post, let’s find out how to fix the error admin page 404 after installation in Magento 2. My environment:

  • Ubuntu server
  • Apache2
  • PHP7

After installing magento 2 , you may get 404 error when visit admin page. In that case, you should check if mod_rewrite module is enabled and loaded using this command:

If you already enabled mod_rewrite, you will get this message:

Or you will get this message if you didn’t enable mod_rewrite:

Then you should run:

In case you still get 404 error after that, open your site config file:

Add or edit:

Then restart your apache:

You can get access to Magento 2 backend now.

Please contact us or leave comment if you get any problem.