Store finder - Dealer locator is Best

Monday, February 6, 2017

On 7:11 AM by Trọng Huỳnh Nguyễn   No comments
Magento 2 Create Payment Method proves that store admin has rights to generate as many payment methods as they need when your store is based on Magento 2 platform, an great era of ecommerce architecture. Depending on the customer’s requirement, you probably plug it in your list of the existing payment method. The additional payment methods surely bring the diversity of customer choice when they proceed to checkout on your site. On the other’s hands, multiple payment method is the great strategy to reach out the global marketplace.

In the tutorial, you will learn how to create own Payment Gateway integration in Magento 2 stores. After launching the new payment methods, you will find and configure it according the path Admin panel > Stores > Settings > Configuration > Sales > Payment Methods. There, admin possibly assigns a payment method to specific shipping method, this means they will work in pairs when enabling.

Step 1: Create payment method module



  1. Create file registration.php


<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
"Mageplaza_Payment",
__DIR__
);


  1. Declare module.xml file


<?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="Mageplaza_Payment" setup_version="0.1.0">
<sequence>
<module name="Magento_Sales"/>
<module name="Magento_Payment"/>
<module name="Magento_Checkout"/>
<module name="Magento_Directory" />
<module name="Magento_Config" />
</sequence>
</module>
</config>


This module have to run after the Magento_Sales, Magento_Payment, Magento_Checkout, Magento_Directory, and Magento_Config. So we add depends (sequence) them like above block of code.

Step 2: Declare payment method module


  1. Now we create file payment.xml file in etc folder etc/payment.xml


<?xml version="1.0" ?>
<payment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Payment:etc/payment.xsd">
<groups>
<group id="offline">
<label>Offline Payment Methods</label>
</group>
</groups>
<methods>
<method name="simple">
<allow_multiple_address>1</allow_multiple_address>
</method>
</methods>
</payment>


  1. Create config.xml file in etc folder.


<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<payment>
<simple>
<active>1</active>
<model>Mageplaza\Payment\Model\Payment\Simple</model>
<order_status>pending</order_status>
<title>Simple</title>
<allowspecific>0</allowspecific>
<group>Offline</group>
</simple>
</payment>
</default>
</config>


In this file, we declare Model of this payment method, we calle Simple model.
  1. Create Simple model file


Create file Model/Payment/Simple.php

<?php
namespace Mageplaza\Payment\Model\Payment;
class Simple extends \Magento\Payment\Model\Method\Cc
InfoInterface $payment
* @param float $amount
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
* @api
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/

public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)

if (!$this->canAuthorize())
throw new \Magento\Framework\Exception\LocalizedException(__("The authorize action is not available."));

return $this;

