+ All Categories
Home > Technology > Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

Date post: 08-Jan-2017
Category:
Upload: joshua-warren
View: 2,439 times
Download: 6 times
Share this document with a friend
139
PRESENTED BY JOSHUA WARREN PRESENTED AT NORTHEAST PHP 2015 Magento 2 AN INTRODUCTION TO A MODERN PHP- BASED SYSTEM
Transcript
Page 1: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

PRESENTED BY JOSHUA WARREN

PRESENTED AT

NORTHEAST PHP 2015

Magento 2AN INTRODUCTION TO A MODERN PHP-

BASED SYSTEM

Page 2: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

MY EXPERIENCE

Page 3: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

My Experience

PHP Developer Since 1999

Founded Creatuity in 2008

Focused on the Magento platform

Magento 2 contributor

#NEPHP

Page 4: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

early adopter of both Magento 1 and Magento 2

#NEPHP

Page 5: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Frequent Magento presenter

#NEPHP

Page 6: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Active member of the #RealMagento community

#NEPHP

Page 7: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Involved in feedback and design discussions throughout the Magento 2 Developer Beta

#NEPHP

Page 8: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Wrote Writing the book on Magento 2

#NEPHP

Page 9: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

A BRIEF HISTORY OF MAGENTO

Photo courtesy of @YoavKutner

Page 10: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Magento 1 development began in 2007 by Varien, a PHP development

agency.

#NEPHP

Page 11: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

In 2007, osCommerce was state of the art.

#NEPHP

Page 12: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Cloud-based eCommerce systems didn’t exist.

#NEPHP

Page 13: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

PHP 5.2 was cutting-edge.

#NEPHP

Page 14: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Composer didn’t exist, and Zend Framework 1 was still in early beta.

#NEPHP

Page 15: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Magento 1 was built to resolve the pain points of osCommerce.

#NEPHP

Page 16: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Designed to be more flexible and to provide standardized ways to

customize the platform.

#NEPHP

Page 17: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

By 2010, Magento had been downloaded 1.5 million times.

#NEPHP

Page 18: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Attracted by Magento’s free, open-source approach, hundreds of

thousands of sites were launched using Magento 1.

#NEPHP

Page 19: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

There’s just one problem…

#NEPHP

Page 20: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Ecommerce development is hard.

#NEPHP

Page 21: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015
Page 22: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Lightly documented ecommerce development is even harder.

#NEPHP

Page 23: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015
Page 24: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Varien, now known as Magento Inc, is acquired by eBay in 2011.

#NEPHP

Page 25: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Work begins on Magento 2 almost immediately.

#NEPHP

Page 26: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Now the most widely-used eCommerce platform, powering over 250,000 sites, expectations are high

for the Magento 2 team.

#NEPHP

Page 27: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Fewer than 30 commits are made to Magento 2 in 2012.

#NEPHP

Page 28: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Internal priorities continue to shift, and finally at the end of 2014,

Magento 2 development is made public on Github.

#NEPHP

Page 29: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Magento commits to a release schedule for Magento 2, and

announces the acceptance of pull requests.

#NEPHP

Page 30: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

devdocs.magento.com launches with significant documentation of

Magento 2.

#NEPHP

Page 31: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Developer Beta is released in December 2014; Merchant Beta in

July 2015

#NEPHP

Page 32: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Production-ready release (‘general availability’) scheduled for Q4 2015

#NEPHP

Page 33: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

MAGENTO 2

github.com/magento/magento2

Page 34: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Feature parity with Magento 1 + UI/UX Improvements + focus on

resolving technical debt

#NEPHP

Page 35: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Technologies

#NEPHP

Page 36: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

Composer

composer create-project magento/product-community-edition --stability="beta" <installation directory name>

Page 37: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Each Magento 2 module is a separate Composer package

#NEPHP

Page 38: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

PSR-0 thru PSR-4

#NEPHP

Page 39: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Testing built in from the start. phpunit, selenium, JMeter, Jasmine

#NEPHP

Page 40: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

magento2/dev/tests/

#NEPHP

Page 41: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

HTML5, CSS3, LESS CSS Preprocessor, JQuery, RequireJS

#NEPHP

Page 42: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Components from Zend Framework 1, Zend Framework 2,

Symfony

#NEPHP

Page 43: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Technical Architecture

#NEPHP

Page 44: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Presentation Layer, Service Layer, Domain Layer, Persistence Layer

#NEPHP

Page 46: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Presentation Layer - views, literally and figuratively

#NEPHP

Page 47: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Service Layer - an intermediary between the presentation and

model layers

#NEPHP

Page 48: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Service layer provides a stable, backwards-compatible interface

and forms the foundation for dependency injection.

#NEPHP

Page 49: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Domain layer - business logic, including models. Contains the

implementation of service contracts.

#NEPHP

Page 50: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Persistence Layer - resource models that perform CRUD

operations on database tables.

#NEPHP

Page 51: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Some models use a single table, others continue to use the

Entity-Attribute-Value design pattern used in Magento 1.

#NEPHP

Page 52: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Design Patterns

#NEPHP

Page 53: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Loose Coupling

#NEPHP

Page 54: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Dependency Injection

#NEPHP

Page 55: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Service Contracts

#NEPHP

Page 56: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Interceptors

#NEPHP

Page 57: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Semantic Versioning

#NEPHP

Page 58: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

DEPENDENCY INJECTION

Sorry - no cool photo here, because I don’t like needles…

Page 59: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

DI is exactly what it sounds like - injecting dependencies into the

objects that need them.

#NEPHP

Page 60: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

DI is designed to reduce dependencies and promote loose

coupling

#NEPHP

Page 61: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

DI makes unit testing much easier

#NEPHP

Page 62: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Magento 2 uses the Constructor Injection pattern of DI

#NEPHP

Page 63: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

DI in Magento 2 is handled via XML files

#NEPHP

Page 64: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

di.xml

<config xmlns:xsi=“[…]” xsi:noNamespaceSchemaLocation=“[…]”> <virtualType name="Magento\SamplePaymentProvider\Block\Form\Payinstore" type="Magento\Payment\Block\Form" shared="false"> <arguments> <argument name="data" xsi:type="array"> <item name="template" xsi:type=“string"> Magento_SamplePaymentProvider::form/payinstore.phtml </item> </argument> </arguments> </virtualType></config>

Page 65: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

INTERCEPTORS

Page 66: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Plugin system based on the interceptor pattern

#NEPHP

Page 67: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Calls to almost any module can be intercepted and altered

#NEPHP

Page 68: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Vast improvement over the rewrite pattern in Magento 1 - no

more rewrite conflicts

#NEPHP

Page 69: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

di.xml

<config> <type name="{ObservedType}"> <plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1" disabled="false"/> </type></config>

Page 70: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Sort order defines order if multiple plugins intercept the

same item

#NEPHP

Page 71: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Possible to intercept before, after and around a function

#NEPHP

Page 72: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

‘Before’ Interceptor

class Plugin{ public function beforeSetName(\Magento\Catalog\Model\Product $subject, $name) { return array('(' . $name . ')'); }}

Page 73: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

‘After’ Interceptor

class Plugin{ public function afterGetName(\Magento\Catalog\Model\Product $subject, $result) { return '|' . $result . '|'; }}

Page 74: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

‘Around’ Interceptor

class Plugin{ public function aroundSave(\Magento\Catalog\Model\Product $subject, \Closure $proceed) { $this->doSomethingBeforeProductIsSaved(); $returnValue = $proceed(); if ($returnValue) { $this->postProductToFacebook(); } return $returnValue; }}

Page 75: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

SERVICE CONTRACTS

Credit to Allan MacGregor for the Soylent Green joke.

Page 76: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Set of interfaces to define the public API of a module

#NEPHP

