php WooCommerce自定义结帐字段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php WooCommerce自定义结帐字段相关的知识,希望对你有一定的参考价值。

<?php

// EXAMPLE 1: Remove the "Additional Fields" section, including the heading

// Remove the "Additional Fields" section, including the heading
add_filter( 'woocommerce_enable_order_notes_field', '__return_false', 1);


// EXAMPLE 2: Change existing fields

// Change existing fields:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {

  // change placeholder
  $fields['order']['order_comments']['placeholder'] = 'My new placeholder';

  // change label
  $fields['order']['order_comments']['label'] = 'My new label';

  // remove a field
  unset($fields['order']['order_comments']);

  // make a required field optional
  $address_fields['address_1']['required'] = false;

  return $fields;

}


// EXAMPLE 3: Add new fields to the checkout

/**
 * Add the field to the checkout
 */
add_action( 'woocommerce_after_order_notes', 'asra2016_custom_checkout_field' );

function asra2016_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h3>' . __('Education Information') . '</h3>';

    woocommerce_form_field( 'court_reporting_school_attended', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Court Reporting School Attended:'),
        'placeholder'   => __('Name of school'),
        'required'      => true,
        ), $checkout->get_value( 'court_reporting_school_attended' ));


    woocommerce_form_field( 'graduation_year', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Graduation Year'),
        'placeholder'   => __(''),
        'required'      => false,
        ), $checkout->get_value( 'graduation_year' ));

    echo '</div>';

}

/**
 * Process the checkout (Validate the added field)
 */
add_action('woocommerce_checkout_process', 'asra2016_custom_checkout_field_process');

function asra2016_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['court_reporting_school_attended'] )
        wc_add_notice( __( 'Please enter which court reporting school you attended.' ), 'error' );
}


/**
 * Update the user meta with field value
 **/
add_action('woocommerce_checkout_update_user_meta', 'asra2016_custom_checkout_field_update_user_meta');

function asra2016_custom_checkout_field_update_user_meta( $user_id ) {
  if ($user_id && $_POST['court_reporting_school_attended']) update_user_meta( $user_id, 'user_court_reporting_school_attended', esc_attr($_POST['court_reporting_school_attended']) );
  if ($user_id && $_POST['graduation_year']) update_user_meta( $user_id, 'user_graduation_year', esc_attr($_POST['graduation_year']) );
}

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'asra2016_custom_checkout_field_update_order_meta');

function asra2016_custom_checkout_field_update_order_meta( $order_id ) {
  if ($_POST['court_reporting_school_attended']) update_post_meta( $order_id, 'Court Reporting School Attended', esc_attr($_POST['court_reporting_school_attended']));
  if ($_POST['graduation_year']) update_post_meta( $order_id, 'Graduation Year', esc_attr($_POST['graduation_year']));
}



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

function asra2016_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Court Reporting School Attended').':</strong> ' . get_post_meta( $order->id, 'Court Reporting School Attended', true ) . '</p>';
    echo '<p><strong>'.__('Graduation Year').':</strong> ' . get_post_meta( $order->id, 'Graduation Year', true ) . '</p>';
}

/**
 * Display field value in emails
 */

/* To use: 
1. Add this snippet to your theme's functions.php file
2. Change the meta key names in the snippet
3. Create a custom field in the order post - e.g. key = "Tracking Code" value = abcdefg
4. When next updating the status, or during any other event which emails the user, they will see this field in their email
*/
add_filter('woocommerce_email_order_meta_keys', 'asra2016_custom_order_meta_keys');

function asra2016_custom_order_meta_keys( $keys ) {
     // $keys[] = 'Court Reporting School Attended'; // This will look for a custom field called 'Tracking Code' and add it to emails
     // $keys[] = 'Graduation Year'; // This will look for a custom field called 'Tracking Code' and add it to emails
     return $keys;
}




// Billing fields:
// -
//    billing_first_name
//    billing_last_name
//    billing_company
//    billing_address_1
//    billing_address_2
//    billing_city
//    billing_postcode
//    billing_country
//    billing_state
//    billing_email
//    billing_phone

// Shipping fields:
// -
//    shipping_first_name
//    shipping_last_name
//    shipping_company
//    shipping_address_1
//    shipping_address_2
//    shipping_city
//    shipping_postcode
//    shipping_country
//    shipping_state
// 

// Account fields:
// -
//    account_username
//    account_password
//    account_password-2

// Order fields:
// -
//    order_comments


// Field properties:
// -
//    type – type of field (text, textarea, password, select)
//    label – label for the input field
//    placeholder – placeholder for the input
//    class – class for the input
//    required – true or false, whether or not the field is require
//    clear – true or false, applies a clear fix to the field/label
//    label_class – class for the label element
//    options – for select boxes, array of options (key => value pairs)


SOURCES: 
- https://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
- https://gist.github.com/ken-muturi/8038a8590cee90a43e7d

以上是关于php WooCommerce自定义结帐字段的主要内容,如果未能解决你的问题,请参考以下文章

php [使用操作和过滤器自定义结帐字段]向电子邮件添加自定义WooCommerce结帐字段

php [使用操作和过滤器自定义结帐字段] WooCommerce自定义结帐字段不需要电话号码示例。

php [使用操作和过滤器自定义结帐字段]显示WooCommerce自定义结帐字段管理员订单页面。

php WooCommerce自定义结帐字段

php [使用操作和过滤器自定义结帐字段]向WooCommerce添加自定义cehckout字段。

php [使用操作和过滤器自定义结帐字段]向WooCommerce添加新的送货字段