Php 在输入邮政编码之前,禁止结帐
在输入邮政编码之前,试图阻止客户点击“继续付款”按钮。然而,我发现,通过下面的功能,如果你改变数量和更新购物车-该功能不再工作,你可以继续支付免费送货。有什么想法吗Php 在输入邮政编码之前,禁止结帐,php,wordpress,woocommerce,checkout,postal-code,Php,Wordpress,Woocommerce,Checkout,Postal Code,在输入邮政编码之前,试图阻止客户点击“继续付款”按钮。然而,我发现,通过下面的功能,如果你改变数量和更新购物车-该功能不再工作,你可以继续支付免费送货。有什么想法吗 add_action( 'wp_head', 'prevent_proceed_to_checkout' ); function prevent_proceed_to_checkout() { echo 'alert(Please enter postcode before payment!")'; } 更新-3种方式-
add_action( 'wp_head', 'prevent_proceed_to_checkout' );
function prevent_proceed_to_checkout() {
echo 'alert(Please enter postcode before payment!")';
}
更新-3种方式-(添加了备选方案)
1) 如果未填写邮政编码,您可以使用以下代码“避免继续结帐”结帐:
// Avoiding checkout when postcode has not been entered
add_action( 'woocommerce_check_cart_items', 'check_shipping_postcode' ); // Cart and Checkout
function check_shipping_postcode() {
$customer = WC()->session->get('customer');
if( ! $customer['calculated_shipping'] || empty( $customer['shipping_postcode'] ) ){
// Display an error message
wc_add_notice( __("Please enter your postcode before checkout", "woocommerce"), 'error' );
}
}
代码进入活动子主题(或活动主题)的function.php文件。测试和工作
购物车页面:
在结帐页面:
2) 尝试此替代方法(检查邮政编码并重定向到购物车以避免结帐): 代码进入活动子主题(或活动主题)的function.php文件。测试和工作 购物车页面:
3) 以上两者的组合(避免签出页面): 代码进入活动子主题(或活动主题)的function.php文件。测试和工作 购物车页面:
谢谢您的回复,但很遗憾,我没有工作,可能是因为我有第三方支付网关?真正发生的事情是,当他进来的时候,通知马上就出现了cart@ViktorFonster奇怪…我添加了第二个选项…请尝试。它应该工作,即使你有一个自定义的支付网关。酷!它非常有效!即使您更改了数量等,唯一的问题是cart simple会重新加载,并且不会显示错误通知。也许一个懒惰的解决方案是,如果我将用户重定向到一个带有通知和“返回购物车”按钮的页面(这不是很好),或者只是有一个大的通知,上面总是显示需要邮政编码?没什么区别,不幸的是:(@ViktorFonster我有第三个选择,应该适合你的需要。
add_action('template_redirect', 'check_shipping_postcode');
function check_shipping_postcode() {
// Only on checkout page (and cart for the displayed message)
if ( ( is_checkout() && ! is_wc_endpoint_url() ) || is_cart() ) {
$customer = WC()->session->get('customer');
if( ! $customer['calculated_shipping'] || empty( $customer['shipping_postcode'] ) ){
wc_add_notice( __("Please enter your postcode before checkout", "woocommerce"), 'error' );
if( ! is_cart() ){
wp_redirect(wc_get_cart_url());
exit();
}
}
}
}
// Avoiding checkout when postcode has not been entered
add_action( 'woocommerce_check_cart_items', 'check_shipping_postcode' ); // Cart and Checkout
function check_shipping_postcode() {
$customer = WC()->session->get('customer');
if( ! $customer['calculated_shipping'] || empty( $customer['shipping_postcode'] ) ){
// Display an error message
wc_add_notice( __("Please enter your postcode before checkout", "woocommerce"), 'error' );
}
}
add_action('template_redirect', 'shipping_postcode_redirection');
function shipping_postcode_redirection() {
// Only on checkout page
if ( is_checkout() && ! is_wc_endpoint_url() ) {
$customer = WC()->session->get('customer');
if( ! $customer['calculated_shipping'] || empty( $customer['shipping_postcode'] ) ){
wp_redirect(wc_get_cart_url());
exit();
}
}
}