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 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!

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.