/**
* Capture payment abstract method
*
* @param \Magento\Framework\DataObject



This model includes basic functions such as - authorize(): Authorize the payment e.g: card - capture(): Capture money from a customer - refund(): Chargeback money to the customer.

Step 3: Display payment method in checkout page


In previous steps, we talked about declare module, config and model files. Now we need to display this Simple payment method in checkout page.
  1. Create layout file: view/frontend/layout/checkout_index_index.xml


<?xml version="1.0" ?>
<pagepage layout="1column" 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="billing-step" xsi:type="array">
<item name="children" xsi:type="array">
<item name="payment" xsi:type="array">
<item name="children" xsi:type="array">
<item name="renders" xsi:type="array">
<item name="children" xsi:type="array">
<item name="offline-payments" xsi:type="array">
<item name="component" xsi:type="string">Magento_OfflinePayments/js/view/payment/offline-payments</item>
<item name="methods" xsi:type="array">
<item name="simple" xsi:type="array">
<item name="isBillingAddressRequired" xsi:type="boolean">true</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</body>
</pagepage>


  1. Create js files to load KO template in checkout page


define(
[
"uiComponent",
"Magento_Checkout/js/model/payment/renderer-list"
],
function (Component,
rendererList)
"use strict";
rendererList.push(

type: "simple",
component: "Mageplaza_Payment/js/view/payment/method-renderer/simple-method"

);
return Component.extend();

);


In this file, it call another component: js/view/payment/method-renderer/simple-method

Now we need to create /view/frontend/web/js/view/payment/method-renderer/simple-method.js

define(
[
"Magento_Checkout/js/view/payment/default"
],
function (Component)
"use strict";
return Component.extend(
defaults:
template: "Mageplaza_Payment/payment/simple"
,
getMailingAddress: function ()
return window.checkoutConfig.payment.checkmo.mailingAddress;
,
);

);


  1. Finally, create KO template file /view/frontend/web/template/payment/simple.html


<div class="payment-method" data-bind="css: "_active": (getCode() == isChecked())">
<div class="payment-method-title field choice">
<input type="radio"
name="payment[method]"
class="radio"
data-bind="attr: "id": getCode(), value: getCode(), checked: isChecked, click: selectPaymentMethod, visible: isRadioButtonVisible()"/>
<label data-bind="attr: "for": getCode()" class="label"><span data-bind="text: getTitle()"></span></label>
</div>
<div class="payment-method-content">
<!-- ko foreach: getRegion("messages") -->
<!-- ko template: getTemplate() --><!-- /ko -->
<!--/ko-->
<div class="payment-method-billing-address">
<!-- ko foreach: $parent.getRegion(getBillingAddressFormName()) -->
<!-- ko template: getTemplate() --><!-- /ko -->
<!--/ko-->
</div>
<div class="checkout-agreements-block">
<!-- ko foreach: $parent.getRegion("before-place-order") -->
<!-- ko template: getTemplate() --><!-- /ko -->
<!--/ko-->
</div>
<div class="actions-toolbar">
<div class="primary">
<button class="action primary checkout"
type="submit"
data-bind="
click: placeOrder,
attr: title: $t("Place Order"),
css: disabled: !isPlaceOrderActionAllowed(),
enable: (getCode() == isChecked())
"

disabled>
<span data-bind="i18n: "Place Order""></span>
</button>
</div>
</div>
</div>
</div>


You maybe also interested in Magento 2 Create Shipping Methods to custom the shipping methods as expected.

https://www.mageplaza.com/magento-2-create-payment-method/
On 7:10 AM by Trọng Huỳnh Nguyễn   No comments
Magento 2 Create Payment Method proves that store admin has rights to generate as many payment methods as they need when your store is based on Magento 2 platform, an great era of ecommerce architecture. Depending on the customer’s requirement, you probably plug it in your list of the existing payment method. The additional payment methods surely bring the diversity of customer choice when they proceed to checkout on your site. On the other’s hands, multiple payment method is the great strategy to reach out the global marketplace.

In the tutorial, you will learn how to create own Payment Gateway integration in Magento 2 stores. After launching the new payment methods, you will find and configure it according the path Admin panel > Stores > Settings > Configuration > Sales > Payment Methods. There, admin possibly assigns a payment method to specific shipping method, this means they will work in pairs when enabling.

Step 1: Create payment method module



  1. Create file registration.php


<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
"Mageplaza_Payment",
__DIR__
);


  1. Declare module.xml file


<?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="Mageplaza_Payment" setup_version="0.1.0">
<sequence>
<module name="Magento_Sales"/>
<module name="Magento_Payment"/>
<module name="Magento_Checkout"/>
<module name="Magento_Directory" />
<module name="Magento_Config" />
</sequence>
</module>
</config>


This module have to run after the Magento_Sales, Magento_Payment, Magento_Checkout, Magento_Directory, and Magento_Config. So we add depends (sequence) them like above block of code.

Step 2: Declare payment method module


  1. Now we create file payment.xml file in etc folder etc/payment.xml


<?xml version="1.0" ?>
<payment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Payment:etc/payment.xsd">
<groups>
<group id="offline">
<label>Offline Payment Methods</label>
</group>
</groups>
<methods>
<method name="simple">
<allow_multiple_address>1</allow_multiple_address>
</method>
</methods>
</payment>


  1. Create config.xml file in etc folder.


<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<payment>
<simple>
<active>1</active>
<model>Mageplaza\Payment\Model\Payment\Simple</model>
<order_status>pending</order_status>
<title>Simple</title>
<allowspecific>0</allowspecific>
<group>Offline</group>
</simple>
</payment>
</default>
</config>


In this file, we declare Model of this payment method, we calle Simple model.
  1. Create Simple model file


Create file Model/Payment/Simple.php

<?php
namespace Mageplaza\Payment\Model\Payment;
class Simple extends \Magento\Payment\Model\Method\Cc
InfoInterface $payment
* @param float $amount
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
* @api
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/

public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)

if (!$this->canCapture())
throw new \Magento\Framework\Exception\LocalizedException(__("The capture action is not available."));

return $this;

/**
* Refund specified amount for payment
*
* @param \Magento\Framework\DataObject



This model includes basic functions such as - authorize(): Authorize the payment e.g: card - capture(): Capture money from a customer - refund(): Chargeback money to the customer.

Step 3: Display payment method in checkout page


In previous steps, we talked about declare module, config and model files. Now we need to display this Simple payment method in checkout page.
  1. Create layout file: view/frontend/layout/checkout_index_index.xml


<?xml version="1.0" ?>
<pagepage layout="1column" 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="billing-step" xsi:type="array">
<item name="children" xsi:type="array">
<item name="payment" xsi:type="array">
<item name="children" xsi:type="array">
<item name="renders" xsi:type="array">
<item name="children" xsi:type="array">
<item name="offline-payments" xsi:type="array">
<item name="component" xsi:type="string">Magento_OfflinePayments/js/view/payment/offline-payments</item>
<item name="methods" xsi:type="array">
<item name="simple" xsi:type="array">
<item name="isBillingAddressRequired" xsi:type="boolean">true</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</body>
</pagepage>


  1. Create js files to load KO template in checkout page


define(
[
"uiComponent",
"Magento_Checkout/js/model/payment/renderer-list"
],
function (Component,
rendererList)
"use strict";
rendererList.push(

type: "simple",
component: "Mageplaza_Payment/js/view/payment/method-renderer/simple-method"

);
return Component.extend();

);


In this file, it call another component: js/view/payment/method-renderer/simple-method

Now we need to create /view/frontend/web/js/view/payment/method-renderer/simple-method.js

define(
[
"Magento_Checkout/js/view/payment/default"
],
function (Component)
"use strict";
return Component.extend(
defaults:
template: "Mageplaza_Payment/payment/simple"
,
getMailingAddress: function ()
return window.checkoutConfig.payment.checkmo.mailingAddress;
,
);

);


  1. Finally, create KO template file /view/frontend/web/template/payment/simple.html


<div class="payment-method" data-bind="css: "_active": (getCode() == isChecked())">
<div class="payment-method-title field choice">
<input type="radio"
name="payment[method]"
class="radio"
data-bind="attr: "id": getCode(), value: getCode(), checked: isChecked, click: selectPaymentMethod, visible: isRadioButtonVisible()"/>
<label data-bind="attr: "for": getCode()" class="label"><span data-bind="text: getTitle()"></span></label>
</div>
<div class="payment-method-content">
<!-- ko foreach: getRegion("messages") -->
<!-- ko template: getTemplate() --><!-- /ko -->
<!--/ko-->
<div class="payment-method-billing-address">
<!-- ko foreach: $parent.getRegion(getBillingAddressFormName()) -->
<!-- ko template: getTemplate() --><!-- /ko -->
<!--/ko-->
</div>
<div class="checkout-agreements-block">
<!-- ko foreach: $parent.getRegion("before-place-order") -->
<!-- ko template: getTemplate() --><!-- /ko -->
<!--/ko-->
</div>
<div class="actions-toolbar">
<div class="primary">
<button class="action primary checkout"
type="submit"
data-bind="
click: placeOrder,
attr: title: $t("Place Order"),
css: disabled: !isPlaceOrderActionAllowed(),
enable: (getCode() == isChecked())
"

disabled>
<span data-bind="i18n: "Place Order""></span>
</button>
</div>
</div>
</div>
</div>


You maybe also interested in Magento 2 Create Shipping Methods to custom the shipping methods as expected.

https://www.mageplaza.com/magento-2-create-payment-method/

Saturday, February 4, 2017

On 11:54 PM by Trọng Huỳnh Nguyễn   No comments
Using VirtualType in Magento 2 is really necessary when you are running an virtual store based on Magento 2 platform? On Magento 2 platform, the di.xml supports for two types of node which are a node type and a node virtualtype, meanwhile virtualtype is considered as a perfect method instead of the type. The virtual tape allows inserting different dependencies into the existing classes but not making any change to other classes. With this tutorial, Mageplaza team will lead you to learn how to create and use VirtualType in Magento 2.

Creating VirtualType in Magento 2



Creating virtualtype in Magneto 2 means creating a sub-class for an existing class. All are available to help you create the virtualtype in magento 2.

<?php
class OurVirtualTypeName extends \Mageplaza\HelloWorld\Model\Virtualtype




The following script code is inserted to the module’s di.xml to create a virtualtype in Magento 2.

#File: app/code/Mageplaza/HelloWorld/etc/di.xml
<config>
<!-- ... -->
<virtualType name="ourVirtualTypeName" type="Mageplaza\HelloWorld\Model\Virtualtype">
</virtualType>
</config>


Namely, the nodes are placed under the main node and include two attributes: name and type. While the name attribute is mostly the universally special name of that node, the type attribute is the real PHP for the virtual type.

As you see, it is simple to give some descriptions about the virtual type. If you erase the cache and repeat the request, the output is still the same.

$ php bin/magento hw:tutorial-virtual-type
First, we"ll report on the Mageplaza\HelloWorld\Model\Example object
The Property $property_of_example_object
is an object
created with the class:
Mageplaza\HelloWorld\Model\Virtualtype

Next, we"re going to report on the Example object"s one property (an Virtualtype class)
The Property $property_of_argument1_object
is an object
created with the class:
Mageplaza\HelloWorld\Model\Argument2

Finally, we"ll report on an Virtualtype object, instantiated separate from Example
The Property $property_of_argument1_object
is an object
created with the class:
Mageplaza\HelloWorld\Model\Argument2


Using Virtual Type in Magento 2


The functionality of a virtual type is replacing the position of PHP classes and you wouldn’t have to use a configuration as the below if you wanted to custom the argument injected into the Example class’s constructor.

#File: app/code/Mageplaza/HelloWorld/etc/di.xml 
<config>
<!-- ... -->
<virtualType name="ourVirtualTypeName" type="Mageplaza\HelloWorld\Model\Virtualtype">
</virtualType>

<type name="Mageplaza\HelloWorld\Model\Example">
<arguments>
<argument name="the_object" xsi:type="object">Some\Other\Class</argument>
</arguments>
</type>

</config>


However, applying that command will lead you to the following error even though you have already cleaned your cache.

$ php bin/magento hw:tutorial-virtual-type

[ReflectionException]
Class Some\Other\Class does not exist


Let’s move to a smart way with the virtual type. This means Some\Other\Class is recovered by ourVirtualTypeName. You can ensure that no error is caused unless you call the command with the above in place.

#File: app/code/Mageplaza/HelloWorld/etc/di.xml 
<config>
<!-- ... -->
<virtualType name="ourVirtualTypeName" type="Mageplaza\HelloWorld\Model\Virtualtype">
</virtualType>

<type name="Mageplaza\HelloWorld\Model\Example">
<arguments>
<argument name="the_object" xsi:type="object">ourVirtualTypeName</argument>
</arguments>
</type>

</config>


Thanks for your reading and I hope that your business become more perfect with our guides of creating and using virtual types.

https://www.mageplaza.com/using-virtual-type-magento-2.html
On 11:54 PM by Trọng Huỳnh Nguyễn   No comments
Using VirtualType in Magento 2 is really necessary when you are running an virtual store based on Magento 2 platform? On Magento 2 platform, the di.xml supports for two types of node which are a node type and a node virtualtype, meanwhile virtualtype is considered as a perfect method instead of the type. The virtual tape allows inserting different dependencies into the existing classes but not making any change to other classes. With this tutorial, Mageplaza team will lead you to learn how to create and use VirtualType in Magento 2.

Creating VirtualType in Magento 2



Creating virtualtype in Magneto 2 means creating a sub-class for an existing class. All are available to help you create the virtualtype in magento 2.

<?php
class OurVirtualTypeName extends \Mageplaza\HelloWorld\Model\Virtualtype




The following script code is inserted to the module’s di.xml to create a virtualtype in Magento 2.

#File: app/code/Mageplaza/HelloWorld/etc/di.xml
<config>
<!-- ... -->
<virtualType name="ourVirtualTypeName" type="Mageplaza\HelloWorld\Model\Virtualtype">
</virtualType>
</config>


Namely, the nodes are placed under the main node and include two attributes: name and type. While the name attribute is mostly the universally special name of that node, the type attribute is the real PHP for the virtual type.

As you see, it is simple to give some descriptions about the virtual type. If you erase the cache and repeat the request, the output is still the same.

$ php bin/magento hw:tutorial-virtual-type
First, we"ll report on the Mageplaza\HelloWorld\Model\Example object
The Property $property_of_example_object
is an object
created with the class:
Mageplaza\HelloWorld\Model\Virtualtype

Next, we"re going to report on the Example object"s one property (an Virtualtype class)
The Property $property_of_argument1_object
is an object
created with the class:
Mageplaza\HelloWorld\Model\Argument2

Finally, we"ll report on an Virtualtype object, instantiated separate from Example
The Property $property_of_argument1_object
is an object
created with the class:
Mageplaza\HelloWorld\Model\Argument2


Using Virtual Type in Magento 2


The functionality of a virtual type is replacing the position of PHP classes and you wouldn’t have to use a configuration as the below if you wanted to custom the argument injected into the Example class’s constructor.

#File: app/code/Mageplaza/HelloWorld/etc/di.xml 
<config>
<!-- ... -->
<virtualType name="ourVirtualTypeName" type="Mageplaza\HelloWorld\Model\Virtualtype">
</virtualType>

<type name="Mageplaza\HelloWorld\Model\Example">
<arguments>
<argument name="the_object" xsi:type="object">Some\Other\Class</argument>
</arguments>
</type>

</config>


However, applying that command will lead you to the following error even though you have already cleaned your cache.

$ php bin/magento hw:tutorial-virtual-type

[ReflectionException]
Class Some\Other\Class does not exist


Let’s move to a smart way with the virtual type. This means Some\Other\Class is recovered by ourVirtualTypeName. You can ensure that no error is caused unless you call the command with the above in place.

#File: app/code/Mageplaza/HelloWorld/etc/di.xml 
<config>
<!-- ... -->
<virtualType name="ourVirtualTypeName" type="Mageplaza\HelloWorld\Model\Virtualtype">
</virtualType>

<type name="Mageplaza\HelloWorld\Model\Example">
<arguments>
<argument name="the_object" xsi:type="object">ourVirtualTypeName</argument>
</arguments>
</type>

</config>


Thanks for your reading and I hope that your business become more perfect with our guides of creating and using virtual types.

https://www.mageplaza.com/using-virtual-type-magento-2.html
On 12:43 PM by Trọng Huỳnh Nguyễn   No comments
Creating composer.json in Magento 2 brings you more and more convenience to manage components better that used in your project. This topic is the well-documented instruction to assist the store owners to be familiar with the composer.json file.

Overview of Magento 2 Composer



Composer is a dependency management for PHP and in Magento 2, the composer plays the role in gathering components and product editions. When using the Magento 2 composer, there is no requirement of storing all code of each component, and you only need to create composer.json file and declare the needed components in that file instead that. Immediately, the composer will auto-define and take the corresponding data of those components to run for your project.

Here are some situations of the Magento 2 components including composer.json file:
  • When a component contains composer.json file, and needs to install via magneto 2 composer as well (like from Packagist, the Magento Marketplace, or another source), the Component Manager can update, uninstall, enable, or disable the component.

  • When a component still includes composer.json file but doesn’t need to install via Magento 2 composer such as the customization of written codes by a developer, the Component Manager can still enable or disable the component.


So, it is easier for you to run the project smoothly when your component’s root directory is added the composer.json file.

Explanation of composer.json files


Both Magento 2 components and product editions can work with composer.json files, specifically:

Root


  • Location: composer.json

  • Name: magento/magento2ce

  • Type: project


This is the main composer.json file. It can be used as a template for every root composer.json file to call dependencies on third-party components.

CE project


  • Location: composer.json

  • Name: magento/project-community-edition

  • Type: project


This file is used for the Magento Community Edition project. It allows calling the dependencies on the Magento product and the class autoloader. In addition, you can use it to deploy Magento using Composer via Magento system integrators.

CE product


  • Location: composer.json

  • Name: magento/product-community-edition

  • Type: metapackage


This file is used for the Magento Community Edition product. It allows calling the dependencies on the Magento components (modules, themes, and so on) and third-party components. In addition, you can use it to deploy Magento using Composer via Magento system integrators.

Magento Framework


  • Location: lib/internal/Magento/Framework/composer.json

  • Name: magento/framework

  • Type: magento2-library


You only used this file for Magento framework.

Module


  • Locations:

app/code/<vendor-name>/<module-name>/composer.json

vendor/<vendor-name>/<module-name>/composer.json
  • Name: <vendor-name>/<package-name>

  • Type: magento2-module


The composer.json file will aid a module extension to call external dependencies when it needs to be functioned.

Theme


  • Locations:

app/design/frontend/<vendor-name>/<theme-name>/composer.json

app/design/adminhtml/<vendor-name>/<theme-name>/composer.json
  • Name: <vendor-name>/<package-name>

  • Type: magento2-theme


For a theme component, this file consists of parent theme dependencies which are used for the inheritance of the extension.

Language Package


  • Location: app/i18n/<vendor-name>/<language-code>/composer.json

  • Name: <vendor-name>/<package-name>

  • Type: magento2-language


The composer.json file for language packages have to include the correct ISO code that is suitable for the language code.

Kinds of the component in Magento 2 Composer


There are many kinds of Magento 2 component like a module, theme, or language package. However in the case of not being assigning to an exact category, that will be called generally as magento-2-component.

To determine the type of Magento 2 component, the system possibly combines the directories and files to the correct locations, that is based on the Magento 2 directory structure.

Naming conventions of the component in Magento 2 Composer


When creating Magneto 2 composer, you are compulsory to use the format of the namespace: <vendor-name>/<package-name>. Thus, in this part, we will learn the conventions of the vendor-name and package-name.

vendor-name


The vendor-name must be lowercase as the following format for Magento 2 extensions: magento2/*

In case that you want to submit the extension to Magento Marketplace, you have to use the name you have registered the account there. That name is injected into the composer.json file as the vendor-name part of the extension name. I have a specific illustrator for your approach:

 
"name": "mageplaza/magento-2-seo-extension",
"description": "Magento 2 SEO extension",
"require": ~7.0.0",
"mageplaza/core-m2": "dev-master"
,
"type": "magento2-module",
"version": "1.1.1",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"authors": [

"name": "Mageplaza",
"email": "hi@mageplaza.com",
"homepage": "https://www.mageplaza.com",
"role": "Leader"

],
"autoload":
"files": [
"registration.php"
],
"psr-4":
"Mageplaza\\Seo\\": ""





package-name


The package-name also contains the lowercase with multiple words, so Magento 2 Composer asks for separating them by dash as the following convention:

magento2/<type-prefix>-<suffix>[-<suffix>]...

In particular, type-prefix is a type of Magento 2 component and suffix is a thing to realize which component it is within that type.

That is all things you need to pay attention when creating a composer.json file in Magento 2. Please follow carefully and make yourself more comfortable with the composer.json file when running your projects.

https://www.mageplaza.com/magento-2-create-composer-json.html
On 12:43 PM by Trọng Huỳnh Nguyễn   No comments
Creating composer.json in Magento 2 brings you more and more convenience to manage components better that used in your project. This topic is the well-documented instruction to assist the store owners to be familiar with the composer.json file.

Overview of Magento 2 Composer



Composer is a dependency management for PHP and in Magento 2, the composer plays the role in gathering components and product editions. When using the Magento 2 composer, there is no requirement of storing all code of each component, and you only need to create composer.json file and declare the needed components in that file instead that. Immediately, the composer will auto-define and take the corresponding data of those components to run for your project.

Here are some situations of the Magento 2 components including composer.json file:
  • When a component contains composer.json file, and needs to install via magneto 2 composer as well (like from Packagist, the Magento Marketplace, or another source), the Component Manager can update, uninstall, enable, or disable the component.

  • When a component still includes composer.json file but doesn’t need to install via Magento 2 composer such as the customization of written codes by a developer, the Component Manager can still enable or disable the component.


So, it is easier for you to run the project smoothly when your component’s root directory is added the composer.json file.

Explanation of composer.json files


Both Magento 2 components and product editions can work with composer.json files, specifically:

Root


  • Location: composer.json

  • Name: magento/magento2ce

  • Type: project


This is the main composer.json file. It can be used as a template for every root composer.json file to call dependencies on third-party components.

CE project


  • Location: composer.json

  • Name: magento/project-community-edition

  • Type: project


This file is used for the Magento Community Edition project. It allows calling the dependencies on the Magento product and the class autoloader. In addition, you can use it to deploy Magento using Composer via Magento system integrators.

CE product


  • Location: composer.json

  • Name: magento/product-community-edition

  • Type: metapackage


This file is used for the Magento Community Edition product. It allows calling the dependencies on the Magento components (modules, themes, and so on) and third-party components. In addition, you can use it to deploy Magento using Composer via Magento system integrators.

Magento Framework


  • Location: lib/internal/Magento/Framework/composer.json

  • Name: magento/framework

  • Type: magento2-library


You only used this file for Magento framework.

Module


  • Locations:

app/code/<vendor-name>/<module-name>/composer.json

vendor/<vendor-name>/<module-name>/composer.json
  • Name: <vendor-name>/<package-name>

  • Type: magento2-module


The composer.json file will aid a module extension to call external dependencies when it needs to be functioned.

Theme


  • Locations:

app/design/frontend/<vendor-name>/<theme-name>/composer.json

app/design/adminhtml/<vendor-name>/<theme-name>/composer.json
  • Name: <vendor-name>/<package-name>

  • Type: magento2-theme


For a theme component, this file consists of parent theme dependencies which are used for the inheritance of the extension.

Language Package


  • Location: app/i18n/<vendor-name>/<language-code>/composer.json

  • Name: <vendor-name>/<package-name>

  • Type: magento2-language


The composer.json file for language packages have to include the correct ISO code that is suitable for the language code.

Kinds of the component in Magento 2 Composer


There are many kinds of Magento 2 component like a module, theme, or language package. However in the case of not being assigning to an exact category, that will be called generally as magento-2-component.

To determine the type of Magento 2 component, the system possibly combines the directories and files to the correct locations, that is based on the Magento 2 directory structure.

Naming conventions of the component in Magento 2 Composer


When creating Magneto 2 composer, you are compulsory to use the format of the namespace: <vendor-name>/<package-name>. Thus, in this part, we will learn the conventions of the vendor-name and package-name.

vendor-name


The vendor-name must be lowercase as the following format for Magento 2 extensions: magento2/*

In case that you want to submit the extension to Magento Marketplace, you have to use the name you have registered the account there. That name is injected into the composer.json file as the vendor-name part of the extension name. I have a specific illustrator for your approach:

 
"name": "mageplaza/magento-2-seo-extension",
"description": "Magento 2 SEO extension",
"require": ~5.6.0,
"type": "magento2-module",
"version": "1.1.1",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"authors": [

"name": "Mageplaza",
"email": "hi@mageplaza.com",
"homepage": "https://www.mageplaza.com",
"role": "Leader"

],
"autoload":
"files": [
"registration.php"
],
"psr-4":
"Mageplaza\\Seo\\": ""





package-name


The package-name also contains the lowercase with multiple words, so Magento 2 Composer asks for separating them by dash as the following convention:

magento2/<type-prefix>-<suffix>[-<suffix>]...

In particular, type-prefix is a type of Magento 2 component and suffix is a thing to realize which component it is within that type.

That is all things you need to pay attention when creating a composer.json file in Magento 2. Please follow carefully and make yourself more comfortable with the composer.json file when running your projects.

https://www.mageplaza.com/magento-2-create-composer-json.html

Friday, February 3, 2017

On 10:14 PM by Trọng Huỳnh Nguyễn   No comments
Magento 2 URL Rewrite programmatically is one of the awesome solutions for online retailers as you who want to really create a huge number of the traffics of your website. The purpose of rewriting the url is allowing you generating 301 redirects into Magento 2 by the programmatical way.

What is URL Redirect?



URL redirect is a popular term in SEO and it is used for navigating the visitors to any link store owners expect. There are two main type of redirect: 301 redirect and 302 redirect. Therefore, if you are wondering how to continue working with the existing visitor at the current site while you are planning to build a new site with more efficiencies, the best answer is creating the search redirection through the redirect 301.

Overview of Magento 2 URL Rewrite programmatically


  • Step 1 : Generate constructor file

  • Step 2 : Insert custom URL rewrite in execute method

Step 1 : Generate constructor file



/**
* @var \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory
*/
protected $_urlRewriteFactory;

/**
* @param Context $context
* @param \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory
*/
public function __construct(
Context $context,
\Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory
)
$this->_eavAttributeFactory = $eavAttributeFactory;
parent::__construct(
$context
);



Step 2 : Insert custom URL rewrite in execute method


Only if your original website is www.example.com/customModule/customController/customAction, you want to rewrite the URL by www.example.com/xyz, instead. Then you can run the following method:

$urlRewriteModel = $this->_urlRewriteFactory->create()
/* set current store id */
$urlRewriteModel->setStoreId(1);
/* this url is not created by system so set as 0 */
$urlRewriteModel->setIsSystem(0);
/* unique identifier - set random unique value to id path */
$urlRewriteModel->setIdPath(rand(1, 100000));
/* set actual url path to target path field */
$urlRewriteModel->setTargetPath("www.example.com/customModule/customController/customAction");
/* set requested path which you want to create */
$urlRewriteModel->setRequestPath("www.example.com/xyz");
/* set current store id */
$urlRewriteModel->save();


 

https://www.mageplaza.com/magento-2-url-rewrite-programmatically.html
On 10:14 PM by Trọng Huỳnh Nguyễn   No comments
Magento 2 URL Rewrite programmatically is one of the awesome solutions for online retailers as you who want to really create a huge number of the traffics of your website. The purpose of rewriting the url is allowing you generating 301 redirects into Magento 2 by the programmatical way.

