# 通过 REST API 获取自定属性的值

**URL:** https://forum.magentochina.org/t/%E9%80%9A%E8%BF%87-rest-api-%E8%8E%B7%E5%8F%96%E8%87%AA%E5%AE%9A%E5%B1%9E%E6%80%A7%E7%9A%84%E5%80%BC/862
**Category:** Magento 2.X
**Tags:** 后端, magento2, 开发, 教程
**Created:** [2020 年10 月 20 日 06:14 UTC](https://forum.magentochina.org/t/%E9%80%9A%E8%BF%87-rest-api-%E8%8E%B7%E5%8F%96%E8%87%AA%E5%AE%9A%E5%B1%9E%E6%80%A7%E7%9A%84%E5%80%BC/862 "2020-10-20T06:14:52Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### 作者： ![Vicky](https://forum.magentochina.org/letter_avatar_proxy/v4/letter/v/b19c9b/32.png) [@Vicky](https://forum.magentochina.org/u/Vicky)
#### 发布日期： [2020 年10 月 20 日 06:14 UTC](https://forum.magentochina.org/t/%E9%80%9A%E8%BF%87-rest-api-%E8%8E%B7%E5%8F%96%E8%87%AA%E5%AE%9A%E5%B1%9E%E6%80%A7%E7%9A%84%E5%80%BC/862/1 "2020-10-20T06:14:53Z")

</div>

知道如何通过 REST API 获取自定属性的值是很有用的，开发人员就可以添加更多的自定义字段了。

### 概览

- 第一步，添加新的列并且给已存在的订单设置值

- 第二步，添加新的特定文件

- 第三步，添加 observe

- 第四步，添加事件处理的文件

- 第五步，清空文件夹以激活方法

### 第一步，添加新的列并且给已存在的订单设置值

为了获取自定义属性，第一件事就是在 sales\_order 表格中插入一个名为 mageplaza\_helloworld\_custom\_attribute 的列，并且给已有的订单设置值。

### 第二步，添加新的特定文件

因为 Magento2 在 REST API 的响应中不支持新文件，所以需要在扩展文件夹下添加一个文件 \app\code\Mageplaza\HelloWorld\etc\extension\_attributes.xml ，内容如下：

```auto
<?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="mageplaza_helloworld_custom_attribute" type="string" />
  </extension_attributes>
</config>

```

### 第三步，添加 observe

为了把自定义属性插入到 extension\_attributes.xml 中，必须通过文件 \app\code\Mageplaza\HelloWorld\etc\events.xml 为事件 ‘sales\_order\_load\_after’ 添加 observe 。

```auto
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
  <event name="sales_order_load_after">
      <observer name="sales_order_load_helloworld_custom_attribute" instance="Mageplaza\HelloWorld\Observer\Sales\OrderLoadAfter" />
  </event>
</config>

```

### 第四步，添加事件处理的文件

创建文件 app\code\Mageplaza\HelloWorld\Observer\Sales\OrderLoadAfter.php 来管理上一步骤中的 sales\_order\_load\_after 事件。

```auto
<?php
namespace Mageplaza\HelloWorld\Observer\Sales;

use Magento\Framework\Event\ObserverInterface;

class OrderLoadAfter implements ObserverInterface
 
{
 
public function execute(\Magento\Framework\Event\Observer $observer)
 
{
 
    $order = $observer->getOrder();
 
    $extensionAttributes = $order->getExtensionAttributes();
 
 
 
    if ($extensionAttributes === null) {
 
        $extensionAttributes = $this->getOrderExtensionDependency();
 
    }
 
    $attr = $order->getData('helloworld_custom_attribute');
 
    $extensionAttributes->setHelloWorldCustomAttribute($attr);
 
    $order->setExtensionAttributes($extensionAttributes);
 
}
 
   
 
private function getOrderExtensionDependency()
 
{
 
    $orderExtension = \Magento\Framework\App\ObjectManager::getInstance()->get(
        '\Magento\Sales\Api\Data\OrderExtension'
    );
 
    return $orderExtension;
 
}
 
}

```

### 第五步，清空文件夹以激活方法

如果想要激活文件 var\generation\Magento\Sales\Api\Data\OrderExtension.php 中的方法 setHelloWorldCustomAttribute() 和 getHelloWorldCustomAttribute ，需要清空文件夹 var\generation 。

```auto
/**
  * @return string|null
  */
public function getHelloWorldCustomAttribute()
{
     return $this->_get('helloworld_custom_attribute');
}

/**
  * @param string $helloWorldCustomAttribute
  * @return $this
  */
public function setHelloWorldAttribute($helloWorldCustomAttribute)
{
	 $this->setData ('helloworld_custom_attribute', $helloWorldCustomAttribute);
	 return $this;
}

```

## 本教程翻译自Mageplaza模块开发系列，其他内容请点击下面链接:

> [@Magento2模块开发中文教程索引](https://forum.magentochina.org/t/magento2%E6%A8%A1%E5%9D%97%E5%BC%80%E5%8F%91%E4%B8%AD%E6%96%87%E6%95%99%E7%A8%8B%E7%B4%A2%E5%BC%95/36):
>
> 本文翻译自mageplaza的Magento2模块开发教程,而且正在翻译中. 翻译也是非常好的学习方式，希望下列表中还有我们没有翻译，而你又想分享的内容,请发表文章之后，在下面附上地址。 如果觉得本站翻译得内容对您有帮助，又没有时间帮助翻译回馈社区.请打赏我们，您的支持是我们的动力: 转载说明:非常欢迎转载，但是必须保证文章的完整性并附上原文地址和翻译原文的地址，保证阅读者不会产生歧义. 目录索引: 索 引 中 文 原 文 1 [Magento2如何创建模块](https://forum.magentochina.org/t/Magento-2%E5%A6%82%E4%BD%95%E5%88%9B%E5%BB%BA%E6%A8%A1%E5%9D%97%E6%95%99%E7%A8%8B/38)[How to Create Module in Magento 2](https://www.mageplaza.com/kb/how-create-hello-world-module-magento-2.html) 2 [控制器详解](https://forum.magentochina.org/t/Magento-2%E6%8E%A7%E5%88%B6%E5%99%A8%E8%AF%A6%E8%A7%A3%E6%95%99%E7%A8%8B/39)[Create Controllers](https://www.mageplaza.com/magento-2-module-development/how-to-create-controllers-magento-2.html) 3 [CRUD 模型](https://forum.magentochina.org/t/Magento2-CRUD%E6%A8%A1%E5%9E%8B%E8%B5%84%E6%BA%90%E9%9B%86%E5%90%88%E5%B7%A5%E5%8E%82%E5%AF%B9%E8%B1%A1%E8%AF%A6%E8%A7%A3%E6%95%99%E7%A8%8B/43)[CRUD Models](https://www.mageplaza.com/magento-2-module-development/how-to-create-crud-model-magento-2.html) 4 [视图层:块,布局和模板](https://forum.magentochina.org/t/%E8%A7%86%E5%9B%BE%E5%B1%82%E5%9D%97%E5%B8%83%E5%B1%80%E5%92%8C%E6%A8%A1%E6%9D%BF%E6%A8%A1%E6%9D%BF%E6%95%99%E7%A8%8B/50/1)[View: Block, Layouts and templates](https://www.mageplaza.com/magento-2-module-development/view-block-layout-template-magento-2.html) 5 [配置system.xml](https://forum.magentochina.org/t/%E5%A6%82%E4%BD%95%E5%88%9B%E5%BB%BA%E7%B3%BB%E7%BB%9F%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6systemxml/55)[Configuration - System.xm…](https://www.mageplaza.com/magento-2-module-development/create-system-xml-configuration-magento-2.html)
