如何获得所需的自定义WooCommerce结帐字段以停止结帐

时间:2016-08-13 01:27:28

标签: php wordpress woocommerce

在客户下订单之前,我的客户要求条款和条件协议部分有3个不同的部分。我写了以下代码:

// in wptheme/functions.php

add_action('woocommerce_after_checkout_billing_form', 'add_three_terms');

function add_three_terms ( $checkout ) {
  echo '<div id="terms_n_conditions"><h2>3. Agree to terms & conditions</h2>';
  echo "<h3>Member Agreement Part 1</h3>";
  echo "<p>...</p>";
  woocommerce_form_field ( 'agreement_one', array (
    "type"       => "checkbox",
    "label"      => __("I Agree"),
    "required" => true
  ), $checkout->get_value("agreement_one") );

  echo "<h3>Member Agreement Part 2</h3>";
  echo "<p>...</p>";
  woocommerce_form_field ( 'agreement_two', array (
    "type"       => "checkbox",
    "label"      => __("I Agree"),
    "required" => true
), $checkout->get_value("agreement_two") );

  echo "<h3>Member Agreement Part 3</h3>";
  echo "<p>...</p>";
  woocommerce_form_field ( 'agreement_three', array (
    "type"       => "checkbox",
    "label"      => __("I Agree"),
    "required" => true
), $checkout->get_value("agreement_three") );
  echo '</div>';
}

add_action( 'woocommerce_checkout_process', 'custom_checkout_function' );

function custom_checkout_function(){
  global $woocommerce;
  if( !isset($_POST['agreement_one']) ) {
    wc_add_notice( __( 'Please agree to the Member Agreement Part 1' ), 'error' );
  }
  if( !isset($_POST['agreement_two']) ) {
    wc_add_notice( __( 'Please agree to the Member Agreement Part 2' ), 'error' );
  }
  if( !isset($_POST['agreement_three']) ) {
    wc_add_notice( __( 'Please agree to the Member Agreement Part 3' ), 'error' );
  }
}

add_action( 'woocommerce_checkout_update_order_meta', 'add_agreements_to_order_meta' );

function add_agreements_to_order_meta ( $order_id ) {
  if ( ! empty ( $_POST["agreement_one"])) {
    update_post_meta( $order_id, "Agreement One", $_POST["agreement_one"] );
  }
  if ( ! empty ( $_POST["agreement_two"])) {
    update_post_meta( $order_id, "Agreement Two", $_POST["agreement_two"] );
  }
  if ( ! empty ( $_POST["agreement_three"])) {
    update_post_meta( $order_id, "Agreement Three", $_POST["agreement_three"] );
  }
}

如果还有其他字段也不正确,则会正确打印错误。只有自定义字段为空时才会出现问题。错误不会阻止订单完成。

我错过了什么吗?我的胡克斯是对的吗?

0 个答案:

没有答案