我正在寻找解决方案,在那里我可以找到用户的国家在woocommerce结帐页面。我是通过地理定位来做的。但是,用户可以更改这一点。我想限制它。
我正在使用一个付费插件给来自特定国家的用户优惠券。国家装货完美无缺,优惠券以国家为基础被应用或拒收。但是,用户可以在结帐页面手动更改国家并获得折扣。我的服务是在线的,因此没有实体货物的交付,所以进入错误的国家不会对用户造成任何影响。
我尝试了2-3个不同的优惠券限制插件,但都有相同的问题。有没有人遇到过类似的问题?
发布于 2021-01-18 07:07:07
更新的
可以禁用“签出国家”下拉字段(只读),如下所示:
add_filter( 'woocommerce_checkout_fields', 'checkout_country_fields_disabled' );
function checkout_country_fields_disabled( $fields ) {
$fields['billing']['billing_country']['custom_attributes']['disabled'] = 'disabled';
$fields['billing']['shipping_country']['custom_attributes']['disabled'] = 'disabled';
return $fields;
}由于禁用的select字段不会被发布,您还需要以下重复的隐藏字段,并在其中设置正确的值。它将避免错误通知,通知国家所需字段值为空。因此,也要添加以下内容:
// Mandatory for disable fields (hidden billing and shipping country fields with correct values)
add_filter( 'woocommerce_after_checkout_billing_form', 'checkout_country_hidden_fields_replacement' );
function checkout_country_hidden_fields_replacement( $fields ) {
$billing_country = WC()->customer->get_billing_country();
$shipping_country = WC()->customer->get_shipping_country();
?>
<input type="hidden" name="billing_country" value="<?php echo $billing_country; ?>">
<input type="hidden" name="shipping_country" value="<?php echo $shipping_country; ?>">
<?php
}代码位于活动子主题(或活动主题)的functions.php文件中。测试和工作。
备注:
如果没有完成,您需要在购物车页面中禁用运送计算器。
如果希望仅在Checkout 和make Address you 中读取国家下拉列表,请使用以下内容:
add_filter( 'woocommerce_billing_fields', 'checkout_billing_country_field_disabled' );
function checkout_billing_country_field_disabled( $fields ) {
$fields['billing_country']['custom_attributes']['disabled'] = 'disabled';
return $fields;
}
add_filter( 'woocommerce_shipping_fields', 'checkout_shipping_country_field_disabled' );
function checkout_shipping_country_field_disabled( $fields ) {
$fields['shipping_country']['custom_attributes']['disabled'] = 'disabled';
return $fields;
}
// Mandatory for disable fields (hidden billing and shipping country fields with correct values)
add_filter( 'woocommerce_after_checkout_billing_form', 'checkout_country_hidden_fields_replacement' );
function checkout_country_hidden_fields_replacement( $fields ) {
$billing_country = WC()->customer->get_billing_country();
$shipping_country = WC()->customer->get_shipping_country();
?>
<input type="hidden" name="billing_country" value="<?php echo $billing_country; ?>">
<input type="hidden" name="shipping_country" value="<?php echo $shipping_country; ?>">
<?php
}https://stackoverflow.com/questions/65769425
复制相似问题