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

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago