【问题标题】:Save multiple WooCommerce custom checkout fields as order meta data将多个 WooCommerce 自定义结帐字段保存为订单元数据
【发布时间】:2021-05-12 04:31:15
【问题描述】:

我正在寻找一些 php 代码,可以让我从结帐字段中提取信息(姓名、地址等)并将其添加到订单元数据中。

希望这个尽可能简单

我之前发现此代码允许将自定义框添加到结帐页面,并且我有点理解它的工作原理,但是我想在他们将其输入计费名字框时捕获他们的名字。我似乎可以掌握如何捕获这些数据并将其放入订单元数据中,我尝试缩短代码并对其进行几次编辑,但我似乎没有获胜

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
     $fields['shipping']['shipping_phone'] = array(
        'label'     => __('Phone', 'woocommerce'),
    'placeholder'   => _x('Phone', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     return $fields;
}

/**
 * Display field value on the order edit page
 */
 
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Phone From Checkout Form').':</strong> ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '</p>';
}
/**
 * Add the field to the checkout
 */
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';

    woocommerce_form_field( 'my_field_name', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Fill in this field'),
        'placeholder'   => __('Enter something'),
        ), $checkout->get_value( 'my_field_name' ));

    echo '</div>';

}
/**
 * Process the checkout
 */
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['my_field_name'] )
        wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
}
/**
 * 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['my_field_name'] ) ) {
        update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
    }
}

我喜欢这个,它确实有效,但不是以我需要的方式工作。感谢您的所有帮助

【问题讨论】:

  • /** * 使用字段值更新订单元数据 */ add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );功能 my_custom_checkout_field_update_order_meta( $order_id ) { if ( !empty( $field[billing_first_name] ) ) { update_post_meta( $order_id, 'start_date_', sanitize_text_field( $field[billing_first_name] ) );这是我创建的代码,我希望将帐单名称复制到订单元数据中
  • 嗨,为了澄清,我尝试创建一些代码,将计费名字的值添加到订单元数据中,但是我的代码似乎不起作用,我不明白为什么

标签: php wordpress woocommerce metadata orders


【解决方案1】:

您的代码中有一些错误和遗漏的东西……请尝试以下替换代码:

// Add shipping phone (in checkout and My account edit shipping address) and save field value
add_action( 'woocommerce_shipping_fields', 'add_shipping_phone_field' );
function add_shipping_phone_field( $fields ) {
     $fields['shipping_phone'] = array(
        'label'     => __('Phone', 'woocommerce'),
        'placeholder'   => _x('Phone', 'placeholder', 'woocommerce'),
        'required'  => false,
        'class'     => array('form-row-wide'),
        'clear'     => true
     );

     return $fields;
}

// Display shipping phone value on the order edit pages under shipping section
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'display_shipping_phone_in_admin_orders' );
function display_shipping_phone_in_admin_orders( $order ){
    $phone_value = $order->get_meta('_shipping_phone');

    if ( ! empty($phone_value) ) {
        echo '<p><strong>'.__('Shipping phone').':</strong> ' . $phone_value . '</p>';
    }
}

// Add a custom checkout field
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';

    woocommerce_form_field( 'my_field_slug', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('My custom field'),
        'placeholder'   => __('Enter something… '),
        'required'      => true,
    ), $checkout->get_value( 'my_field_slug' ) );

    echo '</div>';
}
// Validate required checkout fields
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( isset($_POST['my_field_slug']) && empty($_POST['my_field_slug']) ) {
        wc_add_notice( __( '"My custom field" is a required field.' ), 'error' );
    }
}

// Add custom checkout field value as custom order meta data
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order ) {
    if ( isset($_POST['my_field_slug']) && ! empty($_POST['my_field_slug']) ) {
        $order->update_meta_data( 'My Field', sanitize_text_field( $_POST['my_field_slug'] ) );
    }
}

// Display "My field" value on the order edit pages under billing section
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_my_custom_checkout_field_in_admin_orders', 10, 1 );
function display_my_custom_checkout_field_in_admin_orders($order){
    $my_field_value = $order->get_meta('My Field');

    if ( ! empty($my_field_value) ) {
        echo '<p><strong>'.__('My field').':</strong> ' . $my_field_value . '</p>';
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


现在,如果您需要从 Woocommerce 现有结帐字段中提取一些数据并以自定义方式将其组合以将其保存为自定义订单元数据,请尝试更加明确:

  • 哪些字段?
  • 你想如何组合它?
  • 用于保存该数据组合的自定义字段 slug 是什么?

【讨论】:

  • 您好,非常感谢您的帮助,代码很棒。我尝试使用此代码及其想法来捕获数据,基本上,我使用 woocommerce Booking 插件并尝试获取开始日期结束日期开始时间结束时间由客户选择传递给订单元数据,以便它可以用于导出和排序订单我尝试了一些不同的代码变体我似乎无法做到这一点
  • @AlasdairMacEwan 另请注意,相关的最小预订信息作为自定义订单 item 元数据(而不是订单元数据)存储在订单中,并链接到预订条目......所以最好以清晰的方式提出一个包含所有相关细节的新问题。你可以在这里通知我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
相关资源
最近更新 更多