Magento 2 Create Customer Programmatically

In Magento 2, there are some ways to create a new customer, fill the form in register page, create customer in backend, checkout … However, if you want to create account in some automation process, you will have to do it with code. This tutorial will show you how to create customer programmatically in Magento 2

Code snippet to create customer account

Code snippet to set address for customer account

Hope above code can help you save a little time on coding. In case you want to add many customers, you can apply the part of the code in a loop

Magento 2 Disable Newsletter Subscription Success Email

In Magento 2, you’re able to enable/disable email for newsletter confirmation at Store -> Configuration -> Customers -> Newsletter. However, there is no setting to disable newsletter success email. In this tutorial, we will tell you how to do that.

You can get the package of this module in the end of article.

Disable newsletter subscription success email - Metagento

Where does Magento 2 send the subscription success email?

The sending email process can be found in file:

/vendor/magento/module-newsletter/Model/Subscriber.php method sendConfirmationSuccessEmail 

Editing Magento core file is a bad option, your edited code will be removed when you upgrade Magento. So we will create a simple module to do that.

In this post, we just tell you how to disable subscription success email. You can read this post to know how to create an extension on Magento 2

Assuming that we already have an extension named “Metagento_DisableNewsletterSuccess“, let’s find out what to do next.

1. Create new file: /app/code/Metagento/DisableNewsletterSuccess/etc/di.xml

2. Create new file: /app/code/Metagento/DisableNewsletterSuccess/etc/adminhtml/system.xml

This part will create a new setting, where you can enable/disable this function in the future.

3. Create new file: /app/code/Metagento/DisableNewsletterSuccess/Plugin/Newsletter/Model/Subscriber.php

In this file, we will check the setting first. If the function is enabled, we set the import mode to stop sending emails.

4. After creating all files, execute following commands to install the module.

5. Check again the configuration and enable the function

Disable newsletter subscription email by Metagento

You can get the package of this module on github here:  https://github.com/metagento/magento-2-disable-newsletter-success-email

We also provide a free extension which gives subscriber discount coupon codes.

 

Magento 2 Permission for Files and Directories

In Magento 2 permission is very important. Some features ( or whole website ) won’t work properly if you don’t set the permission correctly. So what are the correct file and folder permissions for Magento 2 and How to set Magento 2 file/folder permissions? Let’s find out

Magento recommend setting the permissions of file/folder in Magento 2 as below:

  • 755 for directories: User has a full control and Group/World can traverse directories;
  • 644 for files: User – read, write; Group/ World – read.

 

It’s clear but how to set the permission like that? We can set them using following commands ( via SSH )

You may need to clear Magento cache then, using command

That’s all we need to do to set correct permission for files and directories in Magento 2

If you have any question, just leave us a message.

Magento 2 clear cache programmatically

In this post, let’s find out how to clear/flush cache programmatically in Magento 2. It will be useful for you in your custom module or new feature. 

Whenever you make any modifications to your Magento website, you will need to flush cache to see the changes. And as you may know, there are some ways to do that.

  1. Go to System -> Tools -> Cache Management then select Flush Magento Cache
  2. Remove var/cache directory using File Manager in cPanel, FTP or SSH
  3. Using code ( helpful for automation ) 

 

In Magento 1

 

In Magento 2

  • Define constructor

  • Create a function to flush magento cache

You can edit $types variable if you don’t want to clear all cache types.

 

Using above code, we can clear Magento cache instead of using magento admin panel or removing var/cache folder. If you need any assitance, leave us a message

Magento 2 component manager please wait stop loading

Magento 2 component manager please wait problem 

In Magento 2 backend, when you try to upgrade modules or Magento using Component Manager, you may get this problem: Magento keeps showing ‘Please wait’ message and it won’t stop.

Magento 2 component manager please wait

There are many reasons why you get this issue. However, in our case, it came from custom module composer and here is how we get it solved.

Go to this file:  /setup/src/Magento/Setup/Model/PackagesData.php

Create a backup of this file before editing.

Then replace it using this file:

PackagesData

You can read more discussion about this issue here: https://github.com/magento/magento2/issues/6718

And this is where you can get this file from Magento: PackageData.php from Magento

That’s how we solve the issue when Magento 2 component manager please wait stop loading. Hope it will be helpful.

 

How to setup cron jobs in Magento 2 with Cpanel and SSH

In this tutorial, we will guide you how to setup cron jobs in Magento 2 with Cpanel and Command line ( using SSH access)

Setup Cron jobs in Magento 2 with Cpanel

Step1 : Login your Cpanel and go to Advanced -> Cron Jobs, you will see the list of cron jobs on your server.

Step 2: You need to add following commands one by one and make them run once per minute.

YOUR_MAGENTO2_PATH is the absolute path of your magento 2 installation directory.

Setup cron jobs in Magento 2 with Cpanel

Step 3: This is an example when you added all commands exactly.

Setup cron jobs in Magento 2 with Cpanel

 

Setup Cron job in Magento 2 with command line ( using SSH access )

Step 1: Login ssh with your Magento 2 directory owner then execute command:

or if you logged in as root access you can use this command

magento_directory_owner is the owner of Magento 2 directory.

Step 2: You will be able to add cron jobs for this user, add following lines:

If you want to edit crontab with nano, you can execute this command instead:

Step 3: After saving crontab, you need to restart cron service on your server, use this command:

That’s how to setup cron jobs in Magento 2 with Cpanel or SSH access. Hope that it will be helpful.

If you have any question, please leave a comment. 

Magento multi site setup

In this post, we will guide you how to setup multiple websites in Magento

As you know, Magento support multiple websites stores and store views. That means you can manage separate websites using same admin panel. Each website has its own settings, themes, products, customers …

In this post, we will tell you how to create and setup multiple website on Magento 1.

Step 1: Create a website in Magento admin

Go to System -> Manage Stores -> Create Website

Setup multiple websites in magento - Metagento

Please remember new website code, store code because you will need it when editing index.php file.

Step 2: Create a new Root Category

( optional – if your new website has different products )

Go to Catalog -> Manage Categories -> Add Root Category

Step 3: Create a new Store

Go to System -> Manage Stores -> Create Store, select the website and the root category that you’ve just created.

Setup multiple websites in magento - Metagento

Step 4: Create a new Store View

Go to System -> Manage Stores -> Create Store View, select the store that you’ve just created.

Setup multiple websites in magento - Metagento

Step 5: Set new website Base URL

Go to System -> Configuration -> General -> Web -> Change “Current Configuration Scope” to the new website.

Edit Base URL in “Unsecure” & “Secure” tab using your new website domain.

Setup multiple websites in magento

Step 6: Edit index.php file in Magento root directory

Use your Cpanel File Manager or other FTP Clients as FilleZilla, WinSCP. Move to document root of Magento installation, then edit index.php file. You should create a backup of this file before editing.

Add this code:

BEFORE:

Step 7: Flush Magento Storage Cache in System -> Cache Management.

It’s done, that’s how to setup multiple websites in Magento 1.

In the next tutorial, we will guide you to do this in Magento 2.

If you have any question, please leave a comment.

Magento 2 Manage Indexer using command line

Indexing is how Magento transforms data ( products, categories … ), to improve the performance of your storefront.
Magento stores lots of data (including catalog data, prices, users, stores … ) in many database tables. To optimize storefront performance, Magento accumulates data into special tables using indexers.

For example, you change the price of an item from $4.99 to $3.99. Magento must reindex the price change to display it on your storefront.

Let’s see how to manage, refresh indexers in Magento 2 using command line via SSH

1. Magento 2 indexers

In default, there are 10 indexers:

  • Design Config Grid ( name: design_config_grid )
  • Customer Grid ( name : customer_grid )
  • Category products ( name: catalog_category_product )
  • Product categories ( name: catalog_product_category)
  • Product price ( name: catalog_product_price)
  • Product entity attribute value ( name: catalog_product_attribute)
  • Catalog search ( name: catalogsearch_fulltext)
  • Stock ( name: cataloginventory_stock)
  • Catalog rule product ( name: catalogrule_rule)
  • Catalog product rule ( name: catalogrule_product)

We can get indexer info using command:

2. Indexer status

An indexer status can be one of following:

  • valid: data is synchronized, no reindex required
  • invalid: the original data was changed, the index should be updated
  • working: indexing is in progress

We can get indexer status using command:

3. Indexing modes

Re-indexing can be performed in two modes:

  • Update on Save: index tables are updated immediately after the dictionary data ( product, category, store…) is changed.
  • Update by Schedule: index tables are updated by cron job according to the configured schedule.

We can check indexer’s mode using command:

To change indexer mode, we use command

“realtime” is equal to  “Update on Save” and “schedule” is equal to “Update by Schedule”.

4. Re-index

Besides using “System -> Index Management” in Magento backend, we also can re-index using command:

We will get the result like:

5. Reset indexer when it’s locked.

During re-index process, you may get the message like:

So we need to reset that indexer status to be able to re-index ( in this case, it’s “cataloginventory_stock” )

We can reset indexer using command:

That’s how we can manage Magento 2 indexers. Please leave a comment or contact us if you have any question.

Thank you!

Manage cache Magento 2 using command line

Manage cache Magento 2 using command line – In Magento 2 there are 2 ways to manage cache, going to “System -> Cache Management” in backend or using command line.

You can run commands in following ways:

  • cd <your Magento install dir>/bin and run them as ./magento <command name>
  • php <your Magento install dir>/bin/magento <command name>

1. View the cache status

magento cache:status

You can get a result like this:

1 means “Enabled”  and 0 means “Disabled”

2. Enable or Disable cache types

This command enables or disables all cache types or only the ones you specify

You can get each cache type code and its description here.

 

Above commands will enable/disable all cache types. You will get the cache types status after running the command.

3. Clean and Flush cache types.

What’s different between “Clean” and “Flush”

  • Cleaning a cache type deletes all items from enabled Magento cache types only. In other words, this option does not affect other processes or applications because it cleans only the cache that Magento uses.Disabled cache types are not cleaned.
  • Flushing a cache type purges the cache storage, which might affect other processes applications that are using the same storage.Flush cache types if you’ve already tried cleaning the cache and you’re still having issues that you cannot isolate.

Commands:

Please note:

magento cache:flush is equivalent “Flush Storage Cache” in System -> Cache Management

magento cache:clean is equivalent “Flush Magento Cache” in System-> Cache Management

That’s how we can manage cache Magento 2 using command line. 

Magento 2 Add a menu item in backend

In this blog post we will see how to add a menu item in backend for your Magento 2 module.

1. Create file VendorName/ModuleName/etc/menu.xml

The admin menu in Magento 2 has 3 parts:

  • The main admin menu title which shows up in left sidebar
  • Submenu title
  • Actual menu

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

3. Create Admin Routes with file: VendorName/ModuleName/etc/adminhtml/routes.xml

with content:

4. Create a backend controller to handle menu item url

VendorName/ModuleName/Controller/Adminhtml/World/Index.php

Finally, when you click on the new menu item, you will see “Hello World” in a page.

If something is wroing, you will be redirected to dashboard automatically.

That’s how to add a menu item in backend Magento 2, see you in other posts.