Skip to content

Instantly share code, notes, and snippets.

@magevision
Last active June 17, 2019 09:51
Show Gist options
  • Save magevision/4f006e88216f2223e1b8322fac3c1b8b to your computer and use it in GitHub Desktop.
Save magevision/4f006e88216f2223e1b8322fac3c1b8b to your computer and use it in GitHub Desktop.
AddACustomOrderAttributeToAPIResponse
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Sales\Api\OrderRepositoryInterface">
<plugin name="magevision_comment" type="MageVision\Blog47\Plugin\Sales\Model\OrderGet"/>
</type>
</config>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
<attribute code="magevision_comment" type="string"/>
</extension_attributes>
</config>
<?php
namespace MageVision\Blog47\Plugin\Sales\Model;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\ResourceModel\Order\Collection;
class OrderGet
{
/**
* @var OrderExtensionFactory
*/
protected $orderExtensionFactory;
/**
* Init plugin
*
* @param OrderExtensionFactory $orderExtensionFactory
*/
public function __construct(
OrderExtensionFactory $orderExtensionFactory
) {
$this->orderExtensionFactory = $orderExtensionFactory;
}
/**
* @param OrderRepositoryInterface $subject
* @param OrderInterface $resultOrder
* @return OrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(
OrderRepositoryInterface $subject,
OrderInterface $resultOrder
) {
$extensionAttributes = $resultOrder->getExtensionAttributes();
if ($extensionAttributes && $extensionAttributes->getMageVisionComment()) {
return $resultOrder;
}
/** @var \Magento\Sales\Api\Data\OrderExtension $orderExtension */
$orderExtension = $extensionAttributes ? $extensionAttributes : $this->orderExtensionFactory->create();
$orderExtension->setMageVisionComment($resultOrder->getMageVisionComment());
$resultOrder->setExtensionAttributes($orderExtension);
return $resultOrder;
}
/**
* @param OrderRepositoryInterface $subject
* @param Collection $resultOrder
* @return Collection
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetList(
OrderRepositoryInterface $subject,
Collection $resultOrder
) {
/** @var $order */
foreach ($resultOrder->getItems() as $order) {
$this->afterGet($subject, $order);
}
return $resultOrder;
}
}
{
"applied_rule_ids": "1",
"base_currency_code": "USD",
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_grand_total": 36.39,
"base_discount_tax_compensation_amount": 0,
"base_discount_tax_compensation_invoiced": 0,
"base_shipping_amount": 5,
"base_shipping_discount_amount": 0,
"base_shipping_incl_tax": 5,
"base_shipping_invoiced": 5,
"base_shipping_tax_amount": 0,
"base_subtotal": 29,
"base_subtotal_incl_tax": 31.39,
"base_subtotal_invoiced": 29,
"base_tax_amount": 2.39,
"base_tax_invoiced": 2.39,
"base_total_due": 0,
"base_total_invoiced": 36.39,
"base_total_invoiced_cost": 0,
"base_total_paid": 36.39,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 2,
"created_at": "2018-01-05 21:20:36",
"customer_dob": "1973-12-15 00:00:00",
"customer_email": "[email protected]",
"customer_firstname": "Veronica",
"customer_gender": 2,
"customer_group_id": 1,
"customer_id": 1,
"customer_is_guest": 0,
"customer_lastname": "Costello",
"customer_note_notify": 1,
"discount_amount": 0,
"discount_invoiced": 0,
"email_sent": 1,
"entity_id": 1,
"global_currency_code": "USD",
"grand_total": 36.39,
"discount_tax_compensation_amount": 0,
"discount_tax_compensation_invoiced": 0,
"increment_id": "000000001",
"is_virtual": 0,
"order_currency_code": "USD",
"protect_code": "44906181afba069f093a5ec9f770e633",
"quote_id": 1,
"shipping_amount": 5,
"shipping_description": "Flat Rate - Fixed",
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 5,
"shipping_invoiced": 5,
"shipping_tax_amount": 0,
"state": "complete",
"status": "complete",
"store_currency_code": "USD",
"store_id": 1,
"store_name": "Main Website\nMain Website Store\n",
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 29,
"subtotal_incl_tax": 31.39,
"subtotal_invoiced": 29,
"tax_amount": 2.39,
"tax_invoiced": 2.39,
"total_due": 0,
"total_invoiced": 36.39,
"total_item_count": 1,
"total_paid": 36.39,
"total_qty_ordered": 1,
"updated_at": "2019-06-17 09:50:30",
"weight": 1,
"items": [
{
"amount_refunded": 0,
"applied_rule_ids": "1",
"base_amount_refunded": 0,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_discount_tax_compensation_invoiced": 0,
"base_original_price": 29,
"base_price": 29,
"base_price_incl_tax": 31.39,
"base_row_invoiced": 29,
"base_row_total": 29,
"base_row_total_incl_tax": 31.39,
"base_tax_amount": 2.39,
"base_tax_invoiced": 2.39,
"created_at": "2018-01-05 21:20:36",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"discount_tax_compensation_invoiced": 0,
"is_qty_decimal": 0,
"item_id": 1,
"name": "Iris Workout Top",
"no_discount": 0,
"order_id": 1,
"original_price": 29,
"price": 29,
"price_incl_tax": 31.39,
"product_id": 1433,
"product_type": "configurable",
"qty_canceled": 0,
"qty_invoiced": 1,
"qty_ordered": 1,
"qty_refunded": 0,
"qty_shipped": 1,
"row_invoiced": 29,
"row_total": 29,
"row_total_incl_tax": 31.39,
"row_weight": 1,
"sku": "WS03-XS-Red",
"store_id": 1,
"tax_amount": 2.39,
"tax_invoiced": 2.39,
"tax_percent": 8.25,
"updated_at": "2018-01-05 21:20:36",
"weight": 1
}
],
"billing_address": {
"address_type": "billing",
"city": "Calder",
"country_id": "US",
"customer_address_id": 1,
"email": "[email protected]",
"entity_id": 2,
"firstname": "Veronica",
"lastname": "Costello",
"parent_id": 1,
"postcode": "49628-7978",
"region": "Michigan",
"region_code": "MI",
"region_id": 33,
"street": [
"6146 Honey Bluff Parkway"
],
"telephone": "(555) 229-3326"
},
"payment": {
"account_status": null,
"additional_information": [
"Check / Money order"
],
"amount_ordered": 36.39,
"amount_paid": 36.39,
"base_amount_ordered": 36.39,
"base_amount_paid": 36.39,
"base_shipping_amount": 5,
"base_shipping_captured": 5,
"cc_last4": null,
"entity_id": 1,
"method": "checkmo",
"parent_id": 1,
"shipping_amount": 5,
"shipping_captured": 5
},
"status_histories": [],
"extension_attributes": {
"magevision_comment": "MageVision Comment",
"shipping_assignments": [
{
"shipping": {
"address": {
"address_type": "shipping",
"city": "Calder",
"country_id": "US",
"customer_address_id": 1,
"email": "[email protected]",
"entity_id": 1,
"firstname": "Veronica",
"lastname": "Costello",
"parent_id": 1,
"postcode": "49628-7978",
"region": "Michigan",
"region_code": "MI",
"region_id": 33,
"street": [
"6146 Honey Bluff Parkway"
],
"telephone": "(555) 229-3326"
},
"method": "flatrate_flatrate",
"total": {
"base_shipping_amount": 5,
"base_shipping_discount_amount": 0,
"base_shipping_incl_tax": 5,
"base_shipping_invoiced": 5,
"base_shipping_tax_amount": 0,
"shipping_amount": 5,
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 5,
"shipping_invoiced": 5,
"shipping_tax_amount": 0
}
},
"items": [
{
"amount_refunded": 0,
"applied_rule_ids": "1",
"base_amount_refunded": 0,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_discount_tax_compensation_invoiced": 0,
"base_original_price": 29,
"base_price": 29,
"base_price_incl_tax": 31.39,
"base_row_invoiced": 29,
"base_row_total": 29,
"base_row_total_incl_tax": 31.39,
"base_tax_amount": 2.39,
"base_tax_invoiced": 2.39,
"created_at": "2018-01-05 21:20:36",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"discount_tax_compensation_invoiced": 0,
"is_qty_decimal": 0,
"item_id": 1,
"name": "Iris Workout Top",
"no_discount": 0,
"order_id": 1,
"original_price": 29,
"price": 29,
"price_incl_tax": 31.39,
"product_id": 1433,
"product_type": "configurable",
"qty_canceled": 0,
"qty_invoiced": 1,
"qty_ordered": 1,
"qty_refunded": 0,
"qty_shipped": 1,
"row_invoiced": 29,
"row_total": 29,
"row_total_incl_tax": 31.39,
"row_weight": 1,
"sku": "WS03-XS-Red",
"store_id": 1,
"tax_amount": 2.39,
"tax_invoiced": 2.39,
"tax_percent": 8.25,
"updated_at": "2018-01-05 21:20:36",
"weight": 1
}
]
}
]
}
}
<?php
namespace MageVision\Blog47\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* {@inheritdoc}
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
if (version_compare($context->getVersion(), '2.0.0', '<')) {
$table = $installer->getTable('sales_order');
$installer->getConnection()
->addColumn(
$table,
'magevision_comment',
[
'type' => Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => 'MageVision Order Comment'
]
);
}
$installer->endSetup();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment