为了防止客户在 WooCommerce 中完成结帐,我们的想法是连接 WooCommerce 的结帐验证系统,并在邮政编码超出您的送货区域时停止结帐过程。
add_action('woocommerce_after_checkout_validation', 'validate_postcode_before_checkout');
function validate_postcode_before_checkout($posted) {
// Get the billing postcode from the checkout form
$postcode = isset($posted['billing_postcode']) ? $posted['billing_postcode'] : '';
// Call the Delivery Checker API to verify if the postcode is in the delivery area
$response = wp_remote_get("https://cdn.deliverychecker.co/api/v1/check-postcode?postcode=" . urlencode($postcode) . "&widget-id=166f5152dc8d7b");
if (is_wp_error($response)) {
wc_add_notice(__('Could not verify your postcode. Please try again later.'), 'error');
return;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
// Check the response for a valid delivery area
if (isset($data['status']) && $data['status'] === 'error') {
wc_add_notice(__('Sorry, we do not deliver to your postcode.'), 'error');
}
}
我希望这对你有用。