Remove index.php from Magento Store URL

In this blog, we will see how to remove index.php from base URL of a Magento store.

To remove “index.php” from the web-site URL, the mod_rewrite module should be enabled on your server.

In case, mod_rewrite is not enabled, follow these steps:

  1. Run this command sudo a2enmod rewrite
  2. Edit your httpd configuration and write following lines
    <Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
  3. Now, restart apache service using this command (the commands might change based on the OS that you use)
    sudo service apache2 restart

You need to following these steps:

  1. Log in to your Magento store admin and go to the Store ⇒ Configuration section. (In case of Magento 1, it’s System  Configuration)
  2. Then click on the Web tab in the leftside menu.
  3. Expand the Search Engines Optimization tab and set the Use Web Server Rewrites option to Yes.
    Search Engines Optimization
  4. Then, go to the Base URLs (Secure) tab and set the Use Secure URLs on Storefront option to Yes, too. Click on the Save Config button.
    Base URLs (Secure) ⇒ Use Secure URLs on Storefront
  5. Make sure your Secure and Unsecure base urls should end with “/”.

Magento 2 Console Command Sortcuts

While working on Magento 2.x, you often need to run several console commands which are actually very handy in terms of managing the things such as cache, upgrades, deploy modes, indexers etc. You can check the entire list of commands by running this command:

php bin/magento list

Well, typing the full long commands are not very pleasant if it has to be done multiple times. So, we can use shortcuts instead full commands. This feature comes due to the Symphony framework which is used by each console class that implement command line functionality. The use Symfony\Component\Console\Command component to implement command line features. You can learn more in the documentation of Symphony framework. You can use shortest unambiguous name intead of full name, e.g. s instead of setup.

Now, let’s see shortcuts for some of the commonly used Magento 2 console commands:

1. Cache Commands

php bin/magento cache:clean ==> php bin/magento c:c

php bin/magento cache:disable ==> php bin/magento c:d

php bin/magento cache:enable [cache_type] ==> php bin/magento c:e [cache_type]

php bin/magento cache:flush [cache_type] ==> php bin/magento c:f [cache_type]

php bin/magento cache:status ==> php bin/magento c:s

Where cache_type is the parameter in which you can pass one or more cache types separated by whitespaces, e.g.

php bin/magento c:c config layout

2. Indexer Commands

php bin/magento indexer:reindex ==> php bin/magento i:rei

php bin/magento indexer:info ==> php bin/magento i:i

php bin/magento indexer:status ==> php bin/magento i:sta

php bin/magento indexer:show-mode ==> php bin/magento i:sho

php bin/magento indexer:reset ==> php bin/magento i:res

You may pass parameters for particular indexer type as well.

3. Compile Command

php bin/magento setup:di:compile ==> php bin/magento s:di:c

4. Setup Upgrade Command

php bin/magento setup:upgrade ==> php bin/magento s:up

5. Static Content Deploy Command

php bin/magento setup:static-content:deploy ==> php bin/magento s:s:d

6. Module Management Commands

php bin/magento module:status ==> php bin/magento mo:s

php bin/magento module:disable Namespace_Module ==> php bin/magento mo:d Namespace_Module

php bin/magento module:enable Namespace_Module ==> php bin/magento mo:e Namespace_Module

php bin/magento module:uninstall Namespace_Module ==> php bin/magento m:u Namespace_Module

How to create admin user using Command Line Interface | Magento 2

As you know a Magento Administrator can create a new user of any role from admin panel, but in some cases, one might need to create it from some other means rather than the conventional method (i.e. using Magento Admin Panel). E.g. if you forget admin login credentials, and you are unable to reset it by normal methods, then you will need some alternate way. This blog explains how you can add a new admin user using command line interface.

This is the command for adding new admin user:

php bin/magento admin:user:create --admin-user="username" --admin-password="password" --admin-email="[email protected]" --admin-firstname="First Name" --admin-lastname="Last Name"

You need to put proper information for username, password and email id and make sure neither username or email should be attached with existing admin users.

That’s all folks. You are all set to create new admin user using CLI.

Add Customer Attributes Programmatically in Magento 2

In this article we’ll see what’re customer attributes and how we add them programmatically in Magento 2. So we’ll proceed with following points :

1. What’re customer attributes
2. Why we need to add them programmatically
3. Steps to create customer attribute.

1. What’re customer attributes

In Magento, fields which are used in registration, login and filling customer address are basically customer attributes. They’re further divided into customer attributes and customer address attributes. Now if attributes used in customer registration / login are customer attributes and attribute used in address information for customers are customer address attributes. Some examples of customer attributes are below :

a. Customer Attributes
– First Name, Last Name, DOB etc
b. Customer Address Attributes
– Zip, City, Street, Country, Region/State etc

2. Why we need to add them programmatically

