WeChat provides a shared shipping address feature that allows users to select and retrieve shipping address information directly within the public account. PHP developers can leverage the WeChat API to easily access user shipping address data.
The process of retrieving WeChat shared shipping addresses involves two main steps: authorization and data retrieval. Below is the detailed procedure:
When a user clicks the shared shipping address button for the first time, they need to be guided through the authorization process. By calling the openAddress interface in WeChat's JS-SDK, the authorization popup will be triggered.
wx.openAddress({<br> success: function(res) {<br> // Handle the logic when the user successfully authorizes<br> },<br> cancel: function() {<br> // Handle the logic when the user cancels the authorization<br> },<br> fail: function() {<br> // Handle the logic when authorization fails<br> }<br>});
Once the user successfully authorizes, WeChat will return a JSON object containing the user's shipping address information. You can receive and parse this object on the backend to obtain the address details.
$jsonStr = $_POST['addressInfo']; // Get the shipping address info from the frontend<br>$addressInfo = json_decode($jsonStr, true); // Parse the JSON string<br>$userName = $addressInfo['userName']; // User's name<br>$postalCode = $addressInfo['postalCode']; // Postal code<br>$provinceName = $addressInfo['provinceName']; // Province<br>$cityName = $addressInfo['cityName']; // City<br>$countyName = $addressInfo['countyName']; // District/County<br>$detailInfo = $addressInfo['detailInfo']; // Detailed address<br>$nationalCode = $addressInfo['nationalCode']; // Country code<br>$telNumber = $addressInfo['telNumber']; // Phone number
The above code shows how to retrieve the user's shipping address and save it to a database or perform other relevant actions.
When developing the WeChat shared shipping address feature, be sure to keep the following points in mind:
Before using the WeChat JS-SDK, ensure that the developer account is linked to the public account and the JS-SDK permissions are properly configured.
During development, you must configure a valid JS interface security domain in the public account settings to ensure the feature works correctly.
Be sure to handle various potential errors, such as when the user cancels or rejects authorization.
With the WeChat JS-SDK interface, PHP developers can easily implement the feature to retrieve shared shipping addresses. During development, make sure to configure the JS-SDK permissions, security domain, and handle possible errors. We hope this article helps developers successfully implement this feature.