Advertisement
Guest User

Kia FIelds

a guest
Jul 25th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.48 KB | None | 0 0
  1. <?php
  2.  
  3. add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
  4. function theme_enqueue_styles() {
  5.     wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
  6.     wp_enqueue_style( 'child-style',
  7.         get_stylesheet_directory_uri() . '/style.css',
  8.         array('parent-style')
  9.     );
  10. }
  11.  
  12. /**
  13.  * so I can see what's happening without ajax
  14.  *
  15.  */
  16. function so_27023433_disable_checkout_script(){
  17.     wp_dequeue_script( 'wc-checkout' );
  18. }
  19. add_action( 'wp_enqueue_scripts', 'so_27023433_disable_checkout_script' );
  20.  
  21.  
  22. /**
  23.  * build our array of fields
  24.  *
  25.  */
  26. function supreme_filter_checkout_fields($fields){
  27.  
  28.     $op_cart_count = WC()->cart->get_cart_contents_count();
  29.    
  30.     $fields['extra_fields'] = array();
  31.    
  32.     for ( $i = 1; $i <= $op_cart_count; $i++) {
  33.        
  34.         $fields['extra_fields']['passenger_title_' . $i] = array( 'type' => 'select', 'options' => array( 'mr' => __( 'Mr' ), 'mrs' => __( 'Mrs' ), 'miss' => __( 'Miss' ) ), 'required' => false, 'label' => __( 'Title' ) );
  35.         $fields['extra_fields']['passenger_title_' . $i] = array( 'type' => 'select', 'options' => array( 'mr' => __( 'Mr' ), 'mrs' => __( 'Mrs' ), 'miss' => __( 'Miss' ) ), 'required' => false, 'label' => __( 'Title' ) );
  36.         $fields['extra_fields']['passenger_first_name_' . $i] = array( 'type' => 'text', 'required' => false, 'label' => __( 'First Name' ) );
  37.         $fields['extra_fields']['passenger_middle_name_' . $i] = array( 'type' => 'text', 'required' => false, 'label' => __( 'Middle Name' ) );
  38.         $fields['extra_fields']['passenger_last_name_' . $i] = array( 'type' => 'text', 'required'  => false, 'label' => __( 'Last Name' ) );
  39.         $fields['extra_fields']['passenger_date_of_birth_' . $i] = array( 'type' => 'text', 'class' => array('date-of-birth form-row-wide'), 'label' => __('Date of Birth'), 'placeholder' => __('Select Date'), 'options' =>   array('' => __('Date of Birth', 'woocommerce' )) );
  40.         $fields['extra_fields']['passenger_dietry_required_' . $i] = array( 'type' => 'select', 'class' => array('dietry-required form-row-wide'), 'options' => array( '' => __( 'Please Select' ), 'y' => __( 'Yes' ), 'n' => __( 'No' ) ), 'required' => false, 'label' => __( 'Dietry Requirements?' ) );
  41.         $fields['extra_fields']['passenger_dietry_preference_' . $i] = array( 'type' => 'select', 'class' => array('dietry-requirements form-row-wide'), 'options' => array( 'v' => __( 'Vegetarian' ), 'GF' => __( 'Gluten Free' ) ), 'required' => false, 'label' => __( 'Meal Preferences' ) );
  42.  
  43.     }
  44.    
  45.     return $fields;
  46.  
  47. }
  48. add_filter( 'woocommerce_checkout_fields', 'supreme_filter_checkout_fields', 10, 4 );
  49.  
  50.  
  51. /**
  52.  * generate the fields
  53.  *
  54.  */
  55. function supreme_extra_checkout_fields(){
  56.  
  57.     $checkout = WC()->checkout();
  58.  
  59.     foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) :
  60.  
  61.         woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
  62.  
  63.     endforeach;
  64.  
  65. }
  66. add_action( 'woocommerce_checkout_after_customer_details' ,'supreme_extra_checkout_fields' );
  67.  
  68.  
  69. /**
  70.  * add some js for our datepicker jqueryUI
  71.  *
  72.  */
  73. function supreme_add_js_functions(){
  74.  
  75.     ?>
  76.  
  77.         <script>  
  78.         jQuery(document).ready(function( $ ) {
  79.  
  80.             $('.date-of-birth .input-text').each(function(){
  81.                
  82.                 $(this).datepicker({
  83.                     changeMonth: true,
  84.                     changeYear: true,
  85.                     yearRange: "-100:+0",
  86.                 });
  87.            
  88.             });
  89.  
  90.         });
  91.         </script>
  92.  
  93.     <?php
  94.  
  95. }
  96. add_action('wp_head','supreme_add_js_functions');
  97.  
  98.  
  99. /**
  100.  * Process the checkout
  101.  * commentred out for testing
  102.  */
  103. //add_action('woocommerce_checkout_process', 'supreme_custom_checkout_field_process');
  104.  
  105. function supreme_custom_checkout_field_process( $fields ) {
  106.  
  107.     $checkout = WC()->checkout();
  108.     $op_cart_count = WC()->cart->get_cart_contents_count();
  109.  
  110.  
  111.     $i = 1;
  112.     if ( $i <= $op_cart_count ) {
  113.  
  114.         if ( ! $_POST['passenger_first_name_' . $i] )
  115.             wc_add_notice( __( 'Please enter your first name' ), 'error' );
  116.            
  117.         if ( ! $_POST['passenger_last_name_' . $i] )
  118.             wc_add_notice( __( 'Please enter your last name' ), 'error' );
  119.  
  120.     $i++;
  121.  
  122.     }
  123. }
  124.  
  125.  
  126. /**
  127.  * update_post_meta
  128.  *
  129.  */
  130. function supreme_custom_checkout_field_update_order_meta( $order_id, $posted ){
  131.  
  132.     $checkout = WC()->checkout();
  133.  
  134.     foreach ( $checkout->checkout_fields['extra_fields'] as $a => $v ) :
  135.  
  136.             if ( isset( $posted[$a] ) ) {
  137.                 update_post_meta( $order_id, $a, $posted[$a] );
  138.             }
  139.  
  140.     endforeach;
  141.  
  142. }
  143. add_action( 'woocommerce_checkout_update_order_meta', 'supreme_custom_checkout_field_update_order_meta', 10, 2 );
  144.  
  145.  
  146. /**  
  147. * Display field value on the order edition page  
  148. **/  
  149. add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );  
  150.  
  151. function my_custom_checkout_field_display_admin_order_meta($order){  
  152.  
  153.     global $woocommerce;
  154.  
  155.     $order_item = $order->get_items();
  156.  
  157.     foreach( $order_item as $product ) {
  158.        
  159.         $prodct_qty = $product['qty'];
  160.        
  161.         for ( $i = 1; $i <= $prodct_qty; $i++) {
  162.            
  163.             echo '<div class="address"><h3>Passenger #' . $i . '</h3>';
  164.             echo '<p><strong>'.__('Title').':</strong> ' . get_post_meta( $order->id, 'passenger_title_' . $i, true ) . '</p>';
  165.             echo '<p><strong>'.__('First Name').':</strong> ' . get_post_meta( $order->id, 'passenger_first_name_' . $i, true ) . '</p>';
  166.             echo '<p><strong>'.__('Middle Name').':</strong> ' . get_post_meta( $order->id, 'passenger_middle_name_' . $i, true ) . '</p>';
  167.             echo '<p><strong>'.__('Last Name').':</strong> ' . get_post_meta( $order->id, 'passenger_last_name_' . $i, true ) . '</p>';
  168.             echo '<p><strong>'.__('Date of Birth').':</strong> ' . get_post_meta( $order->id, 'passenger_date_of_birth_' . $i, true ) . '</p>';
  169.             echo '<p><strong>'.__('Dietry Requirements').':</strong> ' . get_post_meta( $order->id, 'passenger_dietry_required_' . $i, true ) . '</p>';
  170.             echo '<p><strong>'.__('Meal Preferences').':</strong> ' . get_post_meta( $order->id, 'passenger_dietry_preference_' . $i, true ) . '</p>';
  171.             echo '</div>';
  172.            
  173.             echo '<div class="edit_address">';
  174.             woocommerce_wp_text_input( array( 'id' => 'passenger_title_' . $i, 'label' => __( 'Title' ), 'wrapper_class' => '_billing_company_field' ) );
  175.             woocommerce_wp_text_input( array( 'id' => 'passenger_first_name_' . $i, 'label' => __( 'passenger_first_name_' . $i ), 'wrapper_class' => '_billing_company_field' ) );
  176.             woocommerce_wp_text_input( array( 'id' => 'passenger_middle_name_' . $i, 'label' => __( 'passenger_middle_name_' . $i ), 'wrapper_class' => '_billing_company_field' ) );
  177.             woocommerce_wp_text_input( array( 'id' => 'passenger_last_name_' . $i, 'label' => __( 'passenger_last_name_' . $i ), 'wrapper_class' => '_billing_company_field' ) );
  178.             woocommerce_wp_text_input( array( 'id' => 'passenger_date_of_birth_' . $i, 'label' => __( 'passenger_date_of_birth_' . $i ), 'wrapper_class' => '_billing_company_field' ) );
  179.             woocommerce_wp_text_input( array( 'id' => 'passenger_dietry_required_' . $i, 'label' => __( 'passenger_dietry_required_' . $i ), 'wrapper_class' => '_billing_company_field' ) );
  180.             woocommerce_wp_text_input( array( 'id' => 'passenger_dietry_preference_' . $i, 'label' => __( 'passenger_dietry_preference_' . $i ), 'wrapper_class' => '_billing_company_field' ) );
  181.             echo '</div>';
  182.            
  183.         }
  184.  
  185.     }
  186.  
  187. }
  188.  
  189.  
  190. /*
  191. function kia_save_extra_details( $post_id, $post ){
  192.    
  193.     global $woocommerce;
  194.  
  195.     $order_item = $order->get_items();
  196.  
  197.     foreach( $order_item as $product ) {
  198.        
  199.         $prodct_qty = $product['qty'];
  200.        
  201.         for ( $i = 1; $i <= $prodct_qty; $i++) {
  202.            
  203.             update_post_meta( $post_id, 'passenger_title_' . $i, wc_clean( $_POST[ 'passenger_title_' . $i ] ) );
  204.             update_post_meta( $post_id, 'passenger_first_name_' . $i, wc_clean( $_POST[ 'passenger_first_name_' . $i ] ) );
  205.             update_post_meta( $post_id, 'passenger_middle_name_' . $i, wc_clean( $_POST[ 'passenger_middle_name_' . $i ] ) );
  206.             update_post_meta( $post_id, 'passenger_last_name_' . $i, wc_clean( $_POST[ 'passenger_last_name_' . $i ] ) );
  207.             update_post_meta( $post_id, 'passenger_date_of_birth_' . $i, wc_clean( $_POST[ 'passenger_date_of_birth_' . $i ] ) );
  208.             update_post_meta( $post_id, 'passenger_dietry_required_' . $i, wc_clean( $_POST[ 'passenger_dietry_required_' . $i ] ) );
  209.             update_post_meta( $post_id, 'passenger_dietry_preference_' . $i, wc_clean( $_POST[ 'passenger_dietry_preference_' . $i ] ) );
  210.            
  211.         }
  212.  
  213.     }
  214.  
  215. }
  216. add_action( 'woocommerce_process_shop_order_meta', 'kia_save_extra_details', 45, 2 );
  217. */
  218.  
  219. function wp_child_enqueue_styles() {
  220.  
  221.     if ( is_checkout() ) {
  222.  
  223.         wp_enqueue_style( 'cck-jquery-css', get_stylesheet_directory_uri() . '/js/jquery-ui.min.css' );
  224.         wp_enqueue_script( 'cck-jqueryui', get_stylesheet_directory_uri() . '/js/jquery-ui.min.js', array(), '20160412', true );
  225.         wp_enqueue_script( 'cck-custom', get_stylesheet_directory_uri() . '/js/custom.js', array(), '20160412', true );
  226.        
  227.     }
  228.    
  229.    
  230. }
  231. add_action( 'wp_enqueue_scripts', 'wp_child_enqueue_styles' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement