WooCommerce仅允许拉斯塔努拉地区用户结账的实现问题
Hey there! I see you’ve already laid some solid groundwork locking down default checkout fields, but the issue is users can still bypass those restrictions to place orders. Let’s fix this by adding validation checks, hiding payment methods for non-eligible users, and adding upfront notices to guide customers clearly.
Here’s the complete set of code you can add to your OceanWP theme’s functions.php file (pro tip: use a child theme to avoid losing changes when the theme updates):
1. Validate Checkout Fields & Block Invalid Orders
This code checks submitted billing/shipping details during checkout and throws an error if the user isn’t in Ras Tanura, stopping them from completing the order:
// Validate checkout fields to ensure only Ras Tanura is allowed add_action('woocommerce_checkout_process', 'validate_rastanura_checkout_location'); function validate_rastanura_checkout_location() { // Check billing address if (isset($_POST['billing_state']) && $_POST['billing_state'] !== 'RT') { wc_add_notice(__('We only deliver to Ras Tanura. Please confirm your billing location.', 'woocommerce'), 'error'); } if (isset($_POST['billing_city']) && $_POST['billing_city'] !== 'Ras Tanura') { wc_add_notice(__('We only deliver to Ras Tanura. Please confirm your billing location.', 'woocommerce'), 'error'); } // Check shipping address if shipping is required if (WC()->cart->needs_shipping()) { if (isset($_POST['shipping_state']) && $_POST['shipping_state'] !== 'RT') { wc_add_notice(__('We only deliver to Ras Tanura. Please confirm your shipping location.', 'woocommerce'), 'error'); } if (isset($_POST['shipping_city']) && $_POST['shipping_city'] !== 'Ras Tanura') { wc_add_notice(__('We only deliver to Ras Tanura. Please confirm your shipping location.', 'woocommerce'), 'error'); } } }
2. Hide Payment Methods for Non-Ras Tanura Users
Add an extra layer of protection by hiding all payment methods for users outside Ras Tanura—they won’t even see the option to pay:
// Restrict payment gateways to only Ras Tanura customers add_filter('woocommerce_available_payment_gateways', 'restrict_payment_gateways_to_rastanura'); function restrict_payment_gateways_to_rastanura($gateways) { // Only apply this on checkout and order-pay pages if (!is_checkout() && !is_wc_endpoint_url('order-pay')) { return $gateways; } $customer = WC()->customer; $is_eligible = ( $customer->get_billing_state() === 'RT' && $customer->get_billing_city() === 'Ras Tanura' ); // Include shipping check if cart requires shipping if (WC()->cart->needs_shipping()) { $is_eligible = $is_eligible && ( $customer->get_shipping_state() === 'RT' && $customer->get_shipping_city() === 'Ras Tanura' ); } // If not eligible, remove all payment gateways if (!$is_eligible) { $gateways = array(); } return $gateways; }
3. Add a Cart Page Notice (Optional but User-Friendly)
Inform users early on before they reach checkout with this cart page notice if their location isn’t Ras Tanura:
// Show notice on cart page for non-Ras Tanura users add_action('woocommerce_cart_totals_after_order_total', 'rastanura_cart_location_notice'); function rastanura_cart_location_notice() { $customer = WC()->customer; if ($customer->get_billing_state() !== 'RT' || $customer->get_billing_city() !== 'Ras Tanura') { echo '<div class="woocommerce-error" style="padding: 1rem; margin-top: 1rem;">'; echo __('We only deliver to Ras Tanura. Please update your billing location to proceed with checkout.', 'woocommerce'); echo '</div>'; } }
How This Works Together:
- The validation function stops invalid orders at checkout with clear error messages.
- The payment gateway restriction removes the ability to pay entirely for non-eligible users.
- The cart notice guides users to correct their location before they even attempt checkout.
Add these functions alongside your existing code (or replace your current snippets with this full set) and test it out—users outside Ras Tanura won’t be able to complete orders anymore!
内容的提问来源于stack exchange,提问作者www

