How to Get Weight Attribute in Totals Cart REST API Magento 2.X
-
14-03-2021 - |
Question
How Can i Get Weight Attribute in Cart REST API.
End Point: /rest/V1/carts/mine/
Response Default:
{
"id": 8,
"created_at": "2019-09-16 13:51:25",
"updated_at": "2019-09-18 12:07:31",
"is_active": true,
"is_virtual": false,
"items": [
{
"item_id": 7,
"sku": "Test",
"qty": 2,
"name": "Test",
"price": 100,
"product_type": "simple",
"quote_id": "8"
},
{
"item_id": 8,
"sku": "Test Bundle",
"qty": 1,
"name": "Test Bundle",
"price": 200,
"product_type": "bundle",
"quote_id": "8"
}
],
"items_count": 2,
"items_qty": 3,
"customer": {
"id": 4,
"group_id": 1,
"created_at": "2019-09-16 13:48:02",
"updated_at": "2019-09-21 15:01:53",
"created_in": "Default Store View",
"email": "test@testmail.com",
"firstname": "elantest",
"lastname": "m",
"store_id": 1,
"website_id": 1,
"addresses": [],
"disable_auto_group_change": 0,
"extension_attributes": {
"is_subscribed": false
},
"custom_attributes": [
{
"attribute_code": "mobile_number",
"value": "+910000000000"
}
]
},
"billing_address": {
"id": 248,
"region": null,
"region_id": null,
"region_code": null,
"country_id": null,
"street": [
""
],
"telephone": null,
"postcode": null,
"city": null,
"firstname": null,
"lastname": null,
"customer_id": 4,
"email": "test@testmail.com",
"same_as_billing": 0,
"save_in_address_book": 0
},
"orig_order_id": 0,
"currency": {
"global_currency_code": "INR",
"base_currency_code": "INR",
"store_currency_code": "INR",
"quote_currency_code": "INR",
"store_to_base_rate": 0,
"store_to_quote_rate": 0,
"base_to_global_rate": 1,
"base_to_quote_rate": 1
},
"customer_is_guest": false,
"customer_note_notify": true,
"customer_tax_class_id": 3,
"store_id": 1,
"extension_attributes": {
"shipping_assignments": [
{
"shipping": {
"address": {
"id": 53,
"region": null,
"region_id": null,
"region_code": null,
"country_id": null,
"street": [
""
],
"telephone": null,
"postcode": null,
"city": null,
"firstname": null,
"lastname": null,
"customer_id": 4,
"email": "test@testmail.com",
"same_as_billing": 0,
"save_in_address_book": 0
},
"method": null
},
"items": [
{
"item_id": 7,
"sku": "Test",
"qty": 2,
"name": "Test",
"price": 100,
"product_type": "simple",
"quote_id": "8"
},
{
"item_id": 8,
"sku": "Test Bundle",
"qty": 1,
"name": "Test Bundle",
"price": 200,
"product_type": "bundle",
"quote_id": "8"
}
]
}
]
}
}
I need to Get Weight Attribute for each item.
Solution
You can follow below solution to achieve this.
Vendor\Module\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\Quote\Api\Data\CartItemInterface">
<attribute code="item_weight" type="float" />
</extension_attributes>
</config>
Vendor\Module\etc\di.xml
<?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\Quote\Api\CartManagementInterface">
<plugin name="set_item_weight" type="Vendor\Module\Plugin\QuoteItem"/>
</type>
</config>
QuoteItem.php
namespace Vendor\Module\Plugin;
class QuoteItem {
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterfaceFactory $productRepository,
\Magento\Quote\Api\Data\CartItemExtensionInterfaceFactory $cartItemExtensionFactory
){
$this->productRepository = $productRepository;
$this->cartItemExtension = $cartItemExtensionFactory;
}
public function afterGetCartForCustomer(\Magento\Quote\Api\CartManagementInterface $subject, $result)
{
foreach($result->getItems() as $item)
{
$product = $this->productRepository->create()->get($item->getSku());
$extensionAttributes = $item->getExtensionAttributes();
if ($extensionAttributes === null) {
$extensionAttributes = $this->cartItemExtension->create();
}
$extensionAttributes->setItemWeight($product->getWeight());
$item->setExtensionAttributes($extensionAttributes);
}
return $result;
}
}
Let me know if you need further help. Happy Coding :)
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange