原文章:https://aureatelabs.com/blog/what-is-the-purpose-of-extension-attributes-in-magento-2/
google翻译:
本文将介绍如何使用 Magento 2 中的扩展属性。扩展属性主要是用于向我们的实体添加附加信息的容器。
每个扩展的接口\Magento\Framework\Api\ExtensibleDataInterface
都可以在扩展属性的帮助下进行扩展。中定义的方法ExtensibleDataInterface
授予对包含附加数据的对象的访问权限。
下面的示例将让您清楚地了解我们试图解释的内容。首先,我们将创建一个名为 DemoModule 的模块。
我们将在app/code/Aureatelabs/DemoModule/etc/extension_attributes.xml
<?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\Checkout\Api\Data\ShippingInformationInterface"> <attribute code="custom_shipping_charge" type="string"/> </extension_attributes> </config>
这意味着我们正在使用属性扩展我们的运输信息接口。它可以从$order->getExtensionAttributes()->getCustomShippingAttribute()
.
以下是简短的描述,将解释这些词的含义:
for – 具有处理扩展的命名空间的完全限定类型名称。该值必须是实现 `ExtensibleDataInterface` 的类型。该接口可以在不同的模块中。
code – 属性的名称。属性名称应该是蛇形(每个单词的第一个字母应该是小写,每个单词用下划线分隔)。
type – 数据类型。这可以是简单的数据类型,例如字符串或整数,也可以是复杂的类型,例如接口。
现在清除var/generation
运行setup:di:compile
命令时,将添加新的 getter 和 setter 方法/var/generation/Magento/Checkout/Api/Data/ShippingInformationExtensionInterface.php
现在您可以通过创建Magento/Checkout/Api/Data/ShippingInformationInterface.php
接口实例来设置/获取这些属性值。
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $addressInformation = $objectManager->create('Magento\Checkout\Api\Data\ShippingInformationInterface'); $extAttributes = $addressInformation->getExtensionAttributes(); $selectedShipping = $extAttributes->getCustomShippingCharge();
需要注意的一件重要事情是,要使扩展属性按建议工作,我们始终需要getExtensionAttributes()
在使用扩展属性工厂创建新属性工厂之前查询实体以获取现有的 extension_attributes。如果我们忽略此检查,我们将清除之前在实体上设置的任何扩展属性。
您将从使用扩展属性中获得的好处之一是您添加的属性数据可通过 API 请求自动获得。
小程序支付&回调php centos Elasticsearch & LogStash & kibana安装及调试 Magento升级到2.4.3订单详情空白 nba2k23球员列表(八) nba2k23球员列表(七) nba2k23球员列表(六) nba2k23球员列表(五) nba2k23球员列表(四) nba2k23球员列表(三) nba2k23球员列表(二) nba2k23球员列表(一) nba2k22球员列表(四) nba2k22球员列表(三) nba2k22球员列表(二) nba2k22球员列表(一) NBA2K21球员大全 NBA2K20球员列表(五) NBA2K20球员列表(四) NBA2K20球员列表(三) NBA2K20球员列表(二) NBA2K20球员列表 wsl docker 突然 -v /sys/fs/cgroup:/sys/fs/cgroup:ro 无法挂载怎么办? 抽象类和接口 Magento 2 中扩展属性(extension_attributes.xml)的用途是什么? win10底部菜单栏卡死解决 docker容器映射所有端口到宿主机 【frp】RDP软件远程Listener state: Not listening [not supported] 升级到magento2.4.3,后台加载很慢解决方法 微信支付https://api.mch.weixin.qq.com/pay/unifiedorder返回null magento grid增加编辑按钮 PHP循环引用 Broken reference: the 'sales_order_tbs ' elemert canmot be atded as child to ‘left , bvcause the lat ubuntu安装swoole后报错:undefined symbol:XXXX swoole pcre.h: No such file or directory magento2 The account sign-in was incorrect or your account is disabled temporarily. Please wait and 批量修改表前缀 magento2.3.7升级2.4.3 elasticsearch可视化工具 pwaStudio-Magento2.4.3 mysql忘记密码创建新用户设置 Docker容器下 mysql source 导入数据库 如何在 Docker 上安装 Magento 2 mysql:基于扫码记录查找密接人员 varnish:pc mobile分开缓存 The requested PHP extension ext-gd * is missing from your system. Install or enable PHP's gd extensi Ubuntu切换指定PHP版本 magento-varnish安装自述 Ubuntu彻底卸载varnish Varnish - Error 503 Backend fetch failed Scandipwa magento2安装教程(在已存在的magento安装) Navicat15最新版本破解 (Navicat Premium 注册出现 No All Pattern Found! File Already Patched) mac本地运行MySQL;Navicat Authentication plugin ‘caching_sha2_password‘ cannot be loaded: dlope问题 mac本地运行PHP项目问题记录 通过 Binlog 日志来恢复 MySQL 数据 composer file could not be downloaded: SSL operation failed with code 1. OpenSSL Error messages: php Laravel统计所有API响应时长等信息 php计算累加的累加 composer PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 Go使用变量类型声明和方法的注意事项 方法 函数值 映射 切片的长度与容量 go基本类型(包、变量、函数、流程控制语句) 如何保证接口的幂等性 nginx 全部域名解析统一设置 mysql之存储过程(while循环、repeat循环、leave语句、loop循环、iterate语句) Laravel 契约和门面简单解读(Cache) Linux-centos安装node,node -v undefind Nginx 的反向代理、负载均衡、动静分离和高可用的原理 es6 set应用场景 vscode安装HTML Boilerplate解决不识别tpl后缀文件 laravel orm with如何指定select字段 laravel框架应用和composer扩展包开发 php实现斐波那契数列 内网穿透工具FRP搭建远程控制 微信小程序 动态添加view组件 唯物辩证法 nba2k游戏助手 更新日志 微信网页分享代码及提示签名错误解决方法 PHP支付宝支付兼容微信公众号 关于支付宝支付接口实际测试后的记录 网站后台权限管理 12306Bypass 分流软件 nba2kol2 12月份数据大更新,公众号球员更新名单进度贴 腾讯视频会员大放送 公众号新功能征集帖 我们知道CDN护航了双11十年,却不知道背后有那么多故事…… 企业微信第三方应用开发授权设置(PHP版) Linux下certpot 免费搭建https协议 CentOS7下Nginx强制http 301跳转到https的设置方法