php 结帐:添加自定义字段部分#woo

Posted

tags:

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

//Create Custom Checkout Field Section w/ conditional options
//Part1: Create Field - Conditional Optional
\!h add_action('__woocommerce_after_checkout_shipping_form__', 'conditional_custom_checkout_field');  //change the hook to the area you want it to appear
 
  function conditional_custom_checkout_field( $checkout ) {
  
    // see if user has previously saved info for the field (optional)
      $current_user = wp_get_current_user();
  \!h    __$saved_custom_field__ = $current_user->__student_acu_no__; //change this to match user info
     
  // echo field only if check_product_category() is true
    if(check_product_category() ){   //remove this line if you don't want the conditional catagory function to be called
          //Create Field, Label, & Heading
        \!h echo '<div id="custom_checkout_field"><h2>' . __('__New Heading__') . '</h2>';
         //Alt: use if you dont want a heading
         //echo '<div id="__custom_checkout_field__">'; 
          \!h woocommerce_form_field('__custom_field_name__', array(       
              'type'          => 'text',        
              'class'         => array('my-field-class form-row-wide'), 
          \!h     'label'         => __('__Custom Field Label__'),        
          \!h     'placeholder'   => __('__Field Placeholder__'),        
        //    'required'   => true,     uncomment this line to require this field to be filled out
          \!h     'default'   => __$saved_custom_field__,        ), 
          \!h     $checkout->get_value( '__custom_field_name__' )); 
         echo '</div>';
    }
  }
  
  //Part2: Conditional Function to display field only if category is in the Cart
      // Is called by function conditional_custom_checkout_field()
      function check_product_category(){    
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {        
              $product_id   = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );          
        if( product_category_is( $product_id ) ){            
            return  true;               
            }    
          }   
        return false;
      }
       
      // Function that specifies category ID for check_product_category()
      function product_category_is( $product_id ){
    \!h     return has_term( __catagoyID__, 'product_cat', get_post( $product_id ) );  //indicate catagory ID number
      }

//Part3: Validate New Checkout Field
add_action('woocommerce_checkout_process', 'validate_new_checkout_field');
 
  // Show an error message if the field is not set.
  function validate_new_checkout_field() {    
\!h    if ( ! $_POST['__customised_field_name__'] )        
\!h     wc_add_notice( __( '__Error Message:Please enter...__' ), 'error' );
  }

/*
//Part4: Save New Checkout Field Into User Meta, if validation is successful (optional if applicable)
    add_action( 'woocommerce_checkout_update_user_meta', 'custom_checkout_field_update_user_meta' );
       
        function custom_checkout_field_update_user_meta( $user_id ) {   
      \!h    if ( $user_id && $_POST['__customised_field_name__'] ) 
      \!h      update_user_meta($user_id, '__customised_field_name__', sanitize_text_field($_POST['__customised_field_name__']) );
        } 
      
  //Part5: Display New Field In The Admin User Profile Page
  add_action( 'show_user_profile', 'show_user_extra_field' );
  add_action( 'edit_user_profile', 'show_user_extra_field' );    
   
      function show_user_extra_field( $user ){ ?>    
    \!h    <h3>__Additional Fields Header__</h3>    
        <table class="form-table">        
          <tr>            
      \!h    <th><label for="__customised_field_name__">__Label for field__</label></th>            
      \!h    <td><input type="text" name="__customised_field_name__" value="<?php echo esc_attr(get_the_author_meta( '__customised_field_name__', $user->ID )); ?>" class="regular-text" /></td>        
          </tr>    
        </table><?php
      } 
 
    //Save User Field When Updated from the Admin User Page
    add_action( 'personal_options_update', 'save_extra_fields' );    
    add_action( 'edit_user_profile_update', 'save_extra_fields' );    
     
      function save_extra_fields( $user_id ){        
 \!h       update_user_meta( $user_id,'__customised_field_name__', sanitize_text_field( $_POST['__customised_field_name__'] ) );    
      }   
    */
    
//Part6: Save & Show New Field In The WooCommerce Order Details
  // Update order meta with field value
  add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta' );
   
    function custom_checkout_field_update_order_meta( $order_id ) {   
  \!h    if ( $_POST['__customised_field_name__'] ){ 
   \!h     update_post_meta( $order_id, '___customised_field_name__', sanitize_text_field($_POST['__customised_field_name__']) );
      }
    }
   
  // Display User Field @ Order Meta
  add_action( 'woocommerce_admin_order_data_after_shipping_address', 'checkout_field_display_admin_order_meta', 10, 1 );
   
    function checkout_field_display_admin_order_meta( $order ) {    
    \!h  echo '<p><strong>'.__('__Field Label__').':</strong> ' . get_post_meta( $order->id, '___customised_field_name__', true ) . '</p>'; //possibly $order->get_id(),
    }  

//Part7: Add custom field value to WooCommerce emails
add_filter('woocommerce_email_order_meta_keys', 'custom_field_order_email');

function custom_field_order_email( $keys ) {
\!h       $keys[] = '__Customised Field Name__'; // This will look for a custom field called 'Customised Field Name' and add it to emails
     return $keys;
}

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

php 重新排序结帐字段#woo

php 结帐:删除字段#woo #hide

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

php 结帐:添加送货通知#woo

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

php 将自定义字段添加到购物车/结帐