在 Woocommerce 中随处保存和显示特定的支付网关附加字段

Save and display specific payment gateway additional field everywhere in Woocommerce(在 Woocommerce 中随处保存和显示特定的支付网关附加字段)
本文介绍了在 Woocommerce 中随处保存和显示特定的支付网关附加字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 "

<小时>

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

<小时>

在管理订单页面:

I am using "Additional field on checkout for specific payment gateway in Woocommerce" answer code, that displays an additional dropdown field for specific payment gateway in checkout page.

How to save and display the options on the orders and on email notifications?

解决方案

Continuation of "Additional field on checkout for specific payment gateway in Woocommerce"

Here is the complete way to:

  1. Add a dropdown with options to BACS payment
  2. Field validation (required option)
  3. Save chosen option as order custom meta data
  4. Display the chosen option on order totals everywhere (orders and emails notifications)
  5. Display the chosen option on admin order edit page below billing details.

The code:

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

Code goes in functions.php file of your active child theme (or active theme). tested and works.

On checkout page:


On Order received page (on Order view and email notifications):


On Admin Order pages:

这篇关于在 Woocommerce 中随处保存和显示特定的支付网关附加字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)