Page 77: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

This API is the interface provided to other modules to access its

implementation

#NEPHP

Page 78: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Designed to hide business logic behind a stable interface

#NEPHP

Page 79: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Service contracts + semantic versioning = minor releases will not

break existing code

#NEPHP

Page 80: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

@deprecated = will be removed with the next major version release

#NEPHP

Page 81: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

CustomerRepositoryInterface.phpnamespace Magento\Customer\Api;/** * Customer CRUD interface. */interface CustomerRepositoryInterface{ /** * Create customer. * * @api * @param \Magento\Customer\Api\Data\CustomerInterface $customer * @param string $passwordHash * @return \Magento\Customer\Api\Data\CustomerInterface * @throws \Magento\Framework\Exception\InputException If bad input is provided * @throws \Magento\Framework\Exception\State\InputMismatchException If the provided email is already used * @throws \Magento\Framework\Exception\LocalizedException */ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $passwordHash = null);

Page 82: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

CustomerRepositoryInterface.php/** * Retrieve customer. * * @api * @param string $email * @param int|null $websiteId * @return \Magento\Customer\Api\Data\CustomerInterface * @throws \Magento\Framework\Exception\NoSuchEntityException If customer with the specified email does not exist. * @throws \Magento\Framework\Exception\LocalizedException */public function get($email, $websiteId = null);

/** * Retrieve customer. * * @api * @param int $customerId * @return \Magento\Customer\Api\Data\CustomerInterface * @throws \Magento\Framework\Exception\NoSuchEntityException If customer with the specified ID does not exist. * @throws \Magento\Framework\Exception\LocalizedException */public function getById($customerId);

Page 83: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Service Contracts include Data Interfaces and Service Interfaces

#NEPHP

Page 84: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Data Interfaces return information about data entities

#NEPHP

Page 85: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Service Interfaces handle business logic

#NEPHP

Page 86: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Three types of service interfaces in Magento 2 (so far)

#NEPHP

Page 87: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Repository Interfaces provide access to persistent data entities

#NEPHP

Page 88: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

CustomerRepositoryInterface, AddressRepositoryInterface, etc.

#NEPHP

Page 89: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Repository interfaces contain the CRUD operations

#NEPHP

Page 90: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Management interfaces contain management functions not related

to repositories

#NEPHP

Page 91: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Validators, createAccount, changePassword, etc

#NEPHP

Page 92: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Metadata interfaces provide meta information - primarily about

custom attributes

#NEPHP

Page 93: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

EXTENDING MAGENTO 2

Page 94: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Create your basic module file structure

#NEPHP

Page 95: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

App/Code/<Vendor>/<Module>/

composer.json etc/module.xml Test/Unit/<tests go here>

Page 96: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

Composer.json{ "name": "joshuaswarren/sample-module-minimal", "description": "A minimal sample Magento 2 module", "type": "magento2-module", "version": "1.0.0", "license": [ "OSL-3.0", "AFL-3.0" ], "require": { "php": "~5.5.0|~5.6.0", "magento/magento-composer-installer": "*" }, "extra": { "map": [ [ "*", "joshuaswarren/SampleMinimal" ] ] } }

Page 97: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

etc/module.xml

<config> <module name=“Joshuaswarren_SampleMinimal" setup_version="2.0.0"> </module> </config>

Page 98: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Optional config files in etc: acl.xml

config.xml di.xml

webapi.xml

#NEPHP

Page 99: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

acl.xml defines new items for Magento’s ACL system

#NEPHP

Page 100: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Config.xml adds new configuration options

#NEPHP

Page 101: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Webapi.xml defines items to expose via the REST or SOAP APIs

#NEPHP

Page 102: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Optional subdirectories in etc: adminhtml frontend

webapi_rest webapi_soap

#NEPHP

Page 103: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Items in the main etc directory apply globally to your extension

#NEPHP

Page 104: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Items in the 4 subdirectories apply only to that area - i.e., adminhtml

