如果您想在 WooCommerce 中启用此类功能, 客户需要注册并登录首先,因为这是在结账过程之前获取他们城镇位置的唯一方法。
此外,您还必须更改 WooCommerce 中的一些设置,仅允许注册用户结账。
然后,您必须在注册过程中添加一些必填字段,如城镇、邮政编码和国家/地区。
完成后,根据注册的客户字段启用/禁用支付网关将变得很容易。
1) 自定义注册字段:
如何在“我的账户”页面的用户注册中添加自定义字段 https://claudiosmweb.com/2016/03/24/how-to-add-custom-fields-in-user-registration-on-the-my-account-page/
2) 对于基于此客户信息的支付网关/方法,您可以在woocommerce_available_payment_gateways
过滤钩:
add_filter( 'woocommerce_available_payment_gateways', 'custom_payment_gateways_process' );
function custom_payment_gateways_process( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
if ( is_admin() || !is_user_logged_in() )
return $available_gateways;
$current_user_id = get_current_user_id();
$user_meta = get_user_meta( $current_user_id );
// User City, Postcode, State and Country code
$user_city = $user_meta['billing_city'];
$user_postcode = $user_meta['shipping_postcode'];
$user_State = $user_meta['shipping_state'];
$user_country = $user_meta['shipping_country'];
// Disable Cash on delivery ('cod') method example for customer out of spain:
if ( isset( $available_gateways['cod'] ) && $user_country != 'ES' ) {
unset( $available_gateways['cod'] );
}
// You can set many conditions based on the user data
return $available_gateways;
}
此代码只是一个示例,您必须为目标支付方式/网关设置正确的条件……
代码位于活动子主题(或主题)的functions.php 文件中或任何插件文件中。