保存并显示Woocommerce中各处的特定支付网关附加字段

时间:2019-05-22 09:13:39

标签: php wordpress woocommerce checkout payment-method

我使用的是Additional field on checkout for specific payment gateway in Woocommerce答案代码,该代码在结帐页面中为特定的支付网关显示了另一个下拉字段。

如何保存和显示订单和电子邮件通知上的选项?

2 个答案:

答案 0 :(得分:0)

add_action('woocommerce_checkout_update_order_meta','custom_checkout_field_update_order_meta');

函数custom_checkout_field_update_order_meta($ order_id)

{

if(!empty($ _ POST ['field_slug'])){

update_post_meta($ order_id,'Some key',sanitize_text_field($ _ POST ['field_slug']));

}

} 要在订单详细信息或电子邮件模板页面中获取此值,可以使用get_post_meta($ order_id,'Some key',false);

答案 1 :(得分:0)

  

Additional field on checkout for specific payment gateway in Woocommerce

的延续

以下是完整的方法:

  1. 为BACS付款添加带有选项的下拉列表
  2. 字段验证(必需选项)
  3. 将选择的选项另存为订单自定义元数据
  4. 在各地的订单总数(订单和电子邮件通知)上显示所选的选项
  5. 在帐单明细下方的管理订单编辑页面上显示所选的选项。

代码:

// BACS payement gateway description: Append custom select field
add_filter( 'woocommerce_gateway_description', 'gateway_bacs_custom_fields', 20, 2 );
function gateway_bacs_custom_fields( $description, $payment_id ){
    //
    if( 'bacs' === $payment_id ){
        ob_start(); // Start buffering

        echo '<div  class="bacs-options" style="padding:10px 0;">';

        woocommerce_form_field( 'bacs_option', array(
            'type'          => 'select',
            'label'         => __("Fill in this field", "woocommerce"),
            'class'         => array('form-row-wide'),
            'required'      => true,
            'options'       => array(
                ''          => __("Select something", "woocommerce"),
                'Option 1'  => __("Choice one", "woocommerce"),
                'Option 2'  => __("Choice two", "woocommerce"),
            ),
        ), '');

        echo '<div>';

        $description .= ob_get_clean(); // Append buffered content
    }
    return $description;
}

// Checkout custom field validation
add_action('woocommerce_checkout_process', 'bacs_option_validation' );
function bacs_option_validation() {
    if ( isset($_POST['payment_method']) && $_POST['payment_method'] === 'bacs'
    && isset($_POST['bacs_option']) && empty($_POST['bacs_option']) ) {
        wc_add_notice( __( 'Please Select an option for "Direct Bank Transfer" payment, please.' ), 'error' );
    }
}

// Checkout custom field save to order meta
add_action('woocommerce_checkout_create_order', 'save_bacs_option_order_meta', 10, 2 );
function save_bacs_option_order_meta( $order, $data ) {
    if ( isset($_POST['bacs_option']) && ! empty($_POST['bacs_option']) ) {
        $order->update_meta_data( '_bacs_option' , esc_attr($_POST['bacs_option']) );
    }
}

// Display custom field on order totals lines everywhere
add_action('woocommerce_get_order_item_totals', 'display_bacs_option_on_order_totals', 10, 3 );
function display_bacs_option_on_order_totals( $total_rows, $order, $tax_display ) {
    if ( $order->get_payment_method() === 'bacs' && $bacs_option = $order->get_meta('_bacs_option') ) {
        $sorted_total_rows = [];

        foreach ( $total_rows as $key_row => $total_row ) {
            $sorted_total_rows[$key_row] = $total_row;
            if( $key_row === 'payment_method' ) {
                $sorted_total_rows['bacs_option'] = [
                    'label' => __( "Bank wire option", "woocommerce"),
                    'value' => esc_html( $bacs_option ),
                ];
            }
        }
        $total_rows = $sorted_total_rows;
    }
    return $total_rows;
}

// Display custom field in Admin orders, below billing address block
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_bacs_option_near_admin_order_billing_address', 10, 1 );
function display_bacs_option_near_admin_order_billing_address( $order ){
    if( $bacs_option = $order->get_meta('_bacs_option') ) {
        echo '<div class="bacs-option">
        <p><strong>'.__('BACS option').':</strong> ' . $bacs_option . '</p>
        </div>';
    }
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试并可以正常工作。

在结帐页面上

enter image description here


在“收到订单”页面上(在“订单”视图和电子邮件通知上):

enter image description here


在管理订单页面上

enter image description here