禁止在WooCommerce中使用无运输方式访问结帐 [英] Prevent access to checkout without shipping method in WooCommerce

查看:61
本文介绍了禁止在WooCommerce中使用无运输方式访问结帐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除继续进行结帐按钮,并限制对结帐页面的访问,直到客户在购物篮页面上填写计算运费"选项为止.

I am trying to remove the proceed to checkout button and restrict access to the checkout page until a customer fills out the 'Calculate shipping' option on the basket page.

我创建了一种本地送货方式,该方式仅限于多个邮编.然后,我将其添加到我的functions.php文件中.

I created a local shipping method that's restricted to multiple post/zipcodes. I then went and added this to my functions.php file.

function disable_checkout_button_no_shipping() {
  $package_counts = array();

  // get shipping packages and their rate counts
  $packages = WC()->shipping->get_packages();
  foreach( $packages as $key => $pkg )
      $package_counts[ $key ] = count( $pkg[ 'rates' ] );

  // remove button if any packages are missing shipping options
  if( in_array( 0, $package_counts ) )
      remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button_no_shipping', 1 );

但是,当我在不同的浏览器中以隐身模式测试网站时,会出现继续付款"按钮.

However when I test the site in different browsers and in incognito modes the 'Proceed to checkout' button is there.

如果我单击计算运费"链接,但未填写表格但进行了更新,则该按钮消失了.我基本上希望该按钮在客户填写购物车页面上的计算运费"表格时出现(并在我的运送方式中包含邮政编码之一),然后才能继续到结帐页面.

If I click on the 'Calculate shipping' link and not fill out the form but update it the button disappears. I basically want the button to appear when a customer fills out the 'Calculate shipping' form on the cart page (and have one of the postcodes in my shipping method) before being able to proceed to the checkout page.

推荐答案

您最好尝试使用WooCommerce会话中的选择的送货方式",例如:

You should better try with the "Chosen shipping method" from WooCommerce session like:

function disable_checkout_button_no_shipping() {

    $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );

    // remove button if there is no chosen shipping method
    if( empty( $chosen_shipping_methods ) ) {
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
    }
}
add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button_no_shipping', 1 );

或者使用 woocommerce_check_cart_items 操作钩以另一种方式:

Or in a different way using woocommerce_check_cart_items action hook:

add_action( 'woocommerce_check_cart_items', 'required_chosen_shipping_methods' );
function required_chosen_shipping_methods() {
    $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );

    if( is_array( $chosen_shipping_methods ) && count( $chosen_shipping_methods ) > 0 ) {
        // Display an error message
        wc_add_notice( __("A shipping method is required in order to proceed to checkout."), 'error' );
    }
}

应该更好地工作.

这篇关于禁止在WooCommerce中使用无运输方式访问结帐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