Shipping Address API: Customer Shipping Details Using API in Magento 2

I am going to explain how you can get the customer details by using customer shipping address API integration.

Our Magento development team has developed a module that helps you to retrieve the information of customer ID.

So by using this module help you through the entire shipping address information to show on the frontend on any kind of website like you can show it on react js website or node js website.

Magento 2 Shipping Address API

 

Why Shipping Address APIs?

No more complicated code is required for integration.

Wishusucess offers you to get customer shipping orders information by using the ShippingAddressApi.

You just have to call the API on your other website and then you will get the details of your customer ID.

 

Step 1: Shipping API Registration

app/code/Wishusucess/ShippingAddress/registration.php

<?php
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_ShippingAddress
* Website: http://www.wishusucess.com/
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Wishusucess_ShippingAddress',
__DIR__
);

 

Step 2: Module Name & Information

app/code/Wishusucess/ShippingAddress/etc/module.xml

<?xml version="1.0"?>
<!--
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_ShippingAddress
* Website: http://www.wishusucess.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Wishusucess_ShippingAddress" setup_version="1.0.9" />
</config>

 

Step 3: Give Preference of API

This dependency injection file decide the dependency of your custom module. So this module has custom dependencies that going to injected by the object manager. We can also configure the setting but here that is not necessary.

app/code/Wishusucess/ShippingAddress/etc/di.xml

<?xml version="1.0"?>
<!--
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_ShippingAddress
* Website: http://www.wishusucess.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Wishusucess\ShippingAddress\Api\CustomInterface" type="Wishusucess\ShippingAddress\Model\Api\Custom"/>
</config>

 

Step 3: Route Method And URL

Route URL used to decide the URL of your API and method decide what kind of operation we are going to perform so, here i have given /V1/wishusucess/shippingaddress/ route URL and i am using GET method to receive the information using API calls.

app/code/Wishusucess/ShippingAddress/etc/webapi.xml

<?xml version="1.0"?>
<!--
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_ShippingAddress
* Website: http://www.wishusucess.com/
*/
-->
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
<route method="GET" url="/V1/wishusucess/shippingaddress/">
<service class="Wishusucess\ShippingAddress\Api\CustomInterface" method="getPost"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>

 

Step 4: Decide Route ID

Here you can decide the frontend id of your shipping address module by using the route id, So i have given route id shipping for this module.

app/code/Wishusucess/ShippingAddress/etc/frontend/routes.xml

<?xml version="1.0"?>
<!--
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_ShippingAddress
* Website: http://www.wishusucess.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="shipping" frontName="shipping">
<module name="Wishusucess_ShippingAddress" />
</route>
</router>
</config>

 

Step 5: Shipping Address API Model Class

app/code/Wishusucess/ShippingAddress/Model/Api/Custom.php

<?php
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_ShippingAddress
* Website: http://www.wishusucess.com/
*/
namespace Wishusucess\ShippingAddress\Model\Api;

use Psr\Log\LoggerInterface;

class Custom 
{
/** @var \Magento\Framework\View\Result\PageFactory */
protected $resultPageFactory;
protected $_customerFactory;
protected $_addressFactory;

public function __construct(
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\AddressFactory $addressFactory,
array $data = []
) {
$this->resultPageFactory = $resultPageFactory;
$this->_customerFactory = $customerFactory;
$this->_addressFactory = $addressFactory;



}

/**
* @inheritdoc
*/

public function getPost($customerId)
{
$response = ['success' => false];

try {
$customer = $this->_customerFactory->create()->load($customerId); //insert customer id

//billing
$billingAddressId = $customer->getDefaultBilling();
$billingAddress = $this->_addressFactory->create()->load($billingAddressId);


//shipping
$shippingAddressId = $customer->getDefaultShipping();
$shippingAddress = $this->_addressFactory->create()->load($shippingAddressId);

$field_data['ShippingAddress']=$shippingAddress->getData();
$field_data['BillingAddress']=$billingAddress->getData();

$response= $field_data;
// print_r($cart);


} catch (\Exception $e) {
$response = ['success' => false, 'message' => $e->getMessage()];
}
$returnArray = $response;
return $returnArray; 
}
}

 

Step 6: Controller To Execute API

Now here we have to get the customer id and then initialize that customer id in $customerId and then we have to load that customer id in $customer now we can find the customer shipping information and customer billing information from $customer.

Then execute() method will return after the execution of the getDefaultBilling() and getDefaultShipping().

app/code/Wishusucess/ShippingAddress/Controller/Index/Index.php

<?php
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_ShippingAddress
* Website: http://www.wishusucess.com/
*/
namespace Wishusucess\ShippingAddress\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action {

/** @var \Magento\Framework\View\Result\PageFactory */
protected $resultPageFactory;
protected $_customerFactory;
protected $_addressFactory;
protected $_data;


public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\AddressFactory $addressFactory,
array $data = []
) {
$this->_customerFactory = $customerFactory;
$this->_data = $data;
parent::__construct($context);
}

public function execute() {
$customerId=41;
$customer = $this->_customerFactory->create()->load($customerId); //insert customer id

//billing
$billingAddressId = $customer->getDefaultBilling();
$billingAddress = $this->_addressFactory->create()->load($billingAddressId);


//shipping
$shippingAddressId = $customer->getDefaultShipping();
$shippingAddress = $this->_addressFactory->create()->load($shippingAddressId);


echo "<pre>";

print_r($customer->getData());

echo "------------------------";
// print_r($shippingAddress->getData());

die();

}

}

 

Step 7: API for Custom Interface

app/code/Wishusucess/ShippingAddress/Api/CustomInterface.php

<?php
/**
*
* Developer: Hemant Singh Magento Certified Developer
* Category: Wishusucess_ShippingAddress
* Website: http://www.wishusucess.com/
*/
namespace Wishusucess\ShippingAddress\Api;

interface CustomInterface
{
/**
* GET for Post api
* @param string $customerId Customer id.
* @return boolean|array
*/

public function getPost($customerId);
}

 

So when you open your customer account summary on the Magento 2 website, which appears on the right of the Account Details page. It shows shipping addresses and billing addresses that also have the manage addresses on the customer page.

So by using this module you can get all these details through API calls on your other site. and from there you can edit the information, delete the information and add the new address by using different methods.

Here I have used only the GET method to retrieve the information but in order to perform other operations you have to use POST, DELETE, PUT, SAVE etc as per your requirement.

 

Download API

Related Post:

Latest Products API: Magento 2 Get Latest Products Using REST API Call

CMS REST API: Retrieve Magento 2 CMS Pages & Block Data Using API

 

Recommend Post:

Elasticsearch: How to Install Elasticsearch in Ubuntu 18.04