:::: MENU ::::

Magento 2 add canonical link

This tutorial is about Magento 2 add canonical link .i.e how to add a canonical link to a page through it’s layout file?. I assume your question is,is it possible?
So the answer is, Yes.It is possible. And in this tutorial i will try to explain it briefly and in a simple way.

Lets suppose you have a custom page and you want to add a canonical link to it in the layout file. Well,in magento 2 we can specify meta data in the head node.Similarly it is possible to add the canonical link in the layout as well.

It is quite doable and in a very easy way.

Just put the below lines of code in your own code.

<head>
      <link rel="canonical" src="pageurl" src_type="url"/>
</head>

Furthermore change the ‘pageurl’ as per your requirements. And place it in custom layout section in admin , if you want the functionality using admin custom layout section.

That’s it from this tutorial. I hope it serves the purpose.
Since these are learning tutorials,please feel free to drop any suggestions or queries in comments section. That will definitely be highly appreciated.

Author: Qaisar Satti
Category: Magento 2
Last Modified: October 22, 2018


Cookie in magento 2

This tutorial is about Use or Set cookie in magento 2. i.e how to set or use cookie in magento 2?. Suppose you want to set a value to the cookie in a phtml file and need to get it’s value in another phtml file.

There are many ways in magento 2.I will try to discuss some in this tutorial.
Certainly magento uses jquery and it is possible to set and get the values using jQuery.

First of all lets have a look at our first setp. i.e

Set the cookies

You can set the cookies using below code:

<script>
    require([
        'jquery',
        'mage/cookies'
    ], function ($) {
       $.cookie('cookie_name', 'value', { path: '/' });//Set the cookies
    });
</script>

Please note that if you want to get the value from another page, notice the path setting { path: ‘/’ }!

Because cookies are only accessible to the specified path and any subpaths.

Similarly to get cookies:

Get Cookies

<script>
    require([
        'jquery',
        'mage/cookies'
    ], function ($) {
        var temp = $.cookie('cookie_name');//Get the cookies
    });
</script>

Similarly another piece of code is as below:

<script>
    require(
        [ 'jquery'],function($){
            //set cookie
            $.cookie('customname', 'customvalue', { expires: 365, path: '/' });

            //get cookie
            getCookieValue(customname)
        });

        function getCookieValue(cookiename) {
                var name = cookiename + "=";
                var cookieSplit = document.cookie.split(';');
                for(var i = 0; i <cookieSplit.length; i++) {
                    var a = cookieSplit[i];
                    while (a.charAt(0)==' ') {
                        a = a.substring(1);
                    }
                    if (a.indexOf(name) == 0) {
                        return a.substring(name.length,a.length);
                    }
                }
                return "";
            }
</script>

Furthermore there are other way too to do so. I am going to discuss them below. Wil try to keep it short and precise.

Create Cookie

Create a test controller to create:

app/code/QaisarSatti/HelloWorld/Controller/Cookie/Addcookie.php

<?php
namespace QaisarSatti\HelloWorld\Controller\Cookie;
class Addcookie extends \Magento\Framework\App\Action\Action
{
const COOKIE_NAME = 'test';
const COOKIE_DURATION = 86400; // lifetime in seconds
/**
* @var \Magento\Framework\Stdlib\CookieManagerInterface
*/

protected $_cookieManager;
/**
* @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
*/

protected $_cookieMetadataFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
* @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
*/

public function __construct(
     \Magento\Framework\App\Action\Context $context,
     \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
     \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
)
{
     $this->_cookieManager = $cookieManager;
     $this->_cookieMetadataFactory = $cookieMetadataFactory;
     parent::__construct($context);
}
public function execute()
{
     $metadata = $this->_cookieMetadataFactory
         ->createPublicCookieMetadata()
         ->setDuration(self::COOKIE_DURATION);
     $this->_cookieManager->setPublicCookie(
         self::COOKIE_NAME,
         'YOUR COOKIE VALUE',
         $metadata
     );
     echo('Create Cookie');
}
}

Similarly to Read cookie

Read Cookie

Create a Readcookie.php controller to read cookie

app/code/QaisarSatti/HelloWorld/Controller/Cookie

<?php
namespace QaisarSatti\HelloWorld\Controller\Cookie;
class Readcookie extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\Stdlib\CookieManagerInterface
*/