Magento by default didn’t provide functionality to add customer attributes in CE and sometime our project requirement have to add some extra fields(customer attributes) in registration form or address form. To do so we need to add them programmatically or through third party modules.

3. Steps to create customer attribute.

Let see how we can create customer attributes programmatically in Magento 2 step by step :-

Step 1: Create setup file InstallData.php

Firstly, we will create the InstallData.php file:
File: Excellence/Customerattr/Setup/InstallData.php

<?php
namespace Excellence\Customerattr\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
	private $eavSetupFactory;

	public function __construct(EavSetupFactory $eavSetupFactory)
	{
		$this->eavSetupFactory = $eavSetupFactory;
	}

}

In this class, we define the EAV setup model which will be use to interact with Magento 2 attribute.

Step 2: Define the install() method

After that, we have to define the install() method and create eav setup model:

	
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
	{
		$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
		$eavSetup->addAttribute(
			\Magento\Customer\Model\Customer::ENTITY,
			'gst_number',
			[
				'type'         => 'varchar',
				'label'        => 'GST Number',
				'input'        => 'text',
				'required'     => false,
				'visible'      => true,
				'user_defined' => true,
				'position'     => 100,
				'system'       => 0,
			]
		);
}

Step 3: Create custom attribute

Finally, we need to set the forms in which the attributes will be used. In this step, we need define the eavConfig object which allow us to call the attribute back and set the data for it and the full code to create customer attribute is:

Now our final InstallData.php looks like below :-

<?php

namespace Excellence\Customerattr\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;

class InstallData implements InstallDataInterface
{
	private $eavSetupFactory;

	public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
	{
		$this->eavSetupFactory = $eavSetupFactory;
		$this->eavConfig       = $eavConfig;
	}

	public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
	{
		$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
		$eavSetup->addAttribute(
			\Magento\Customer\Model\Customer::ENTITY,
			'gst_number',
			[
				'type'         => 'varchar',
				'label'        => 'GST Number',
				'input'        => 'text',
				'required'     => false,
				'visible'      => true,
				'user_defined' => true,
				'position'     => 999,
				'system'       => 0,
			]
		);
		$attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'gst_number');

		//  used_in_forms are of these types you can use forms key according to your need ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address', 'customer_account_create']
		
               $attribute->setData(
			'used_in_forms',
			['adminhtml_customer', 'customer_account_create']

		);
		$attribute->save();
	}
}

Note: We are using InstallData script (but not InstallSchema or UpgradeSchema) here as we are not going to change anything related to schema (database/table structure). When the script is run, the attribute is created by creating a new entry in eav_attribute (as Magento uses EAV ORM).
Step 4: Show Customer Attribute in Register form
For showing this customer attribute on the registration page, we need to do following things :

We will add our phtml files to ‘form.additional.info’ reference name using Excellence/Customerattr/view/frontend/layout/customer_account_create.xml

<page 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="form.additional.info">
            <block class="Magento\Framework\View\Element\Template" name="gst_number" template="Customerattr::extra_field.phtml"/>
        </referenceContainer>
    </body>
</page>

Explaination : we’re not overriding the entire vendor/magento/module-customer/view/frontend/templates/form/register.phtml file. Instead we used container name form.additional.info as reference and adding a new block to it. ‘form.additional.info’ is being called in register.phtml using this code getChildHtml(‘form_additional_info’); ?>

So by above xml we can simply add new attribute fields with editing register.phtml of core or without overriding it and getChildHtml(‘form_additional_info’); ?> automatically read content of ‘form.additional.info’ child block.

Now, in the extra_field.phtml file, we will write the code to show the additional textbox:

<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */echo __('* Required Fields') ?>">
	<legend class="legend"><span><?php /* @escapeNotVerified */echo __('Additional Information') ?></span></legend><br>
    <div class="field gst_number required">
        <label class="label" for="email"><span><?= $block->escapeHtml(__('GST Number')) ?></span></label>
        <div class="control">
            <input type="text" name="gst_number" id="gst_number" title="<?php /* @escapeNotVerified */echo __('My Attribute') ?>" class="input-text" data-validate="{required:true}" autocomplete="off">
        </div>
    </div>
</fieldset>

After performing all of the above tasks, the attribute will be shown on the customer registration page.

Now, when a user creates an account along with the GST number, it gets stored into the database in customer_entity_varchar table. This table contains following field:

value_id: It is just the auto-increament primary key for the table.
attribute_id: The attribute’s id which we had previosuly seen in eav_attribute table.
entity_id: It will contain the customer id.
value: It contains the value for the attribute which was filled while registration.

Now the value that was entered can be seen in admin panel as well. Navigate to Customers -> All Customers and click on edit for the newly created customer. Here, in the account information tab, you will find the field for this attribute and the value that was entered while creating the account as well.

That’s all for adding customer attribute programmatically.