Table of Contents for
Magento 2 Development Quick Start Guide

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Magento 2 Development Quick Start Guide by Branko Ajzele Published by Packt Publishing, 2018
  1. Magento 2 Development Quick Start Guide
  2. Title Page
  3. Copyright and Credits
  4. Magento 2 Development Quick Start Guide
  5. Packt Upsell
  6. Why subscribe?
  7. Packt.com
  8. Contributors
  9. About the author
  10. About the reviewer
  11. Packt is searching for authors like you
  12. Table of Contents
  13. Preface
  14. Who this book is for
  15. What this book covers
  16. To get the most out of this book
  17. Download the example code files
  18. Code in Action
  19. Conventions used
  20. Get in touch
  21. Reviews
  22. Understanding the Magento Architecture
  23. Technical requirements
  24. Installing Magento
  25. Modes
  26. Areas
  27. Request flow processing
  28. Modules
  29. Creating the minimal module
  30. Cache
  31. Dependency injection
  32. Argument injection
  33. Virtual types
  34. Proxies
  35. Factories
  36. Plugins
  37. The before plugin
  38. The around plugin
  39. The after plugin
  40. Events and observers
  41. Console commands
  42. Cron jobs
  43. Summary
  44. Working with Entities
  45. Technical requirements
  46. Understanding types of models
  47. Creating a simple model
  48. Methods worth memorizing
  49. Working with setup scripts
  50. The InstallSchema script
  51. The UpgradeSchema script
  52. The Recurring script
  53. The InstallData script
  54. The UpgradeData script
  55. The RecurringData script
  56. Extending entities
  57. Creating extension attributes
  58. Summary
  59. Understanding Web APIs
  60. Technical requirements
  61. Types of users
  62. Types of authentication
  63. Types of APIs
  64. Using existing web APIs
  65. Creating custom web APIs
  66. Understanding search criteria
  67. Summary
  68. Building and Distributing Extensions
  69. Technical requirements
  70. Building a shipping extension
  71. Distributing via GitHub
  72. Distributing via Packagist
  73. Summary
  74. Developing for Admin
  75. Technical requirements
  76. Using the listing component
  77. Using the form component
  78. Summary
  79. Developing for Storefront
  80. Technical requirements
  81. Setting up the playground
  82. Calling and initializing JS components
  83. Meet RequireJS
  84. Replacing jQuery widget components
  85. Extending jQuery widget components
  86. Creating jQuery widgets components
  87. Creating UI/KnockoutJS components
  88. Extending UI/KnockoutJS components
  89. Summary
  90. Customizing Catalog Behavior
  91. Technical requirements
  92. Creating the size guide
  93. Creating the same day delivery
  94. Flagging new products
  95. Summary
  96. Customizing Checkout Experiences
  97. Technical requirements
  98. Passing data to the checkout
  99. Adding order notes to the checkout
  100. Summary
  101. Customizing Customer Interactions
  102. Technical requirements
  103. Understanding the section mechanism
  104. Adding contact preferences to customer accounts
  105. Adding contact preferences to the checkout
  106. Summary
  107. Other Books You May Enjoy
  108. Leave a review - let other readers know what you think

Creating extension attributes

Creating a new extension attribute for an existing entity is usually a case of doing the following:

  1. Using setup scripts to set the attribute, column, or table for persistence
  2. Defining the extension attribute via <VendorName>/<ModuleName>/etc/extension_attributes.xml
  3. Adding an after and/or before plugin to the save, get, and getList methods of an entity repository

Moving forward, we are going to create extension attributes for the order entity, that is, customer_note and merchant_note.

We can imagine customer_note as an attribute that does not persist its value(s) in the sales_order table as order entity does, whereas merchant_note attribute does. This is why we created the sales_order.merchant_note column earlier via the UpgradeData

script.

Let's go ahead and create the <MAGELICIOUS_DIR>/Core/Api/Data/CustomerNoteInterface.php file with the following content:

interface CustomerNoteInterface extends \Magento\Framework\Api\ExtensibleDataInterface
{
const CREATED_BY = 'created_by';
const NOTE = 'note';

public function setCreatedBy($createdBy);
public function getCreatedBy();
public function setNote($note);
public function getNote();
}

The customer_note attribute is going to be a full-blown object, so we will create an interface for it.

While omitted in the example, be sure to set the doc blocks on each method, otherwise the Magento web API will throw an Each getter must have a doc block error once we hook up the plugin methods.

We will then create the <MAGELICIOUS_DIR>/Core/Model/CustomerNote.php file with the following content:

class CustomerNote extends \Magento\Framework\Model\AbstractExtensibleModel implements \Magelicious\Core\Api\Data\CustomerNoteInterface
{
public function setCreatedBy($createdBy) {
return $this->setData(self::CREATED_BY, $createdBy);
}

public function getCreatedBy() {
return $this->getData(self::CREATED_BY);
}

public function getNote() {
return $this->getData(self::NOTE);
}

public function setNote($note) {
return $this->setData(self::NOTE, $note);
}
}

This class is essentially our customer_note entity model. To keep things minimal, we will just implement the CustomerNoteInterface, without any extra logic.

We will then go ahead and create the <MAGELICIOUS_DIR>/Core/etc/extension_attributes.xml file with the following content:

<?xml version="1.0"?>

<config>
<extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
<attribute code="customer_note" type="Magelicious\Core\Api\Data\CustomerNoteInterface"/>
<attribute code="merchant_note" type="string"/>
</extension_attributes>
</config>

The extension_attributes.xml file is where we register our extension attributes. The type argument allows us to register either complex types, such as an interface, or scalar types, such as a string or integer. With the extension attributes registered, it is time to register the corresponding plugins. This is done via the di.xml file.

Let's go ahead and create the <MAGELICIOUS_DIR>/Core/etc/di.xml file with the following content:

<?xml version="1.0"?>

<config>
<preference for="Magelicious\Core\Api\Data\CustomerNoteInterface" type="Magelicious\Core\Model\CustomerNote"/>
<type name="Magento\Sales\Api\OrderRepositoryInterface">
<plugin name="customerNoteToOrderRepository" type="Magelicious\Core\Plugin\CustomerNoteToOrderRepository"/>
<plugin name="merchantNoteToOrderRepository" type="Magelicious\Core\Plugin\MerchantNoteToOrderRepository"/>
</type>
</config>

The reason for registering plugins in the first place is to have our customer_note and merchant_note attributes available on the getList, get, and save methods of the Magento\Sales\Api\OrderRepositoryInterface interface. The repository interfaces are the main way of CRUD-ing entities under service contracts. Without proper plugins, Magento simply would not see our attributes.

Let's create the <MAGELICIOUS_DIR>/Core/Plugin/CustomerNoteToOrderRepository.php file with the following content:

class CustomerNoteToOrderRepository {
protected $orderExtensionFactory;
protected $customerNoteInterfaceFactory;

public function __construct(
\Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory,
\Magelicious\Core\Api\Data\CustomerNoteInterfaceFactory $customerNoteInterfaceFactory
) {
$this->orderExtensionFactory = $orderExtensionFactory;
$this->customerNoteInterfaceFactory = $customerNoteInterfaceFactory;
}

private function getCustomerNoteAttribute(
\Magento\Sales\Api\Data\OrderInterface $resultOrder
) {
$extensionAttributes = $resultOrder->getExtensionAttributes() ?: $this->orderExtensionFactory->create();

// TODO: Get customer note from somewhere (below we fake it)
$customerNote = $this->customerNoteInterfaceFactory->create()
->setCreatedBy('Mark')
->setNote('The note ' . \time());

$extensionAttributes->setCustomerNote($customerNote);
$resultOrder->setExtensionAttributes($extensionAttributes);
return $resultOrder;
}

private function saveCustomerNoteAttribute(
\Magento\Sales\Api\Data\OrderInterface $resultOrder
) {
$extensionAttributes = $resultOrder->getExtensionAttributes();
if ($extensionAttributes && $extensionAttributes->getCustomerNote()) {
// TODO: Save $extensionAttributes->getCustomerNote() somewhere
}
return $resultOrder;
}
}

Right now, there are no plugin methods defined. getCustomerNoteAttribute and saveCustomerNoteAttribute are essentially helper methods that we will soon use.

Let's extend our CustomerNoteToOrderRepository class by adding the after plugin for the getList method, as follows:

public function afterGetList(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
\Magento\Sales\Model\ResourceModel\Order\Collection $resultOrder
) {
foreach ($resultOrder->getItems() as $order) {
$this->afterGet($subject, $order);
}
return $resultOrder;
}

Now, let's extend our CustomerNoteToOrderRepository class by adding the after plugin for the get method, as follows:

public function afterGet(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
\Magento\Sales\Api\Data\OrderInterface $resultOrder
) {
$resultOrder = $this->getCustomerNoteAttribute($resultOrder);
return $resultOrder;
}

Finally, let's extend our CustomerNoteToOrderRepository class by adding the after plugin for the save method, as follows:

public function afterSave(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
\Magento\Sales\Api\Data\OrderInterface $resultOrder
) {
$resultOrder = $this->saveCustomerNoteAttribute($resultOrder);
return $resultOrder;
}

With the plugins for customer_note sorted, let's go ahead and address the plugins for merchant_note. We will create the <MAGELICIOUS_DIR>/Core/Plugin/MerchantNoteToOrderRepository.php file with the following content:

class MerchantNoteToOrderRepository {
protected $orderExtensionFactory;

public function __construct(
\Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory
) {
$this->orderExtensionFactory = $orderExtensionFactory;
}

private function getMerchantNoteAttribute(
\Magento\Sales\Api\Data\OrderInterface $order
) {
$extensionAttributes = $order->getExtensionAttributes() ?: $this->orderExtensionFactory->create();
$extensionAttributes->setMerchantNote($order->getData('merchant_note'));
$order->setExtensionAttributes($extensionAttributes);
return $order;
}

private function saveMerchantNoteAttribute(
\Magento\Sales\Api\Data\OrderInterface $order
) {
$extensionAttributes = $order->getExtensionAttributes();
if ($extensionAttributes && $extensionAttributes->getMerchantNote()) {
$order->setData('merchant_note', $extensionAttributes->getMerchantNote());
}
return $order;
}
}

Right now, there are no plugin methods defined. getMerchantNoteAttribute and saveMerchantNoteAttribute are essentially helper methods that we will soon use.

Let's extend our MerchantNoteToOrderRepository class by adding the after plugin for the getList method, as follows:

public function afterGetList(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
\Magento\Sales\Model\ResourceModel\Order\Collection $order
) {
foreach ($order->getItems() as $_order) {
$this->afterGet($subject, $_order);
}
return $order;
}

Now, let's extend our MerchantNoteToOrderRepository class by adding the after plugin for the get method, as follows:

public function afterGet(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
\Magento\Sales\Api\Data\OrderInterface $order
) {
$order = $this->getMerchantNoteAttribute($order);
return $order;
}

Finally, let's extend our MerchantNoteToOrderRepository class by adding the before plugin for the save method, as follows:

public function beforeSave(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
\Magento\Sales\Api\Data\OrderInterface $order
) {
$order = $this->saveMerchantNoteAttribute($order);
return [$order];
}

The obvious difference here is that, with MerchantNoteToOrderRepository, we are using beforeSave, whereas we used afterSave with CustomerNoteToOrderRepository. The reason for this is that merchant_note is to be saved directly on the entity whose repository we are plugging into, that is, its table in the sales_order database. This way, we use its Magento\Framework\DataObject properties of setData to fetch what was assumingly note already set via extension attributes and pass it onto the object's merchant_note property before it is saved. Magento's built-in save mechanism then takes over and stores the property, as long as the corresponding column exists in the database.

With the plugins in place, our attributes should now be visible and persistable when used through the OrderRepositoryInterface. Without getting too deep into the web API at this point, we can quickly test this via performing the following REST request:

GET /index.php/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=entity_id&searchCriteria[filter_groups][0][filters][0][value]=1
Host: magelicious.loc
Content-Type: application/json
Authorization: Bearer 0vq6d4kabpxgc5kysb2sybf3n4ct771x

Whereas the Bearer token is something we get by running the following REST login action:

POST /index.php/rest/V1/integration/admin/token
Host: magelicious.loc
Content-Type: application/json
{"username": "john", "password": "grdM%0i9a49n"}

The successful response of GET /V1/orders should yield a result of the following partial structure:

{
"items": [
{
"extension_attributes": {
"shipping_assignments": [...],
"customer_note": {
"created_by": "Mark",
"note": "Note ABC"
},
"merchant_note": "Note XYZ"
}
}
]
}
We can see that our two attributes are nicely nested within the extension_attributes key.
Postman, the API development tool, makes it easy to test APIs. See https://www.getpostman.com for more information.

The OrderRepositoryInterface to web API REST relationship maps out as follows:

  • getList: GET /V1/orders (plus the search criteria part)
  • get: GET /V1/orders/:id
  • save: POST /V1/orders/create

We will learn more about the web API in the next chapter. The example given here was merely for the purpose of visualizing the work we have done around plugins. Using extension attributes, with the help of plugins, we have essentially extended the Magento web API.