How To

How to Get Quote Details with Custom Extension Attributes value using REST API In Magento 2

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.

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:

<?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:

<?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:

<?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:

<?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!

Click to rate this post!
[Total: 2 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

2 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

4 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

5 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

5 days ago