Site icon MageComp Blog

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

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!

Exit mobile version