What is URL Redirect?



URL redirect is a popular term in SEO and it is used for navigating the visitors to any link store owners expect. There are two main type of redirect: 301 redirect and 302 redirect. Therefore, if you are wondering how to continue working with the existing visitor at the current site while you are planning to build a new site with more efficiencies, the best answer is creating the search redirection through the redirect 301.

Overview of Magento 2 URL Rewrite programmatically


  • Step 1 : Generate constructor file

  • Step 2 : Insert custom URL rewrite in execute method

Step 1 : Generate constructor file



/**
* @var \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory
*/
protected $_urlRewriteFactory;

/**
* @param Context $context
* @param \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory
*/
public function __construct(
Context $context,
\Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory
)
$this->_eavAttributeFactory = $eavAttributeFactory;
parent::__construct(
$context
);



Step 2 : Insert custom URL rewrite in execute method


Only if your original website is www.example.com/customModule/customController/customAction, you want to rewrite the URL by www.example.com/xyz, instead. Then you can run the following method:

$urlRewriteModel = $this->_urlRewriteFactory->create()
/* set current store id */
$urlRewriteModel->setStoreId(1);
/* this url is not created by system so set as 0 */
$urlRewriteModel->setIsSystem(0);
/* unique identifier - set random unique value to id path */
$urlRewriteModel->setIdPath(rand(1, 100000));
/* set actual url path to target path field */
$urlRewriteModel->setTargetPath("www.example.com/customModule/customController/customAction");
/* set requested path which you want to create */
$urlRewriteModel->setRequestPath("www.example.com/xyz");
/* set current store id */
$urlRewriteModel->save();


 

