6

如何在交货时在 cah 正下方添加自定义 textarea 字段,以便当您单击货到付款单选按钮时出现 textarea 以输入保存到订单中的详细信息?

4

1 回答 1

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

  2. 使用以下代码更改内容 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. 在你的主题functions.php文件中添加以下代码

/**
 * 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>';
}

而已。希望这有帮助。

于 2016-10-05T16:04:40.727 回答