How To

Magento 2: How to Get Custom Attribute Value of Shipment Using REST API

Hello Magento Friends,

In today’s blog, I will explain How to Get a Custom Attribute Value of Shipment using REST API in Magento 2.

In Magento 2, a shipment represents the process of sending goods to customers. Shipment entities are rich with data, and businesses often extend these entities with custom attributes to capture additional information relevant to their operations. These custom attributes could range from batch numbers to packaging details.

The REST API in Magento 2 provides a standardized way to interact with the platform, allowing developers to create, read, update, and delete various entities, including shipments.

Let’s find out How to Get Custom Attribute Value of Shipment using REST API in Magento 2.

Steps to Get Custom Attribute Value of Shipment using REST API in Magento 2:

Step 1: Create extension_attributes.xml file inside etc folder

app\code\Vendor\Extension\etc\

And add the below code

<?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\ShipmentInterface">
        <attribute code="custom_attribute" type="string" />
         <attribute code="secondcustom_attribute" type="string" />
    </extension_attributes>
</config>

Step 2: Now, create di.xml file inside the etc folder

app\code\Vendor\Extension\etc\

Then add the code as follows

<?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\Sales\Api\ShipmentRepositoryInterface">
        <plugin name="shipmentInformationUpdate" type="Vendor\Extension\Plugin\Api\ShipmentRepository" />
    </type>
</config>

Step 3: Then create ShipmentRepository.php file inside the Plugin folder.

app\code\Vendor\Extension\Plugin\Api\

After that, include the below-mentioned code

<?php
namespace Vendor\Extension\Plugin\Api;

use Magento\Sales\Api\Data\ShipmentExtensionFactory;
use Magento\Sales\Api\Data\ShipmentInterface;
use Magento\Sales\Api\Data\ShipmentSearchResultInterface;

class ShipmentRepository
{
     private $feeDataFields = [
        'custom_attribute',
        'secondcustom_attribute'
     
    ];

    protected $extensionFactory;

    public function __construct(ShipmentExtensionFactory $extensionFactory)
    {
        $this->extensionFactory = $extensionFactory;
    }

    public function afterGet(
        \Magento\Sales\Api\ShipmentRepositoryInterface $subject,
        ShipmentInterface $shipment
    ) {
        $extensionAttributes = $shipment->getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();

        foreach ($this->feeDataFields as $key) {
            $data = $shipment->getData($key) ? $shipment->getData($key) : '0.0000';
            $extensionAttributes->setData($key, $data);
        }
            $shipment->setExtensionAttributes($extensionAttributes);
            
        return $shipment;
    }

    public function afterGetList(\Magento\Sales\Api\ShipmentRepositoryInterface $subject, ShipmentSearchResultInterface $searchResult)
    {
        $shipments = $searchResult->getItems();

        foreach ($shipments as &$shipment) {
            $extensionAttributes = $shipment->getExtensionAttributes();
            $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();

        foreach ($this->feeDataFields as $key) {

            $data = $shipment->getData($key) ? $shipment->getData($key) : '0.0000';
            $extensionAttributes->setData($key, $data);
        }
        $shipment->setExtensionAttributes($extensionAttributes);
        }

        return $searchResult;
    }
}

Step 4: Now, need to run the below command

php bin/magento setup:upgrade

Then, you can see the values ​​of the custom attributes from the URL below.

BASE_URL/rest/all/V1/shipment/increment_id

Conclusion:

By following the steps outlined in this blog post, you can unlock the full potential of Magento 2 and provide a more personalized and efficient e-commerce experience for your customers. 

Also find out,

If you have any doubt share it with me through the comment section.

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
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

Mastering Tailwind CSS in Laravel: A Comprehensive Guide

Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…

1 day ago

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

3 days ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

6 days ago

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…

1 week ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

1 week ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

1 week ago