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.

Create product attribute programmatically in Magento

Create product attribute programmatically in Magento – In Magento, product is an EAV Model so to add a custom product attribute we need to create an EAV attribute. Before starting, make sure that you already prepared your own module. If you haven’t, you can create a simple module following this post.

In this tutorial, you need to to:
– edit your config.xml file
– create file /app/code/local/YourVendor/YourVendor/Model/Mysql4/Setup.php
– create file /app/code/local/YourVendor/YourModule/sql/yourmodule_setup/mysql4-install-1.0.0.php

Edit your config.xml file, add this content:

Create file /app/code/local/YourVendor/YourVendor/Model/Mysql4/Setup.php

with content:

Create file /app/code/local/YourVendor/YourModule/sql/yourmodule_setup/mysql4-install-1.0.0.php

with content:

In this file:
“1.0.0” is your module version which is declared in /etc/config.xml file.

To create a new product attribute, we will use function addAttribute of model eav/entity_setup. You can find that function in file /app/code/core/Mage/Eav/Model/Entity/Setup.php and this is the function:

 

After editing the file, if you already installed your module on your server before, you need to re-install in to execute mysql4-install-1.0.0.php file. You also can upgrade your module to higher version as 1.1.0 by editing config.xml file and rename file ‘mysql-install-1.0.0.php‘ to ‘mysql4-upgrade-1.0.0-1.1.0.php

To re-install your module, go to your phpmyadmin, find table core_resouce and delete row which has code ‘yourmodule_setup‘.

Finally, flush cache storage.

When everything is done, you could see your new product attribute in ‘Backend->Catalog->Attribute->Manage Attributes‘ search ‘new_product_attribute‘ for Attribute Code.

Create product attribute programmatically in MagentoThat’s how to create product attribute programmatically in Magento. Hope it will be helpful.
If you have any question, leave a comment or contact us.

Create custom product type in Magento

Create custom product type in Magento – As you know, in Magento, there are 6 product types:
– Simple products
– Configurable products
– Grouped products
– Bundle products
– Virtual products
– Downloadable products

In this tutorial, we will show you how to create a custom product type in Magento. If you don’t have your own module, please create a simple one following this post.

To create a new product type, we need to do:
– Edit your config.xml file to declare your new product type
– Create a model for your product type
– Create a model for your product type price

Let’s do it step by step.

1. Edit your config.xml file.

Open file /app/code/local/YourVendor/YourModule/etc/config.xml and add content:

– ‘special‘ is your product type code.
‘yourmodule/catalog/product/type/special‘ is your product type model.
– ‘yourmodule/catalog/product/type/special‘ is your product type price model.

2. Create product type model.

Create new file /app/code/local/YourVendor/YourModule/Model/Catalog/Product/Type/Special.php with content:

3. Create product type price model

Create new file /app/code/local/YourVendor/YourModule/Model/Catalog/Product/Type/Sepecial/Price.php with content:

In those 2 classes, you can override some functions of Magento core product type. You can find parent classes at:
/app/code/core/Mage/Catalog/Model/Product/Type/Abstract.php
/app/code/core/Mage/Catalog/Model/Product/Type/Price.php
At last, refresh all cache types and try to create a new product to check if Magento recognized your new product type.

create custom product type in Magento
create custom product type in Magento

 

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

Create new module in Magento

Create new module in Magento – As you know, Magento has its own modules in /app/core/ directory. In this tutorial, we’re going to create a simple module which is necessary to use in other tutorials.

To make Magento recognize your module, you need to create at least following directories and files:

/app/etc/modules/YourVendor_YourModule.xml
/app/code/local/YourVendor/YourModule/etc/config.xml
/app/code/local/YourVendor/YourModule/Helper/Data.php

Let’s do it step by step:

1. Create file /app/etc/modules/YourVendor_YourModule.xml

with content:

This file make Magento know about your module name and code pool ( which is ‘YourVendor_YourModule’ and ‘local’)

2. Create your module config file /app/code/local/YourVendor/YourModule/etc/config.xml

with content:

3. Create your module helper file /app/code/local/YourVendor/YourModule/Helper/Data.php

with content:

After creating those files, go to your backend -> System -> Cache Management, refresh all cache types and flush cache storage also.
At last, to check if Magento recognized your module, go to your backend -> System -> Configuration -> Advanced -> Disable Modules Output, you can see your module with name ‘YourVendor_YourModule‘ has been listed.

If you got any problem when create new module in Magento, please leave a comment or contact us.

Send custom email in Magento

Send custom email in Magento – Sometimes, you want to create a function that can send an email without using the built-in email feature of Magento. Let’s find out how to do that.

All things we need to to:

  • Create an email template which is a html file.
  • Declare that email template in config.xml file
  • Build your own send email function.

Let’s do it step by step.

1. Create an email template.

Create file: /app/locale/en_US/template/email/yourmodulename/sample_email.html with simple content:

You can use your own template name and your store locale instead of “en_US”

2. Declare that email template in config.xml file.

3. Create your own send email function using the email template.

You can place it anywhere you want.

 

You can review function ‘sendTransactional‘ of class ‘Mage_Core_Model_Email_Template‘ for details:

 

Override a Block in Magento 2

Override a Block in Magento 2 – In the last tutorial, you can learn how to create a table and model in Magento 2. Today I’ll tell you how to override a Block in Magento 2.
What we have to do:
– Build a Magento 2 extension structure. I’ll use the module in last tutorial, you can download it here.
– Set preference in di.xml file
– Define a class to override Magento core class.

Continue reading “Override a Block in Magento 2”