only applies to the Magento backend

#NEPHP

Page 105: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Optional subdirectories: API

Block Controller

Helper Model

#NEPHP

Page 106: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Optional subdirectories: Plugin Setup

Ui i18n view

#NEPHP

Page 107: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

API contains any new service contracts your extension adds

#NEPHP

Page 108: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Block contains any new template blocks your extension adds

#NEPHP

Page 109: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Controller contains your extension’s controllers

#NEPHP

Page 110: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Helper contains any helper functions that your extension

needs

#NEPHP

Page 111: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Model contains your extension’s models

#NEPHP

Page 112: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Plugin contains any interceptors your extension defines

#NEPHP

Page 113: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Setup contains your database migrations using Magento’s setup

script system

#NEPHP

Page 114: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

UI is for Magento 2’s new Magento UI library

#NEPHP

Page 115: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

I18n contains internationalization files - CSV files defining the

translations for your strings

#NEPHP

Page 116: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

View contains the views for your extension

#NEPHP

Page 117: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

If your extension doesn’t need one of these items, just omit that

folder

#NEPHP

Page 118: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Sample: custom shipping method to allow for in-store pickup from

several locations

#NEPHP

Page 119: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

Block/System/Config/Form/Field/Locations.phpnamespace Magento\SampleShippingProvider\Block\System\Config\Form\Field;use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;/** * Backend system config array field renderer */class Locations extends AbstractFieldArray{ /** * Initialise columns for 'Store Locations' * * @return void */ protected function _construct() { $this->addColumn('title', ['label' => __('Title'), 'class' => 'validate-no-empty validate-alphanum-with-spaces']); $this->addColumn('street', ['label' => __('Street Address'), 'class' => 'validate-no-empty validate-alphanum-with-spaces']); $this->addColumn('phone', ['label' => __('Phone Number'), 'class' => 'validate-no-empty validate-no-empty validate-phoneStrict']); $this->addColumn('message', ['label' => __('Message'), 'class' => 'validate-no-empty']); $this->_addAfter = false; parent::_construct(); }}

Page 120: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

Model/Type/Plugin/Onepage.php [1/2]namespace Magento\SampleShippingProvider\Model\Type\Plugin;use Magento\Checkout\Model\Type\Onepage as CheckoutOnePage;use Magento\SampleShippingProvider\Model\Carrier;/** * Change Shipping Address to selected Store location address */class Onepage{ /** * @var Carrier */ private $carrier; /** * @param Carrier $carrier */ public function __construct(Carrier $carrier) { $this->carrier = $carrier; }

Page 121: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

Model/Type/Plugin/Onepage.php [2/2] /** * Replace shipping address with pickup location address * @param CheckoutOnePage $subject * @param array $result * @return $this */ public function afterSaveShippingMethod(CheckoutOnePage $subject, array $result) { if ($result) { return $result; } $quote = $subject->getQuote(); $shippingAddress = $quote->getShippingAddress(); $shippingMethod = $shippingAddress->getShippingMethod(); /** * In-Store pickup selected * Update Shipping Address */ if (strpos($shippingMethod, $this->carrier->getCarrierCode()) !== false) { $locationAddress = $this->carrier->getLocationInfo($shippingMethod); $shippingAddress->setCountryId($locationAddress['country_id']); $shippingAddress->setRegionId($locationAddress['region_id']); $shippingAddress->setPostcode($locationAddress['postcode']); $shippingAddress->setCity($locationAddress['city']); $shippingAddress->setStreet($locationAddress['street']); $shippingAddress->setTelephone($locationAddress['phone']); } return $result; }}

Page 122: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

