Woocommerce货到付款添加自定义字段

时间:2016-09-29 14:50:49

标签: woocommerce woothemes hook-woocommerce woocommerce-rest-api

如何在发货时在cah下方添加自定义textarea字段,这样当您点击货到付款单选按钮时,textarea会显示输入保存到订单中的详细信息?

1 个答案:

答案 0 :(得分:1)

  1. 将checkout / payment-method.php文件复制到您的主题目录woocommerce文件夹中。即your-theme / woocommerce / checkout / payment-method.php

  2. 使用以下代码更改内容li标记:

  3. <li class="wc_payment_method payment_method_<?php echo $gateway->id; ?>">
    <input id="payment_method_<?php echo $gateway->id; ?>" type="radio" class="input-radio" name="payment_method" value="<?php echo esc_attr( $gateway->id ); ?>" <?php checked( $gateway->chosen, true ); ?> data-order_button_text="<?php echo esc_attr( $gateway->order_button_text ); ?>" />
    <label for="payment_method_<?php echo $gateway->id; ?>">
    <?php echo $gateway->get_title(); ?> <?php echo $gateway->get_icon(); ?>
    </label>
    <?php if ( ( $gateway->has_fields() || $gateway->get_description() ) &&  $gateway->id != "cod" ) : ?>
    <div class="payment_box payment_method_<?php echo $gateway->id; ?>" <?php if ( ! $gateway->chosen ) : ?>style="display:none;"<?php endif; ?>>
    <?php $gateway->payment_fields(); ?>
    </div>
    <?php endif; ?>
    <?php if ( $gateway->id == "cod" ) : ?>
    <div class="payment_box payment_method_<?php echo $gateway->id; ?>" <?php if ( ! $gateway->chosen ) : ?>style="display:none;"<?php endif; ?>>
    <?php $gateway->payment_fields(); ?>
    <label>Custom Field</label>
    <input type="text" name='cod_custom_field'>
    </div>
    <?php endif; ?>
    </li>
    

    <li class="wc_payment_method payment_method_<?php echo $gateway->id; ?>"> <input id="payment_method_<?php echo $gateway->id; ?>" type="radio" class="input-radio" name="payment_method" value="<?php echo esc_attr( $gateway->id ); ?>" <?php checked( $gateway->chosen, true ); ?> data-order_button_text="<?php echo esc_attr( $gateway->order_button_text ); ?>" /> <label for="payment_method_<?php echo $gateway->id; ?>"> <?php echo $gateway->get_title(); ?> <?php echo $gateway->get_icon(); ?> </label> <?php if ( ( $gateway->has_fields() || $gateway->get_description() ) && $gateway->id != "cod" ) : ?> <div class="payment_box payment_method_<?php echo $gateway->id; ?>" <?php if ( ! $gateway->chosen ) : ?>style="display:none;"<?php endif; ?>> <?php $gateway->payment_fields(); ?> </div> <?php endif; ?> <?php if ( $gateway->id == "cod" ) : ?> <div class="payment_box payment_method_<?php echo $gateway->id; ?>" <?php if ( ! $gateway->chosen ) : ?>style="display:none;"<?php endif; ?>> <?php $gateway->payment_fields(); ?> <label>Custom Field</label> <input type="text" name='cod_custom_field'> </div> <?php endif; ?> </li>

    1. 在yout themes functions.php文件
    2. 中添加以下代码

      /**
       * Update the order meta with field value
       */
      add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
      
      function my_custom_checkout_field_update_order_meta( $order_id ) {
          if ( ! empty( $_POST['cod_custom_field'] ) ) {
              update_post_meta( $order_id, 'COD Custom Field', sanitize_text_field( $_POST['cod_custom_field'] ) );
          }
      }
      
      /**
       * Display field value on the order edit page
       */
      add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
      
      function my_custom_checkout_field_display_admin_order_meta($order){
          echo '<p><strong>'.__('COD Custom Field').':</strong> ' . get_post_meta( $order->id, 'COD Custom Field', true ) . '</p>';
      }
      

      那就是它。希望这有帮助。