protected $_cookieManager;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
*/

public function __construct(
     \Magento\Framework\App\Action\Context $context,
     \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
)
{
     $this->_cookieManager = $cookieManager;
     parent::__construct($context);
}
public function execute()
{
     $cookieValue = $this->_cookieManager->getCookie(\[Name_Space]\[Your_Module]\Controller\Cookie\Addcookie::COOKIE_NAME);
     echo($cookieValue);
}
}

Furthermore Delete Cookie.

Delete Cookie

Create a Deletecookie.php controller to create cookie.

<?php
namespace QaisarSatti\HelloWorld\Controller\Cookie;
class Deletecookie extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\Stdlib\CookieManagerInterface
*/

protected $_cookieManager;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
*/

public function __construct(
     \Magento\Framework\App\Action\Context $context,
     \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
)
{
     $this->_cookieManager = $cookieManager;
     parent::__construct($context);
}
public function execute()
{
     $this->_cookieManager->deleteCookie(
         \[Name_Space]\[Your_Module]\Controller\Cookie\Addcookie::COOKIE_NAME
     );
     echo('DELETED');
}
}

Hence above mentioned steps are the shortest process for you to Use Cookie in Magento 2. I have tried to provide you the code with simple logic.

That’s it from this tutorial. I hope it serves the purpose.

Since these are learning tutorials,please feel free to drop any suggestions or queries in comments section. That will definitely be highly appreciated.

Author: Qaisar Satti
Category: Magento 2
Last Modified: October 22, 2018


Magento 2 shipping extensions

Today we will discuss about Free magento 2 shipping extensions.In this tutorial we will discuss available free magento 2 shipping extensions.This tutorial just have information of free extensions that are available on magento marketplace or any other extension provider.I have tried to list a few.So let’s start with our list.

Free magento 2 Shipping extensions

Magento provides its own shipping methods.Yet here we will discuss free magento shipping extensions.Because these are available for free so anyone can use them.Lets have a look.

Shipping Flat Rate5

This is an upgraded extension that allows the merchant to offer a single flat rate shipping option to their Magento store by serving multiple flat rate methods based on different prices and conditions. This extension overcomes the limitation of having only one flat rate shipping method available in Magento.

Key features:

  • Complete backend configuration
  • Create 5 flat rate shipping methods with different prices and conditions
  • Magento Multi-Store full support and Multiple Currencies as well
  • Select admin section of shipping methods
  • Shipping methods a enable/disable setting
  • Set minimum/maximum order amount for free shipping feature
  • Flat rate shipping methods in fixed manner
  • Dispaly diferent error message for specific shipping method.

Extension Link

ShipRocket

It is a unique E-commerce Shipping Solution which gives access to multiple courier partners on a single platform. With Shiprocket, merchants can automate their shipping seamlessly without any technical know how.
Shiprcoket offers four plans:

  • Lite
  • Basic
  • Advanced
  • Pro

Key features:

  • Automatic Order Sync
  • Automatic shipping status sync
  • Catalog and Inventory sync

Extension Link

MatrixRate Table Rate Shipping

The MatrixRate shipping extension enables you to offer multiple shipping options to customers based on their location.

Key features:

  • The most popular multiple table rates extension for Magento
  • Display multiple delivery methods
  • Customize shipping rates based on destination
  • Customize shipping rates based on cart weight, subtotal or quantity
  • Pattern-based zipcode filtering to determine shipping charges
  • Supports zipcode numerical ranges (e.g. 5000-6000)
  • Proven success across the world – used widely in the UK, Europe, US and Australia
  • Excellent documentation, including walkthrough examples

Extension Link

bpost Shipping Manager

This module implements 5 new shipping methods in Magento. The merchant can create bpost shipping and return labels in the administration panel of Magento. These labels can be generated in bulk or for each order separately.The tracking numbers are automatically filled in.

Key features:

  • Receive your parcel at home or at the office (national or international)
  • Over 1.250 locations (pick-up points) nearby home or the office
  • Pick-up your parcel whenever you want, thanks to the 24/7 service of bpost (parcel locker)
  • Pick-up your parcel on your own shops thanks to Click&Collect delivery method

Extension Link

Shipping Easy

Your orders will automatically download from Magento to ShippingEasy, where powerful automation streamlines carrier/service selection and printing of shipping labels, packing slips, and pick lists, with carrier and tracking information automatically sent back to Magento. Includes full support for leading carriers: USPS, UPS, FedEx, and DHL.

