Hello Magento Friends,
Welcome to MageComp’s “How To” Magento Blog series. Today we will find out, How to Get Quote Details with Custom Extension Attributes value using REST API In Magento 2.
When the customer sends a quote for any product, it is essential for Magento 2 store owners to get quote information. Handle quote requests smoothly by including Email Quote Pro Extension for Magento 2. REST API is used to fetch data. Hence, you can use REST API to get quote details. Previously we have discussed How to Get Quote Information using REST API in Magento 2.
But if we have used custom extension attributes, default Magento does not return its value. To get quote Details with Custom Extension Attributes value using REST API in Magento 2, use the below steps.
Contents
Steps to Get Quote Details with Custom Extension Attributes value using REST API In Magento 2:
Step 1: Firstly, go to the below path
app\code\Vendor\Extension\etc\extension_attributes.xml
And add the code as follows:
1 2 3 4 5 6 |
<?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\CartInterface"> <attribute code="customattribute" type="string" /> </extension_attributes> </config> |
Step 2: Now go to the below path
app\code\Vendor\Extension\etc\di.xml
And add the code as given below:
1 2 3 4 5 6 |
<?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\CartRepositoryInterface"> <plugin name="cart_extra_attribute" type="Vendor\Extension\Plugin\Cartrepositoryplugin" /> </type> </config> |
Step 3: Then, move to the below path
app\code\Vendor\Extension\Plugin\Cartrepositoryplugin.php
And add the code as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php namespace Vendor\Extension\Plugin; use Magento\Quote\Api\Data\CartExtensionFactory; use Magento\Quote\Api\Data\CartInterface; use Magento\Quote\Api\Data\CartSearchResultInterface; use Magento\Quote\Api\CartRepositoryInterface; use Magento\Framework\Exception\CouldNotSaveException; class Cartrepositoryplugin { protected $extensionFactory; public function __construct(CartExtensionFactory $extensionFactory) { $this->extensionFactory = $extensionFactory; } public function afterGet(CartRepositoryInterface $subject, CartInterface $quote) { $customattribute = $quote->getData('customattribute'); $extensionAttributes = $quote->getExtensionAttributes(); $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create(); $extensionAttributes->setData('customattribute',$customattribute); $quote->setExtensionAttributes($extensionAttributes); return $quote; } } |
Step 4: Lastly, create getquotedetails.php in the Magento root directory and add the below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $admindata = array("username" => "admin", "password" => "admin@123"); $baseUrl = "http://yourdomain"; // your magento base url $ch = curl_init($baseUrl."/rest/V1/integration/admin/token"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($admindata)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($admindata)))); $token = curl_exec($ch); $ch = curl_init($baseUrl."/rest/V1/carts/1"); // change cart id curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token))); $result = curl_exec($ch); $result = json_decode($result, 1); echo '<pre>'; print_r($result); ?> |
After that run URL as below:
https://yourdomain/getquotedetails.php
Conclusion:
Hence, with the help of the above steps, you can easily Get Quote Details with Custom Extension Attributes value using REST API In Magento 2. If you face any problem with the above code, let me know in the comment and I will help you out. Do share the article with your friends. Stay updated for more such Magento solutions.
Happy Coding!