Hello Magento Friends,
In today’s blog, we will learn about How to Get Custom Column Value of Shipment using GraphQL in Magento 2.
In Magento 2, shipments represent the process of delivering orders to customers. While Magento offers a plethora of default attributes for shipments, there are instances where merchants need to include custom data fields to streamline their logistics or integrate with third-party systems. These custom columns could contain information such as delivery instructions, special packaging requirements, or additional tracking details.
Now, let’s dive into the practical aspect of accessing custom column values of shipments using GraphQL in Magento 2.
Steps to Get Custom Column Value of Shipment using GraphQL in Magento 2:
Step 1: First, we need to create “schema.graphqls” file inside the etc folder.
app\code\Vendor\Extension\etc\
Then add the code as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
type Query { customerOrders: CustomerOrders @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\Orders") @deprecated(reason: "Use the `customer` query instead.") @cache(cacheable: false) } type CustomerOrders @doc(description: "The collection of orders that match the conditions defined in the filter.") { items: [CustomerOrder]! @doc(description: "An array of customer orders.") page_info: SearchResultPageInfo @doc(description: "Contains pagination metadata.") total_count: Int @doc(description: "The total count of customer orders.") } type CustomerOrder { shipments: [OrderShipment] @doc(description: "A list of shipments for the order.") @resolver(class: "Vendor\\Extension\\Model\\Resolver\\Shipments") } type OrderShipment @doc(description: "Contains shipment details.") { custom_fee: Float @deprecated(reason: "Use the `custom_fee` field instead.") } |
Step 2: After that, we must create a “Shipments.php” file inside the Resolver folder.
app\code\Vendor\Extension\Model\Resolver\
And add the code as given 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ declare(strict_types=1); namespace Vendor\Extension\Model\Resolver; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Sales\Api\Data\ShipmentInterface; use Magento\Sales\Model\Order; /** * Resolve shipment information for order */ class Shipments implements ResolverInterface { /** * @inheritDoc */ public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) { if (!isset($value['model']) && !($value['model'] instanceof Order)) { throw new LocalizedException(__('"model" value should be specified')); } /** @var Order $order */ $order = $value['model']; $shipments = $order->getShipmentsCollection()->getItems(); if (empty($shipments)) { //Order does not have any shipments return []; } $orderShipments = []; foreach ($shipments as $shipment) { $orderShipments[] = [ 'id' => base64_encode($shipment->getIncrementId()), 'number' => $shipment->getIncrementId(), 'comments' => $this->getShipmentComments($shipment), 'custom_fee' => $shipment->getCustomFee(), 'model' => $shipment, 'order' => $order ]; } return $orderShipments; } /** * Get comments shipments in proper format * * @param ShipmentInterface $shipment * @return array */ private function getShipmentComments(ShipmentInterface $shipment): array { $comments = []; foreach ($shipment->getComments() as $comment) { if ($comment->getIsVisibleOnFront()) { $comments[] = [ 'timestamp' => $comment->getCreatedAt(), 'message' => $comment->getComment() ]; } } return $comments; } } |
Step 3: At last run the upgrade command and compile command then pass the query like below in the server
1 2 3 4 5 6 7 8 9 10 11 12 |
{ customerOrders { items { order_number id shipments{ custom_fee } } } } |
Output:
Conclusion:
This way you can retrieve the custom column value of shipment using GraphQL in Magento 2. If you have any doubts, share them with me through the comment section and I will be quick to provide you with a solution. Share the article with your friends, and stay in touch with us for more such Magento 2 tutorials.
Also learn-
Magento 2: How to Get Custom Column Value of Invoice and Credit Memo using GraphQL
Magento 2: How to Get Custom Column Value of Order using GraphQL
Happy Coding!