Key features:

  • Savings add up quickly using ShippingEasy
  • Save time on multichannel order fulfillment
  • Streamline inventory processes with Inventory Management
  • Increase sales with Customer Marketing
  • A powerful enterprise platform for high-volume shippers

Extension Link

Advanced Shipping

With Advanced Shipping, you can set shipping fees by using almost all the variables available, such as destination, weight, price, quantity, categories, product attributes, item options, the customer group, customer attributes, coupon codes, Magento custom variables, date, etc.

Key features:

  • Manage one shipping mode or carrier
  • Several conditional shipping methods
  • Shipping fees based on destination, weight, price, quantity
  • Shipping fees based on the customer group or customer attribute
  • Shipping fees based on categories, product attributes or item options
  • Shipping fees based on custom variables, the coupon code, the date
  • Shipping fees based on formulas combining all the variables you need
  • Filtering zip codes with regular expressions
  • Extension used in many countries in the world
  • Documentation in english

Extension Link

Similarly there are many other extensions available. I tried to list a few. I hope this tutorial help you in any way.

Please feel free to drop any suggestions or queries in comments section. It will be highly appreciated.

Author: Qaisar Satti
Category: Magento 2
Last Modified: October 22, 2018


Magento shipping extensions

Today we will discuss about Free magento shipping extensions.In this tutorial we will discuss available free magento shipping extensions.This tutorial just have information of free extensions that are available on magento marketplace or any other extension provider.I have tried to list a few.So let’s start with our list.

Free magento Shipping extensions

Magento provides its own shipping methods.Yet here we will discuss free magento shipping extensions.Because these are available for free so anyone can use them.Lets have a look.

ShipperHQ

ShipperHQ provides you the options to fully control and customize your shipping rates like never before. You define the carriers to use, the shipping methods that apply, and the unique rules of your own rate structure.

Key features:

  • Multi Carrier Support
  • Fully Customizable Rate Management
  • Dimensional Shipping
  • Multi-Origin Shipping
  • Date & Time

Extension Link

R+L Carriers Freight Shipping

R+L Carriers is an American private freight shipping company who provides freight services such as LTL, Truckload, Logistics, and more.Freight shipping is used when large and heavy items need to be shipped.This extension provides the integration of R+L Carriers Freight to show freight shipping quotes and delivery tracking on your Magento® store.This extension fetch live freight rates from R+L Carriers using their available Rate Quote API.

Key features:

  • Retrieve live rates from the R+L Carriers freight
  • Supports accessorial services and can be manage from the admin
  • Set Min/Max package weight to show freight rates from R+L Carriers
  • Support all product types
  • Support Magento® multi store functionality
  • Assign a store freight class or assign freight classes per product basis

Extension Link

ShipStation

ShipStation is a powerful, yet easy to use web-based, multi-channel, multi-carrier shipping solution designed to facilitate the order fulfillment process for your Magento store. ShipStation downloads your orders in real-time, allows you to create USPS, UPS, FedEx, DHL, Royal Mail, Canada Post, Australia Post, and more shipping labels and packing slips in batch, and communicates tracking information to your customers.

Key features:

  • Print Shipping Labels and Packing Slips in Batch
  • Consolidate Orders from Multiple Channels
  • Unlimited Selling Channels
  • No Software to Install
  • Free USPS Provider Account

Extension Link

Shipwire Fulfillment Platform

This extension provides Magento Enterprise and Community edition merchants with e-commerce order fulfillment, shipping software, and cloud-based logistics services from fulfillment centers around the world.Also the Shipwire Platform helps brands grow sales, expand into new markets, and delight their customers by eliminating shipping and storage hassles.

Key features:

  • Order entry sync
  • Order tracking
  • Inventory status
  • Shipping rates

Extension Link

Shipping Tracker

This versatile extension lets you customize the shipment tracking links provided to your customers. Shipping Tracker is easy to set up and supports up to five carriers as long as they provide a tracking URL and reference number. Shipping Tracker uses the Magento mechanism to add a tracking ID to a shipment. When you create a shipment, select a tracking carrier you have configured and add the tracking ID.

Key features:

  • Embeds shipment tracking links in shipping confirmation emails
  • Completely generic – not tied to any carrier
  • Supports up to five carriers at any one time
  • Features a dropdown menu with known tracking URLs from major shipping carriers
Extension Link

Similarly there are many other extensions available. I tried to list a few. I hope this tutorial help you in any way.

Please feel free to drop any suggestions or queries in comments section. It will be highly appreciated.

Author: Qaisar Satti
Category: Magento 1
Last Modified: October 22, 2018


Magento 2 add custom block below shipping method

Hello, this tutorial is about Magento 2 add custom block below shipping method.First of all i assume that you are an advance learner of magento 2.Hence you are here to learn how to add custom block below shipping method in one page check out in magento 2.Above all if you are not familiar with magento 2 please start from basics.

lets start our tutorial and focus on our topic. For example if you want to add custom block below shipping methods. And aslo this custom block should be inside of shipping method form. In this tutorial i will discuss in detail how we can do it. It is a four to five step method.And i would try to explain it in a simple way. So that you can do it efficiently.

The first step is to Declare module’s checkout dependency.

Declare module’s checkout dependency

First of all find the directory to your module file

app/code/QaisarSatti/HelloWorld/etc/module.xml

and paste the below code

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="QaisarSatti_HelloWorld" setup_version="0.0.1" active="true">
        <sequence>
            <module name="Magento_Checkout"/>
        </sequence>
    </module>
</config>

Now we will move to the second step which is to Overwrite checkout layout.

Overwrite checkout layout

In the

app/code/QaisarSatti/HelloWorld/view/frontend/layout/checkout_index_index.xml

directory, paste the below code:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.root">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="checkout" xsi:type="array">
                            <item name="children" xsi:type="array">
                                <item name="steps" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="shipping-step" xsi:type="array">
                                            <item name="children" xsi:type="array">
                                                <item name="shippingAddress" xsi:type="array">
                                                    <item name="children" xsi:type="array">
                                                        <item name="shippingAdditional" xsi:type="array">
                                                            <item name="component" xsi:type="string">uiComponent</item>
                                                            <item name="displayArea" xsi:type="string">shippingAdditional</item>
                                                            <item name="children" xsi:type="array">
                                                                <item name="additional_block" xsi:type="array">
                                                                    <item name="component" xsi:type="string">QaisarSatti_HelloWorld/js/view/checkout/shipping/additional-block</item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

similarly the the third step is to Create JavaScript UI Component.

Create JavaScript UI Component

For your information JavaScript (with Knockout)manages checkout in Magento 2 .So for that you need to create a custom JS compnent. It will create the link between checkout UI component and your custom HTML template.

in the

app/code/QaisarSatti/HelloWorld/view/frontend/web/js/view/checkout/shipping/additional-block.js

directory, paste the below code

define([
    'uiComponent'

], function (Component) {
    'use strict';

    return Component.extend({
        defaults: {
            template: 'QaisarSatti_HelloWorld/checkout/shipping/additional-block'
        }
    });
});

Similarly we will move to the fourth step which is to Create HTML template.

Create HTML template

Now create the HTML template wich going to be displayed in checkout.
In the

app/code/QaisarSatti/HelloWorld/view/frontend/web/template/checkout/shipping/additional-block.html

directory,use below code

<div class="checkout-block" id="block-custom">
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
    <p>Cum sociis natoque penatibus et magnis dis parturient montes.</p>
</div>

Now the fifth and final step is to clear cache.

clear cache

Finally run following commands to clear cache:

