繁体   English   中英

将多个 WooCommerce 自定义结帐字段保存为订单元数据

[英]Save multiple WooCommerce custom checkout fields as order meta data

我希望找到一些 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 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 是什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM