1

I'm using the following:

POST /V1/carts/{cartId}/shipping-information

POST /V1/carts/{cartId}/billing-address

My idea is to have a form that allows people to select one of the addresses already associated with their account. In one example I have a customer with two addresses that have IDs of 31 and 32.

Rather then populate all the address details on the above fields, I was just wanting to pass an ID to say "use address 32" for Billing e.g.

[
    'address' => [
                    'id' => 32
                ]
]

I've played around with a few different arguments but no luck - is it even possible? Or, do I just need to use the address book to pre-populate all the standard fields?

1 Answer 1

2

That won't work with the existing endpoint. The webapi validates the request based on the phpdocs defined for the method in the service class. for Magento\Checkout\Api\ShippingInformationManagementInterface::saveAddressInformation the request/response requirements are:

/**
 * @param int $cartId
 * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
 * @return \Magento\Checkout\Api\Data\PaymentDetailsInterface
 */

If you don't want to populate the address object you could create your own service to meet your requirements. For the shipping address it would look something similar to the following. Although you may also need to implement a separate service for setting the shipping method information. The shipping method information is included with the address information in the current service and used for recalculating totals and return available payment methods in the billing/payment step.

Vendor/Module/etc/webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/carts/:cartId/shipping-information-by-id" method="POST">
        <service class="Vendor\Module\Api\ShippingInformationByIdManagementInterface" method="saveAddressInformation"/>
        <resources>
            <resource ref="Magento_Cart::manage" />
        </resources>
    </route>
</routes>

Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Vendor\Module\Api\ShippingInformationByIdManagementInterface" type="Vendor\Module\Model\ShippingInformationByIdManagement" />
</config>

Vendor/Module/Api/ShippingInformationByIdManagementInterface.php

<?php

namespace Vendor\Module\Api;

/**
 * Interface for managing customer shipping address information by id
 * @api
 */
interface ShippingInformationByIdManagementInterface
{
    /**
     * @param int $cartId
     * @param int $addressId
     * @return \Magento\Checkout\Api\Data\PaymentDetailsInterface
     */
    public function saveAddressInformation(
        $cartId,
        $addressId
    );
}

Vendor/Module/Model/ShippingInformationByIdManagement.php

<?php

namespace Vendor\Module\Model;

class ShippingInformationByIdManagementManagement implements \Vendor\Module\Api\ShippingInformationByIdManagementInterface
{
    /**
     * {@inheritDoc}
     */
    public function saveAddressInformation(
        $cartId,
        $addressId
    ) {
        /**
         * Your custom logic for saving the address by id
         */
    }
}
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.