Retrieve extension_attributes with API searchCriteria working wrong
-
08-10-2020 - |
题
I created a new field on Creditmemos as an extension_attribute.
I thought is working fine, when I create a Creditmemo, the field is saved correctly in the ddbb and when I get the creditmemo with the API: /rest/V1/creditmemo/6
it displays correctly too.
But then I tested using /rest/V1/creditmemos?searchCriteria=0
and is doing something weird... my attribute extension is always returning the same value for every creditmemo. Is that a bug? How can I solve that?
I post my code for the context of retrieving the value:
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\Sales\Api\Data\CreditmemoInterface">
<attribute code="size_exchange" type="string"/>
</extension_attributes>
</config>
etc/events.xml
<?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_creditmemo_save_after">
<observer name="creditmemo_save_size_exchange" instance="vendor\module\Observer\SaveExchangeCreditmemo"/>
</event>
<event name="sales_order_creditmemo_load_after">
<observer name="creditmemo_load_size_exchange" instance="vendor\module\Observer\LoadExchangeCreditmemo"/>
</event>
</config>
Observer/LoadExchangeCreditmemo.php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Sales\Api\Data\CreditmemoExtension;
class LoadExchangeCreditmemo implements ObserverInterface
{
private $field = 'size_exchange';
private $creditmemoExtension;
public function __construct(CreditmemoExtension $creditmemoExtension)
{
$this->creditmemoExtension = $creditmemoExtension;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$creditmemo = $observer->getCreditmemo();
$extensionAttributes = $creditmemo->getExtensionAttributes();
if ($extensionAttributes === null) {
$extensionAttributes = $this->creditmemoExtension;
}
$value = $creditmemo->getData($this->field) ? $creditmemo->getData($this->field) : '0';
$extensionAttributes->setData($this->field, $value);
$creditmemo->setExtensionAttributes($extensionAttributes);
}
}
Results
V1/creditmemos?searchCriteria=0
{
"items": [
{
"adjustment": 0,
"base_adjustment": 0,
"base_currency_code": "USD",
"base_discount_amount": 0,
"base_grand_total": 7.64,
"base_discount_tax_compensation_amount": 0,
"base_shipping_amount": 5,
"base_shipping_incl_tax": 5,
"base_shipping_tax_amount": 0,
"base_subtotal": 0,
"base_subtotal_incl_tax": 0,
"base_tax_amount": 2.64,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 4,
"created_at": "2017-05-04 09:20:00",
"discount_amount": 0,
"entity_id": 1,
"global_currency_code": "USD",
"grand_total": 7.64,
"discount_tax_compensation_amount": 0,
"increment_id": "000000001",
"order_currency_code": "USD",
"order_id": 2,
"shipping_address_id": 3,
"shipping_amount": 5,
"shipping_incl_tax": 5,
"shipping_tax_amount": 0,
"state": 2,
"store_currency_code": "USD",
"store_id": 1,
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 0,
"subtotal_incl_tax": 0,
"tax_amount": 2.64,
"updated_at": "2017-05-04 09:20:00",
"items": [
{
"base_cost": null,
"base_price": 0,
"base_row_total": 0,
"base_weee_tax_row_disposition": 0,
"entity_id": 1,
"name": "Minerva LumaTech™ V-Tee-XS-Blue",
"order_item_id": 2,
"parent_id": 1,
"price": 0,
"product_id": 1483,
"qty": 1,
"row_total": 0,
"sku": "WS08-XS-Blue",
"weee_tax_applied": "[]",
"weee_tax_applied_row_amount": 0,
"weee_tax_row_disposition": 0
}
],
"comments": [],
"extension_attributes": {
"size_exchange": "1"
}
},
{
"adjustment": 0,
"adjustment_negative": 0,
"adjustment_positive": 0,
"base_adjustment": 0,
"base_adjustment_negative": 0,
"base_adjustment_positive": 0,
"base_currency_code": "USD",
"base_discount_amount": 0,
"base_grand_total": 7.39,
"base_discount_tax_compensation_amount": 0,
"base_shipping_amount": 5,
"base_shipping_incl_tax": 5,
"base_shipping_tax_amount": 0,
"base_subtotal": 0,
"base_subtotal_incl_tax": 0,
"base_tax_amount": 2.39,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 2,
"created_at": "2017-05-04 20:16:59",
"discount_amount": 0,
"entity_id": 3,
"global_currency_code": "USD",
"grand_total": 7.39,
"discount_tax_compensation_amount": 0,
"increment_id": "000000003",
"order_currency_code": "USD",
"order_id": 1,
"shipping_address_id": 1,
"shipping_amount": 5,
"shipping_incl_tax": 5,
"shipping_tax_amount": 0,
"state": 2,
"store_currency_code": "USD",
"store_id": 1,
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 0,
"subtotal_incl_tax": 0,
"tax_amount": 2.39,
"updated_at": "2017-05-04 20:16:59",
"items": [
{
"base_cost": null,
"base_price": 0,
"base_row_total": 0,
"base_weee_tax_row_disposition": 0,
"entity_id": 2,
"name": "Iris Workout Top-XS-Red",
"order_item_id": 1,
"parent_id": 3,
"price": 0,
"product_id": 1420,
"qty": 1,
"row_total": 0,
"sku": "WS03-XS-Red",
"weee_tax_applied": "[]",
"weee_tax_applied_row_amount": 0,
"weee_tax_row_disposition": 0
}
],
"comments": [],
"extension_attributes": {
"size_exchange": "1"
}
},
{
"adjustment": 0,
"adjustment_negative": 0,
"adjustment_positive": 0,
"base_adjustment": 0,
"base_adjustment_negative": 0,
"base_adjustment_positive": 0,
"base_currency_code": "USD",
"base_discount_amount": 0,
"base_grand_total": 45.47,
"base_discount_tax_compensation_amount": 0,
"base_shipping_amount": 0,
"base_shipping_incl_tax": 0,
"base_shipping_tax_amount": 0,
"base_subtotal": 42,
"base_subtotal_incl_tax": 45.47,
"base_tax_amount": 3.47,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 6,
"created_at": "2017-05-04 20:19:22",
"discount_amount": 0,
"entity_id": 4,
"global_currency_code": "USD",
"grand_total": 45.47,
"discount_tax_compensation_amount": 0,
"increment_id": "000000004",
"order_currency_code": "USD",
"order_id": 3,
"shipping_address_id": 5,
"shipping_amount": 0,
"shipping_incl_tax": 0,
"shipping_tax_amount": 0,
"state": 2,
"store_currency_code": "USD",
"store_id": 1,
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 42,
"subtotal_incl_tax": 45.47,
"tax_amount": 3.47,
"updated_at": "2017-05-04 20:19:22",
"items": [
{
"base_cost": null,
"base_discount_tax_compensation_amount": 0,
"base_price": 21,
"base_price_incl_tax": 22.74,
"base_row_total": 42,
"base_row_total_incl_tax": 45.47,
"base_tax_amount": 3.47,
"base_weee_tax_row_disposition": 0,
"entity_id": 3,
"discount_tax_compensation_amount": 0,
"name": "Sprite Yoga Strap 10 foot",
"order_item_id": 3,
"parent_id": 4,
"price": 21,
"price_incl_tax": 22.74,
"product_id": 35,
"qty": 2,
"row_total": 42,
"row_total_incl_tax": 45.47,
"sku": "24-WG087",
"tax_amount": 3.47,
"weee_tax_applied": "[]",
"weee_tax_applied_row_amount": 0,
"weee_tax_row_disposition": 0
}
],
"comments": [],
"extension_attributes": {
"size_exchange": "1"
}
},
{
"adjustment": 0,
"adjustment_negative": 0,
"adjustment_positive": 0,
"base_adjustment": 0,
"base_adjustment_negative": 0,
"base_adjustment_positive": 0,
"base_currency_code": "USD",
"base_discount_amount": 0,
"base_grand_total": 1,
"base_discount_tax_compensation_amount": 0,
"base_shipping_amount": 1,
"base_shipping_incl_tax": 1,
"base_shipping_tax_amount": 0,
"base_subtotal": 0,
"base_subtotal_incl_tax": 0,
"base_tax_amount": 0,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 6,
"created_at": "2017-05-04 20:24:44",
"discount_amount": 0,
"entity_id": 7,
"global_currency_code": "USD",
"grand_total": 1,
"discount_tax_compensation_amount": 0,
"increment_id": "000000007",
"order_currency_code": "USD",
"order_id": 3,
"shipping_address_id": 5,
"shipping_amount": 1,
"shipping_incl_tax": 1,
"shipping_tax_amount": 0,
"state": 2,
"store_currency_code": "USD",
"store_id": 1,
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 0,
"subtotal_incl_tax": 0,
"tax_amount": 0,
"updated_at": "2017-05-04 20:24:44",
"items": [],
"comments": [],
"extension_attributes": {
"size_exchange": "1"
}
},
{
"adjustment": 0,
"adjustment_negative": 0,
"adjustment_positive": 0,
"base_adjustment": 0,
"base_adjustment_negative": 0,
"base_adjustment_positive": 0,
"base_currency_code": "USD",
"base_discount_amount": 0,
"base_grand_total": 1,
"base_discount_tax_compensation_amount": 0,
"base_shipping_amount": 1,
"base_shipping_incl_tax": 1,
"base_shipping_tax_amount": 0,
"base_subtotal": 0,
"base_subtotal_incl_tax": 0,
"base_tax_amount": 0,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 6,
"created_at": "2017-05-04 20:32:40",
"discount_amount": 0,
"entity_id": 15,
"global_currency_code": "USD",
"grand_total": 1,
"discount_tax_compensation_amount": 0,
"increment_id": "000000015",
"order_currency_code": "USD",
"order_id": 3,
"shipping_address_id": 5,
"shipping_amount": 1,
"shipping_incl_tax": 1,
"shipping_tax_amount": 0,
"state": 2,
"store_currency_code": "USD",
"store_id": 1,
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 0,
"subtotal_incl_tax": 0,
"tax_amount": 0,
"updated_at": "2017-05-04 20:32:40",
"items": [],
"comments": [],
"extension_attributes": {
"size_exchange": "1"
}
},
{
"adjustment": 0,
"adjustment_negative": 0,
"adjustment_positive": 0,
"base_adjustment": 0,
"base_adjustment_negative": 0,
"base_adjustment_positive": 0,
"base_currency_code": "USD",
"base_discount_amount": 0,
"base_grand_total": 1,
"base_discount_tax_compensation_amount": 0,
"base_shipping_amount": 1,
"base_shipping_incl_tax": 1,
"base_shipping_tax_amount": 0,
"base_subtotal": 0,
"base_subtotal_incl_tax": 0,
"base_tax_amount": 0,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 6,
"created_at": "2017-05-04 20:33:00",
"discount_amount": 0,
"entity_id": 16,
"global_currency_code": "USD",
"grand_total": 1,
"discount_tax_compensation_amount": 0,
"increment_id": "000000016",
"order_currency_code": "USD",
"order_id": 3,
"shipping_address_id": 5,
"shipping_amount": 1,
"shipping_incl_tax": 1,
"shipping_tax_amount": 0,
"state": 2,
"store_currency_code": "USD",
"store_id": 1,
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 0,
"subtotal_incl_tax": 0,
"tax_amount": 0,
"updated_at": "2017-05-04 20:33:00",
"items": [],
"comments": [],
"extension_attributes": {
"size_exchange": "1"
}
},
{
"adjustment": 0,
"adjustment_negative": 0,
"adjustment_positive": 0,
"base_adjustment": 0,
"base_adjustment_negative": 0,
"base_adjustment_positive": 0,
"base_currency_code": "USD",
"base_discount_amount": 0,
"base_grand_total": 1,
"base_discount_tax_compensation_amount": 0,
"base_shipping_amount": 1,
"base_shipping_incl_tax": 1,
"base_shipping_tax_amount": 0,
"base_subtotal": 0,
"base_subtotal_incl_tax": 0,
"base_tax_amount": 0,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 6,
"created_at": "2017-05-04 20:33:12",
"discount_amount": 0,
"entity_id": 17,
"global_currency_code": "USD",
"grand_total": 1,
"discount_tax_compensation_amount": 0,
"increment_id": "000000017",
"order_currency_code": "USD",
"order_id": 3,
"shipping_address_id": 5,
"shipping_amount": 1,
"shipping_incl_tax": 1,
"shipping_tax_amount": 0,
"state": 2,
"store_currency_code": "USD",
"store_id": 1,
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 0,
"subtotal_incl_tax": 0,
"tax_amount": 0,
"updated_at": "2017-05-04 20:33:12",
"items": [],
"comments": [],
"extension_attributes": {
"size_exchange": "1"
}
},
{
"adjustment": 0,
"adjustment_negative": 0,
"adjustment_positive": 0,
"base_adjustment": 0,
"base_adjustment_negative": 0,
"base_adjustment_positive": 0,
"base_currency_code": "USD",
"base_discount_amount": 0,
"base_grand_total": 1,
"base_discount_tax_compensation_amount": 0,
"base_shipping_amount": 1,
"base_shipping_incl_tax": 1,
"base_shipping_tax_amount": 0,
"base_subtotal": 0,
"base_subtotal_incl_tax": 0,
"base_tax_amount": 0,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 6,
"created_at": "2017-05-04 21:34:12",
"discount_amount": 0,
"entity_id": 18,
"global_currency_code": "USD",
"grand_total": 1,
"discount_tax_compensation_amount": 0,
"increment_id": "000000018",
"order_currency_code": "USD",
"order_id": 3,
"shipping_address_id": 5,
"shipping_amount": 1,
"shipping_incl_tax": 1,
"shipping_tax_amount": 0,
"state": 2,
"store_currency_code": "USD",
"store_id": 1,
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 0,
"subtotal_incl_tax": 0,
"tax_amount": 0,
"updated_at": "2017-05-04 21:34:12",
"items": [],
"comments": [],
"extension_attributes": {
"size_exchange": "1"
}
}
],
"search_criteria": {
"filter_groups": []
},
"total_count": 8
}
V1/creditmemo/16
{
"adjustment": 0,
"adjustment_negative": 0,
"adjustment_positive": 0,
"base_adjustment": 0,
"base_adjustment_negative": 0,
"base_adjustment_positive": 0,
"base_currency_code": "USD",
"base_discount_amount": 0,
"base_grand_total": 1,
"base_discount_tax_compensation_amount": 0,
"base_shipping_amount": 1,
"base_shipping_incl_tax": 1,
"base_shipping_tax_amount": 0,
"base_subtotal": 0,
"base_subtotal_incl_tax": 0,
"base_tax_amount": 0,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 6,
"created_at": "2017-05-04 20:33:00",
"discount_amount": 0,
"entity_id": 16,
"global_currency_code": "USD",
"grand_total": 1,
"discount_tax_compensation_amount": 0,
"increment_id": "000000016",
"order_currency_code": "USD",
"order_id": 3,
"shipping_address_id": 5,
"shipping_amount": 1,
"shipping_incl_tax": 1,
"shipping_tax_amount": 0,
"state": 2,
"store_currency_code": "USD",
"store_id": 1,
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 0,
"subtotal_incl_tax": 0,
"tax_amount": 0,
"updated_at": "2017-05-04 20:33:00",
"items": [],
"comments": [],
"extension_attributes": {
"size_exchange": "0"
}
}
As you can see, when I retrieve all creditmemos, all "size_exchange" are 1, but when I get only the creditmemo with id=16, the value of that attribute is 0.
解决方案
I solved my problem, just creating a plugin:
etc/di.xml
<type name="Magento\Sales\Api\Data\CreditmemoInterface">
<plugin name="AddCreditmemoExtensionAttributes"
type="Vendor\Module\Plugin\AddCreditmemoExtensionAttributes"/>
</type>
Plugin/AddCreditmemoExtensionAttributes.php
public function afterGetExtensionAttributes(
CreditmemoInterface $creditmemo,
CreditmemoExtensionInterface $extension = null
)
{
if ($extension === null) {
$extension = $this->extensionFactory->create();
}
foreach ($this->fields as $key) {
$data = $creditmemo->getData($key) ? $creditmemo->getData($key) : '0';
$extension->setData($key, $data);
}
return $extension;
}
And working like a charm :)