I need to update the shipping address for a placed order.
curl --request PUT \
--url {base_url}/rest/V1/orders/177962/ \
--header 'authorization: Bearer xpto' \
--header 'content-type: application/json' \
--data '{
"entity": {
"entity_id": 123456,
"postcode": "01301000"
}
}
'
RESPONSE:
{
"message": "Não foi possível salvar o endereço do pedido",
"trace": "#0 [internal function]: Magento\\Sales\\Model\\Order\\AddressRepository->save(Object(Magento\\Sales\\Model\\Order\\Address\\Interceptor))\n#1 \/home\/154258.cloudwaysapps.com\/vfeefatesn\/public_html\/vendor\/magento\/module-webapi\/Controller\/Rest.php(330): call_user_func_array(Array, Array)\n#2 \/home\/154258.cloudwaysapps.com\/vfeefatesn\/public_html\/vendor\/magento\/module-webapi\/Controller\/Rest.php(239): Magento\\Webapi\\Controller\\Rest->processApiRequest()\n#3 \/home\/154258.cloudwaysapps.com\/vfeefatesn\/public_html\/vendor\/magento\/framework\/Interception\/Interceptor.php(58): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#4 \/home\/154258.cloudwaysapps.com\/vfeefatesn\/public_html\/vendor\/magento\/framework\/Interception\/Interceptor.php(138): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callParent('dispatch', Array)\n#5 \/home\/154258.cloudwaysapps.com\/vfeefatesn\/public_html\/app\/code\/Innersite\/Webservice\/Model\/Plugin\/Webapi\/Controller\/Rest.php(22): Magento\\Webapi\\Controller\\Rest\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n#6 \/home\/154258.cloudwaysapps.com\/vfeefatesn\/public_html\/vendor\/magento\/framework\/Interception\/Interceptor.php(135): Innersite\\Webservice\\Model\\Plugin\\Webapi\\Controller\\Rest->aroundDispatch(Object(Magento\\Webapi\\Controller\\Rest\\Interceptor), Object(Closure), Object(Magento\\Framework\\App\\Request\\Http))\n#7 \/home\/154258.cloudwaysapps.com\/vfeefatesn\/public_html\/vendor\/magento\/framework\/Interception\/Interceptor.php(153): Magento\\Webapi\\Controller\\Rest\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n#8 \/home\/154258.cloudwaysapps.com\/vfeefatesn\/public_html\/generated\/code\/Magento\/Webapi\/Controller\/Rest\/Interceptor.php(26): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callPlugins('dispatch', Array, Array)\n#9 \/home\/154258.cloudwaysapps.com\/vfeefatesn\/public_html\/vendor\/magento\/framework\/App\/Http.php(135): Magento\\Webapi\\Controller\\Rest\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#10 \/home\/154258.cloudwaysapps.com\/vfeefatesn\/public_html\/generated\/code\/Magento\/Framework\/App\/Http\/Interceptor.php(24): Magento\\Framework\\App\\Http->launch()\n#11 \/home\/154258.cloudwaysapps.com\/vfeefatesn\/public_html\/vendor\/magento\/framework\/App\/Bootstrap.php(256): Magento\\Framework\\App\\Http\\Interceptor->launch()\n#12 \/home\/154258.cloudwaysapps.com\/vfeefatesn\/public_html\/index.php(66): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http\\Interceptor))\n#13 {main}"
}
You can use following custom API to update shipping or billing address of placed order:
Create webapi.xml
<?xml version="1.0"?> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd"> <route method="POST" url="/V1/setaddress/"> <service class="<vendor>\<modelname>\Api\SetAddressInterface" method="setAddress"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes>
Add following code with di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="<vendor>\<modelname>\Api\SetAddressInterface" type="<vendor>\<modelname>\Model\SetAddress"/>
</config>Create API Interface:
<?php
namespace <vendor>\<modelname>\Api;
interface SetAddressInterface
{
/**
*
* @param string $entity_id
* @param string $parent_id
* @param string $address_type
* @param string $region
* @param string $region_id
* @param string $postcode
* @param string $firstname
* @param string $lastname
* @param string $middlename
* @param string $company
* @param string $country_id
* @param string $telephone
* @param string $prefix
* @param string $street
* @return string
*/
public function setAddress($entity_id,$parent_id,$address_type,$region,$region_id,$postcode,$firstname,$lastname,$middlename,$company,$country_id,$telephone,$prefix,$street);
}Create model file :
<?php
namespace <vendor>\<modelname>\Model;
class SetAddress implements \<vendor>\<modelname>\Api\SetAddressInterface
{
/**
*
* @param string $entity_id
* @param string $parent_id
* @param string $address_type
* @param string $region
* @param string $region_id
* @param string $postcode
* @param string $firstname
* @param string $lastname
* @param string $middlename
* @param string $company
* @param string $country_id
* @param string $telephone
* @param string $prefix
* @param string $street
* @return string
*/
public function setAddress($entity_id,$parent_id,$address_type,$region,$region_id,$postcode,$firstname,$lastname,$middlename,$company,$country_id,$telephone,$prefix,$street){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderAddress = $objectManager->create('Magento\Sales\Model\Order\Address');
$collection = $orderAddress->getCollection();
$collection->addFieldToFilter('entity_id',['eq'=>$entity_id]);
$collection->addFieldToFilter('parent_id',['eq'=>$parent_id]);
foreach($collection as $item)
{
$item->setRegion($region);
$item->setRegion($region_id);
$item->setPostCode($postcode);
$item->setFirstName($firstname);
$item->setLastName($lastname);
$item->setMiddleName($middlename);
$item->setCompany($company);
$item->setCountryId($country_id);
$item->setTelephone($telephone);
$item->setPrefix($prefix);
$item->setStreet($street);
}
$collection->save();
return "Addres Updated sucessfully";
}
}
NOTE: "entity_id" is address id of order and "parent_id" is order entity id.
Issue resolved ?
Please click on Kudos, and accept as Solution!