Magento 2 – Add Product to Cart Programmatically

While building an extension, you may want a function which add product to cart in background.
Example, give customer free gifts.

In this tutorial, we will create a method to programmatically add product to cart in Magento 2. 
Use following code to create a controller to test this function

Hope this tutorial is helpful. If you have any question, just leave us a message.
Thank you!

Magento 2 – Set custom page title for Category Pages

Set page title for Category Page in Magento 2 – By default, Magento uses the category name for page title. However, sometimes we want to set the page title manually.

In this tutorial, we will tell you a way to change category page title without editing the category name.
We will use layout update feature.

1. Go to the category you want to edit title

Admin Menu -> Catalog -> Categories -> Select the category -> Choose store scope ( optional )

2. Select tab “Design” and field “Layout Update XML”

Set page title for category pages

3. Enter following code

4. Save the category and clear cache.

That’s how we use a custom page title for category page.
Thanks for reading! Leave us a message or contact us if you need assistance.

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