https://www.mageplaza.com/magento-2-url-rewrite-programmatically.html
On 8:53 PM by Trọng Huỳnh Nguyễn   No comments
Magento 2 registry is the next topic Mageplaza wants to introduce in the series of this module development. Magento 1, as well as Magento 2, authorize you to register global variable that supports the static registry method.

To implement that, maybe you used to work with Mage::register() and Mage::registry() in Magento 1, but now in Magento 2 platform, there is a difference in running the registry. You will be required to apply \Magento\Framework\Registry, that accepts the settings and the registry of the restored data. However, first of all, you need to learn how to create or use the own custom registry and also show you how to retrieve global Magento 2 registry objects like current product, category, cms page, cms block, etc.

And that is lucky because all of them will be referred here. The topic today will help you be familiar with Magento 2 registry objects.

Please follow the following code snippet:

How to get and set custom attribute in registry / register




/**
* @var \Magento\Framework\Registry
*/

protected $_registry;

/**
* ...
* ...
* @param \Magento\Framework\Registry $registry,
*/
public function __construct(
...,
...,
\Magento\Framework\Registry $registry,
...
)
$this->_registry = $registry;
...
...


/**
* Setting custom variable in registry to be used
*
*/

public function setCustomVariable()

$this->registry->register("custom_var", "Added Value");


