Share
Embed
Magento 2 Workshop - Meet Magento New York 2015 #MM15NY
Learning the basics of Magento 2 for Magento 1 developers. Periscope recording: https://t.co/zpQp7K3DXO
Joshua Warren
September 22, 2015
Transcript
137 slides
-
PRESENTED BY JOSHUA WARREN PRESENTED AT MEET MAGENTO NEW YORK
2015 Magento 2 WORKSHOP -
JoshuaWarren.com My Experience PHP Developer Since 1999 Founded Creatuity in
2008 Focused on the Magento platform Magento 2 contributor #MM15NY -
JoshuaWarren.com Led the Creatuity team in building 3 Magento 2
extensions (more on the way!) #MM15NY -
JoshuaWarren.com Already migrating a few merchants from Magento 1 to
Magento 2 #MM15NY -
JoshuaWarren.com Just kidding - you all have heard the story
of Magento 1 #MM15NY -
JoshuaWarren.com I’ve been presenting about Magento 2 to PHP developers
for a while #MM15NY -
JoshuaWarren.com For Meet Magento, though, I’ve prepared something a bit
different… #MM15NY -
JoshuaWarren.com The hardest part for a Magento 1 developer learning
Magento 2 has nothing to do with code… #MM15NY -
JoshuaWarren.com PHP developers are learning Magento 2 faster than many
Magento 1 developers. #MM15NY -
JoshuaWarren.com Approach Magento 2 with a desire to learn and
understand the underlying patterns. #MM15NY -
JoshuaWarren.com Don’t approach Magento 2 with the thought “how do
I make my Magento 1 code work here” #MM15NY -
JoshuaWarren.com A look at the state of the art when
Magento 1 was built PHP & Ecommerce in 2007 osCommerce is state of the art Cloud-based / SaaS ecommerce doesn’t exist yet PHP 5.2 is cutting-edge Composer doesn’t exist - PHP has no dependency management There’s this new thing called ZF, otherwise it’s Cake or Symfony Testing? You might find a few people using PHPUnit #MM15NY -
JoshuaWarren.com The Painful World of osCommerce PHP & Ecommerce in
2007 Want to add an attribute? Modify your database schema on live. Looking at add new features? Modify core PHP files. Want to upgrade your store? Good luck! #MM15NY -
JoshuaWarren.com Modernizing PHP-based Ecommerce in 2007 Magento 1’s Initial Development
Forces a standardized approach to extensions, database migrations, etc. Introduces patterns previously unseen in PHP (EAV, anyone?) Broke with standard practices at the time in pursuit of something better Developers flock to Magento 1 because of this #MM15NY -
JoshuaWarren.com As a community, we built documentation & tools to
make Magento 1 development easier. #MM15NY -
JoshuaWarren.com Ecommerce in 2015 Ecommerce grows by 10%/year every year
- despite the Great Recession SaaS ecommerce platforms take off Responsive design & mobile commerce are the norm Ecommerce becomes a basic requirement for modern business - B2C & B2B #MM15NY -
JoshuaWarren.com PHP in 2015 PHP 5.2 has been unsupported for
4 years Namespaces, added in PHP 5.3, didn’t exist when Magento 1 was designed Laravel, Symfony & others are bringing advanced design patterns to PHP PHP 7 is right around the corner, including scalar type hints and more #MM15NY -
JoshuaWarren.com With all of those changes in mind, let’s dive
into Magento 2… #MM15NY -
JoshuaWarren.com Service Layer - an intermediary between the presentation and
model layers #MM15NY -
JoshuaWarren.com Service layer provides a stable, backwards-compatible interface and forms
the foundation for dependency injection. #MM15NY -
JoshuaWarren.com Domain layer - business logic, including models. Contains the
implementation of service contracts. #MM15NY -
JoshuaWarren.com Persistence Layer - resource models that perform CRUD operations
on database tables. #MM15NY -
JoshuaWarren.com Some models use a single table, others continue to
use the Entity-Attribute-Value design pattern used in Magento 1. #MM15NY -
JoshuaWarren.com Start your Magento 2 journey learning the basics of
these design patterns #MM15NY -
DEPENDENCY INJECTION Sorry - no cool photo here, because I
don’t like needles… -
JoshuaWarren.com DI is exactly what it sounds like - injecting
dependencies into the objects that need them. #MM15NY -
JoshuaWarren.com #MM15NY 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> -
JoshuaWarren.com Vast improvement over the rewrite pattern in Magento 1
- no more rewrite conflicts #MM15NY -
JoshuaWarren.com #MM15NY di.xml <config> <type name="{ObservedType}"> <plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1"
disabled="false"/> </type> </config> -
JoshuaWarren.com #MM15NY ‘Before’ Interceptor class Plugin { public function beforeSetName(\Magento\Catalog\Model\Product
$subject, $name) { return array('(' . $name . ')'); } } -
JoshuaWarren.com #MM15NY ‘After’ Interceptor class Plugin { public function afterGetName(\Magento\Catalog\Model\Product
$subject, $result) { return '|' . $result . '|'; } } -
JoshuaWarren.com #MM15NY ‘Around’ Interceptor class Plugin { public function aroundSave(\Magento\Catalog\Model\Product
$subject, \Closure $proceed) { $this->doSomethingBeforeProductIsSaved(); $returnValue = $proceed(); if ($returnValue) { $this->postProductToFacebook(); } return $returnValue; } } -
JoshuaWarren.com Set of interfaces to define the public API of
a module #MM15NY -
JoshuaWarren.com This API is the interface provided to other modules
to access its implementation #MM15NY -
JoshuaWarren.com Service contracts + semantic versioning = minor releases will
not break existing code #MM15NY -
JoshuaWarren.com @deprecated = will be removed with the next major
version release #MM15NY -
JoshuaWarren.com #MM15NY CustomerRepositoryInterface.php namespace 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); -
JoshuaWarren.com #MM15NY 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); -
JoshuaWarren.com #MM15NY 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" ] ] } } -
JoshuaWarren.com Webapi.xml defines items to expose via the REST or
SOAP APIs #MM15NY -
JoshuaWarren.com Items in the main etc directory apply globally to
your extension #MM15NY -
JoshuaWarren.com Items in the 4 subdirectories apply only to that
area - i.e., adminhtml only applies to the Magento backend #MM15NY -
JoshuaWarren.com I18n contains internationalization files - CSV files defining the
translations for your strings #MM15NY -
JoshuaWarren.com If your extension doesn’t need one of these items,
just omit that folder #MM15NY -
JoshuaWarren.com Sample: custom shipping method to allow for in-store pickup
from several locations #MM15NY -
JoshuaWarren.com #MM15NY Block/System/Config/Form/Field/Locations.php namespace 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(); } } -
JoshuaWarren.com #MM15NY 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; } -
JoshuaWarren.com #MM15NY 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; } } -
JoshuaWarren.com #MM15NY Model/Carrier.php namespace 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 -
JoshuaWarren.com model/carrier.php contains all of the functions a shipping carrier
must have in Magento 2 #MM15NY -
JoshuaWarren.com #MM15NY 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> […] -
JoshuaWarren.com #MM15NY 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> -
JoshuaWarren.com #MM15NY 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" ] ] } } -
JoshuaWarren.com We now have a complete, functioning in-store-pickup shipping extension
for Magento 2 #MM15NY -
JoshuaWarren.com Take a look at the Test/Unit directory on Github
for tests for this extension #MM15NY -
JoshuaWarren.com Magento 2 unit testing includes mocking, fixtures, etc. -
everything you need for TDD #MM15NY -
JoshuaWarren.com As a freelancer… Learning Magento 2 Set aside time
in your week to learn the design patterns Magento 2 uses Work through the sample code the Magento 2 team has provided Begin experimenting with developing with Magento 2 Do not try to learn ‘on the job’ - be careful accepting M2 work before you’re ready #MM15NY -
JoshuaWarren.com As an in-house developer for a merchant… Learning Magento
2 Determine when your business is likely to migrate to Magento 2 Start your Magento 2 migration schedule with 2-4 weeks of time dedicated to learning Magento 2 Learn the patterns before you start! #MM15NY -
JoshuaWarren.com As an agency or industry partner… Learning Magento 2
Create a tiger team of developers focused on Magento 2 Allow those developers time in the day to learn Magento 2 Those developers should implement your first Magento 2 projects That team then helps the rest of your team through the learning curve #MM15NY -
JoshuaWarren.com When do I need to be Magento 2 ready?
Learning Magento 2 Magento 2 will be released late enough in the year that most merchants won’t begin using it immediately. Merchants will also wait until their mission-critical extensions are available on Magento 2. Start learning it now - but don’t panic! #MM15NY