php bin/magento cache:clean
php bin/magento setup:upgrade
chmod -R 777 var/*

That’s it in this tutorial. Certainly, i have tried to explain these codes and their logic in a simple way, i hope they help you learn in an easy and better way.

Since these tutorials are for learning purpose please feel free to drop any suggestions or queries in comments section. That will definitely be highly appreciated.

Author: Qaisar Satti
Category: Magento 2
Last Modified: October 12, 2018


Magento 2 Get base url in js

This tutorial is about magento 2 Get base url in js file. First of all i assume that you are familiar with magento 2 and hence you are at this level where you want to know how to get base url from js file in magento 2. Above all if you are not familiar with advance magento concepts and are looking for basics,please look for magento 2 basic tutorials.

So lets start our tutorial and focus on our topic. For example if you want to get base url from .js file and append it with your module controller link from. How can we do it? It is almost impossible to use block to get the base url.

Get base url

So we will look at the way how we can get base url. to do that,paste the following code in your routes.xml file

define([

   'mage/url'
], function (url) {

  var linkUrl = url.build('test/test/test');
  console.log(linkUrl);
});

Where frontname is your routes.xml file frontname.

Since it is necessary,you have to pass your frontname from routes.xml file instead of module name(namespace_module).

Furthermore you can lookup your routes.xml file from

app/code/QaisarSatti/HelloWorld/etc/frontend/routes.xml

It might work for some and might not for others.

Further more Add this code in phtml file:

<input type="hidden" value="<?php echo $this->getUrl(); ?>" id="baseUrl"/>

having done that you can use this base url in js file by id :

$("#baseUrl").val();

It is a quite useful and tested trick. So i thought to share it.

There is another approach for this task.Having said that it may too be useful.

You should pass url to js(widget).Url is a parameter of js widget.And put script below to your template where you want to.

<script type="text/x-magento-init">
{
    "*": {
        "Magento_Ui/js/core/app": {
            "components": {
                "yourWidget": {
                    "dataUrl": "<?php echo $block->getBaseUrl(); ?>"
                }
            }
        }
    }
}
</script>

Similarly in javascript widget, you can access to dataUrl by below code.

define(['uiComponent'], function(Component) {
    'use strict';

    return Component.extend({
        initialize: function() {
            console.log(this.dataUrl);
        }
    });
});

Another simple approach is

<script>
window.testUrl = <?php echo json_encode($block->getBaseUrl()); ?>
</script>

Using your custom js you can have easy access to window.testUrl global variable.

That’s it from this tutorial. I hope it serves the purpose.

Since these tutorials are for learning purpose please feel free to drop any suggestions or queries in comments section. That will definitely be highly appreciated.

Author: Qaisar Satti
Category: Magento 2
Last Modified: October 12, 2018


Use Country based region selection in admin form Magento 2

In this tutorial we will learn about a default functionality of magento,Use Country based region selection in admin form Magento 2 . This is a built in function of magento. Magento provides this default functionality in the admin panel ui-component.Suppose you want to add country and region fields in your custom module in admin section.Now we know what you want to do, so lets shift our focus to the method and code which will help us to accomplish our task.

Lets have a look at the code part.

Country based region selection magento

Below code will use this default magneto functionality in your admin panel ui component form.

<field name="country_id">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Magento\Directory\Model\Config\Source\Country</item>
        <item name="config" xsi:type="array">
            <item name="dataType" xsi:type="string">text</item>
            <item name="label" xsi:type="string" translate="true">Country</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="source" xsi:type="string">store</item>
            <item name="component" xsi:type="string">Magento_Ui/js/form/element/country</item>
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">true</item>
            </item>
        </item>
    </argument>
</field>
<field name="region">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="dataType" xsi:type="string">text</item>
            <item name="label" xsi:type="string" translate="true">State/Region</item>
            <item name="formElement" xsi:type="string">input</item>
            <item name="source" xsi:type="string">store</item>
            <item name="sortOrder" xsi:type="number">15</item>
            <item name="visible" xsi:type="boolean">false</item>
        </item>
    </argument>
</field>
<field name="region_id">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Magento\Directory\Model\ResourceModel\Region\Collection</item>
        <item name="config" xsi:type="array">
            <item name="dataType" xsi:type="string">text</item>
            <item name="label" xsi:type="string" translate="true">State/Region</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="source" xsi:type="string">store</item>
            <item name="sortOrder" xsi:type="number">20</item>
            <item name="customEntry" xsi:type="string">region</item>
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">true</item>
            </item>
            <item name="filterBy" xsi:type="array">
                <item name="target" xsi:type="string">${ $.provider }:${ $.parentScope }.country_id</item>
                <item name="field" xsi:type="string">country_id</item>
            </item>
        </item>
    </argument>
</field>

So you can populate the country field values, and by selecting country you can also get the regions by using above code.

That’s it from this tutorial,i have tried to explain this magento functionality in a simplest and easiest way.Therefore i hope this will help you in a better way.

Since these tutorials are for learning purpose please feel free to drop any suggestions or queries in comments section. That will definitely be highly appreciated.

Author: Qaisar Satti
Category: Magento 2
Last Modified: October 12, 2018


Pages:1234567...18