/**
* Retrieving custom variable from registry
* @return string
*/
public function getCustomVariable()

return $this->registry->registry("custom_var");




How to get registry of current Product, Category, CMS Page




/**
* @var \Magento\Framework\Registry
*/

protected $_registry;

/**
* ...
* ...
* @param \Magento\Framework\Registry $registry,
*/
public function __construct(
...,
...,
\Magento\Framework\Registry $registry,
...
)
$this->_registry = $registry;
...
...


/**
* Return catalog product object
*
* @return \Magento\Catalog\Model\Product
*/

public function getProduct()

return $this->_registry->registry("current_product");


/**
* Return catalog current category object
*
* @return \Magento\Catalog\Model\Category
*/

public function getCurrentCategory()

return $this->_registry->registry("current_category");



/**
* Return catalog current cms page object
*
*/
public function getCurrentCategory()

return $this->_registry->registry("current_cms_page");




With this instruction, you can achieve the best results of Magento 2 registry. Thanks for your watching and hope you are happy with it.

Happy coding!

https://www.mageplaza.com/magento-2-registry-register.html
On 8:52 PM by Trọng Huỳnh Nguyễn   No comments
Magento 2 registry is the next topic Mageplaza wants to introduce in the series of this module development. Magento 1, as well as Magento 2, authorize you to register global variable that supports the static registry method.