Model/Carrier.phpnamespace Magento\SampleShippingProvider\Model;use Psr\Log\LoggerInterface;use Magento\Framework\App\Config\ScopeConfigInterface;use Magento\Store\Model\ScopeInterface;use Magento\Shipping\Model\Carrier\AbstractCarrier;use Magento\Shipping\Model\Carrier\CarrierInterface;use Magento\Shipping\Model\Config;use Magento\Shipping\Model\Rate\ResultFactory;use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;/** * In-Store Pickup shipping model */class Carrier extends AbstractCarrier implements CarrierInterface{ /** * @var string */ protected $_code = 'storepickup'; /** * @var bool */ protected $_isFixed = true;

… see https://github.com/magento/magento2-samples/blob/master/sample-module-shipping-provider/Model/Carrier.php

Page 123: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

In model/carrier.php we implement all of the functions a shipping carrier must have in Magento 2

#NEPHP

Page 124: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

GetAllowedMethods CollectRates

GetLocationInfo BuildRateForLocation

GetLocations GetShippingOrigin

#NEPHP

Page 125: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

Etc/Adminhtml/System.xml<system> <section id="carriers"> <group id="storepickup" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <label>In-Store Pickup</label> <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Enabled</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> <comment> <![CDATA[<strong style="color:red">Warning</strong>: Shipping Origin should be configured to use this method.]]> </comment> </field> <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Title</label> </field>[…]

Page 126: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

Etc/Frontend/di.xml<config xmlns:xsi=“[…]” xsi:noNamespaceSchemaLocation=“[…]”> <type name="Magento\SampleShippingProvider\Model\Type\Plugin\Onepage"/> <type name="Magento\Checkout\Model\Type\Onepage"> <plugin name="change_shipping_address" type="Magento\SampleShippingProvider\Model\Type\Plugin\Onepage"/> </type></config>

Page 127: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

Etc/Config.xml<config xmlns:xsi=“[…]” xsi:noNamespaceSchemaLocation=“[…]”><default><carriers> <storepickup> <active>1</active> <model>Magento\SampleShippingProvider\Model\Carrier</model> <title>In-Store Pickup</title> <specificerrmsg>This shipping method is not available.</specificerrmsg> </storepickup></carriers></default></config>

Page 128: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

Etc/Module.xml<config xmlns:xsi=“[…]” xsi:noNamespaceSchemaLocation=“[…]”> <module name="Magento_SampleShippingProvider" setup_version="2.0.0"> </module></config>

Page 129: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP

Composer.json{ "name": "magento/sample-module-shipping-provider", "description": "Demonstrate Shipping Provider", "type": "magento2-module", "version": "1.0.0", "license": [ "OSL-3.0", "AFL-3.0" ], "require": { "php": "~5.5.0|~5.6.0", "magento/magento-composer-installer": "*", "magento/framework": "~0.74", "magento/module-store": "~0.74", "magento/module-shipping": "~0.74", "magento/module-quote": "~0.74", "magento/module-checkout": "~0.74" }, "extra": { "map": [ [ "*", "Magento/SampleShippingProvider" ] ] } }

Page 130: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

We now have a complete, functioning in-store-pickup

shipping extension for Magento 2

#NEPHP

Page 131: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Take a look at the Test/Unit directory on Github for tests for

this extension

#NEPHP

Page 132: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Magento 2 unit testing includes mocking, fixtures, etc. -

everything you need for TDD

#NEPHP

Page 133: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

LEARNING MOREDon’t end up like this guy ->

Page 134: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

devdocs.magento.com

magento.stackexchange.com/questions/tagged/magento2

#NEPHP

Page 135: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

AlanStorm.com AlanKent.me

CoderOnCode.com

#NEPHP

Page 136: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Upcoming events: Meet Magento New York, ZendCon, php[world]

#NEPHP

Page 137: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com

Programming With Magento 2 coming to amazon.com & phparch.com

#NEPHP

Page 138: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

Keep in Touch!

joind.in/14737

@JoshuaSWarren

JoshuaWarren.com

Mage2DevBook.com

Page 139: Magento 2 - An Intro to a Modern PHP-Based System - Northeast PHP 2015

JoshuaWarren.com #NEPHP


Recommended