To implement that, maybe you used to work with Mage::register() and Mage::registry() in Magento 1, but now in Magento 2 platform, there is a difference in running the registry. You will be required to apply \Magento\Framework\Registry, that accepts the settings and the registry of the restored data. However, first of all, you need to learn how to create or use the own custom registry and also show you how to retrieve global Magento 2 registry objects like current product, category, cms page, cms block, etc.

And that is lucky because all of them will be referred here. The topic today will help you be familiar with Magento 2 registry objects.

Please follow the following code snippet:

How to get and set custom attribute in registry / register




/**
* @var \Magento\Framework\Registry
*/

protected $_registry;

/**
* ...
* ...
* @param \Magento\Framework\Registry $registry,
*/
public function __construct(
...,
...,
\Magento\Framework\Registry $registry,
...
)
$this->_registry = $registry;
...
...


/**
* Setting custom variable in registry to be used
*
*/

public function setCustomVariable()

$this->registry->register("custom_var", "Added Value");


/**
* Retrieving custom variable from registry
* @return string
*/
public function getCustomVariable()

return $this->registry->registry("custom_var");




How to get registry of current Product, Category, CMS Page




/**
* @var \Magento\Framework\Registry
*/

protected $_registry;

/**
* ...
* ...
* @param \Magento\Framework\Registry $registry,
*/
public function __construct(
...,
...,
\Magento\Framework\Registry $registry,
...
)
$this->_registry = $registry;
...
...


/**
* Return catalog product object
*
* @return \Magento\Catalog\Model\Product
*/

public function getProduct()

return $this->_registry->registry("current_product");


/**
* Return catalog current category object
*
* @return \Magento\Catalog\Model\Category
*/

public function getCurrentCategory()

return $this->_registry->registry("current_category");



/**
* Return catalog current cms page object
*
*/
public function getCurrentCategory()

return $this->_registry->registry("current_cms_page");




With this instruction, you can achieve the best results of Magento 2 registry. Thanks for your watching and hope you are happy with it.

Happy coding!

https://www.mageplaza.com/magento-2-registry-register.html