Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
- function theme_enqueue_styles() {
- wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
- wp_enqueue_style( 'child-style',
- get_stylesheet_directory_uri() . '/style.css',
- array('parent-style')
- );
- }
- /**
- * so I can see what's happening without ajax
- *
- */
- function so_27023433_disable_checkout_script(){
- wp_dequeue_script( 'wc-checkout' );
- }
- add_action( 'wp_enqueue_scripts', 'so_27023433_disable_checkout_script' );
- /**
- * build our array of fields
- *
- */
- function supreme_filter_checkout_fields($fields){
- $op_cart_count = WC()->cart->get_cart_contents_count();
- $fields['extra_fields'] = array();
- for ( $i = 1; $i <= $op_cart_count; $i++) {
- $fields['extra_fields']['passenger_title_' . $i] = array( 'type' => 'select', 'options' => array( 'mr' => __( 'Mr' ), 'mrs' => __( 'Mrs' ), 'miss' => __( 'Miss' ) ), 'required' => false, 'label' => __( 'Title' ) );
- $fields['extra_fields']['passenger_title_' . $i] = array( 'type' => 'select', 'options' => array( 'mr' => __( 'Mr' ), 'mrs' => __( 'Mrs' ), 'miss' => __( 'Miss' ) ), 'required' => false, 'label' => __( 'Title' ) );
- $fields['extra_fields']['passenger_first_name_' . $i] = array( 'type' => 'text', 'required' => false, 'label' => __( 'First Name' ) );
- $fields['extra_fields']['passenger_middle_name_' . $i] = array( 'type' => 'text', 'required' => false, 'label' => __( 'Middle Name' ) );
- $fields['extra_fields']['passenger_last_name_' . $i] = array( 'type' => 'text', 'required' => false, 'label' => __( 'Last Name' ) );
- $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' )) );
- $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?' ) );
- $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' ) );
- }
- return $fields;
- }
- add_filter( 'woocommerce_checkout_fields', 'supreme_filter_checkout_fields', 10, 4 );
- /**
- * generate the fields
- *
- */
- function supreme_extra_checkout_fields(){
- $checkout = WC()->checkout();
- foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) :
- woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
- endforeach;
- }
- add_action( 'woocommerce_checkout_after_customer_details' ,'supreme_extra_checkout_fields' );
- /**
- * add some js for our datepicker jqueryUI
- *
- */
- function supreme_add_js_functions(){
- ?>
- <script>
- jQuery(document).ready(function( $ ) {
- $('.date-of-birth .input-text').each(function(){
- $(this).datepicker({
- changeMonth: true,
- changeYear: true,
- yearRange: "-100:+0",
- });
- });
- });
- </script>
- <?php
- }
- add_action('wp_head','supreme_add_js_functions');
- /**
- * Process the checkout
- * commentred out for testing
- */
- //add_action('woocommerce_checkout_process', 'supreme_custom_checkout_field_process');
- function supreme_custom_checkout_field_process( $fields ) {
- $checkout = WC()->checkout();
- $op_cart_count = WC()->cart->get_cart_contents_count();
- $i = 1;
- if ( $i <= $op_cart_count ) {
- if ( ! $_POST['passenger_first_name_' . $i] )
- wc_add_notice( __( 'Please enter your first name' ), 'error' );
- if ( ! $_POST['passenger_last_name_' . $i] )
- wc_add_notice( __( 'Please enter your last name' ), 'error' );
- $i++;
- }
- }
- /**
- * update_post_meta
- *
- */
- function supreme_custom_checkout_field_update_order_meta( $order_id, $posted ){
- $checkout = WC()->checkout();
- foreach ( $checkout->checkout_fields['extra_fields'] as $a => $v ) :
- if ( isset( $posted[$a] ) ) {
- update_post_meta( $order_id, $a, $posted[$a] );
- }
- endforeach;
- }
- add_action( 'woocommerce_checkout_update_order_meta', 'supreme_custom_checkout_field_update_order_meta', 10, 2 );
- /**
- * Display field value on the order edition page
- **/
- add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
- function my_custom_checkout_field_display_admin_order_meta($order){
- global $woocommerce;
- $order_item = $order->get_items();
- foreach( $order_item as $product ) {
- $prodct_qty = $product['qty'];
- for ( $i = 1; $i <= $prodct_qty; $i++) {
- echo '<div class="address"><h3>Passenger #' . $i . '</h3>';
- echo '<p><strong>'.__('Title').':</strong> ' . get_post_meta( $order->id, 'passenger_title_' . $i, true ) . '</p>';
- echo '<p><strong>'.__('First Name').':</strong> ' . get_post_meta( $order->id, 'passenger_first_name_' . $i, true ) . '</p>';
- echo '<p><strong>'.__('Middle Name').':</strong> ' . get_post_meta( $order->id, 'passenger_middle_name_' . $i, true ) . '</p>';
- echo '<p><strong>'.__('Last Name').':</strong> ' . get_post_meta( $order->id, 'passenger_last_name_' . $i, true ) . '</p>';
- echo '<p><strong>'.__('Date of Birth').':</strong> ' . get_post_meta( $order->id, 'passenger_date_of_birth_' . $i, true ) . '</p>';
- echo '<p><strong>'.__('Dietry Requirements').':</strong> ' . get_post_meta( $order->id, 'passenger_dietry_required_' . $i, true ) . '</p>';
- echo '<p><strong>'.__('Meal Preferences').':</strong> ' . get_post_meta( $order->id, 'passenger_dietry_preference_' . $i, true ) . '</p>';
- echo '</div>';
- echo '<div class="edit_address">';
- woocommerce_wp_text_input( array( 'id' => 'passenger_title_' . $i, 'label' => __( 'Title' ), 'wrapper_class' => '_billing_company_field' ) );
- woocommerce_wp_text_input( array( 'id' => 'passenger_first_name_' . $i, 'label' => __( 'passenger_first_name_' . $i ), 'wrapper_class' => '_billing_company_field' ) );
- woocommerce_wp_text_input( array( 'id' => 'passenger_middle_name_' . $i, 'label' => __( 'passenger_middle_name_' . $i ), 'wrapper_class' => '_billing_company_field' ) );
- woocommerce_wp_text_input( array( 'id' => 'passenger_last_name_' . $i, 'label' => __( 'passenger_last_name_' . $i ), 'wrapper_class' => '_billing_company_field' ) );
- woocommerce_wp_text_input( array( 'id' => 'passenger_date_of_birth_' . $i, 'label' => __( 'passenger_date_of_birth_' . $i ), 'wrapper_class' => '_billing_company_field' ) );
- woocommerce_wp_text_input( array( 'id' => 'passenger_dietry_required_' . $i, 'label' => __( 'passenger_dietry_required_' . $i ), 'wrapper_class' => '_billing_company_field' ) );
- woocommerce_wp_text_input( array( 'id' => 'passenger_dietry_preference_' . $i, 'label' => __( 'passenger_dietry_preference_' . $i ), 'wrapper_class' => '_billing_company_field' ) );
- echo '</div>';
- }
- }
- }
- /*
- function kia_save_extra_details( $post_id, $post ){
- global $woocommerce;
- $order_item = $order->get_items();
- foreach( $order_item as $product ) {
- $prodct_qty = $product['qty'];
- for ( $i = 1; $i <= $prodct_qty; $i++) {
- update_post_meta( $post_id, 'passenger_title_' . $i, wc_clean( $_POST[ 'passenger_title_' . $i ] ) );
- update_post_meta( $post_id, 'passenger_first_name_' . $i, wc_clean( $_POST[ 'passenger_first_name_' . $i ] ) );
- update_post_meta( $post_id, 'passenger_middle_name_' . $i, wc_clean( $_POST[ 'passenger_middle_name_' . $i ] ) );
- update_post_meta( $post_id, 'passenger_last_name_' . $i, wc_clean( $_POST[ 'passenger_last_name_' . $i ] ) );
- update_post_meta( $post_id, 'passenger_date_of_birth_' . $i, wc_clean( $_POST[ 'passenger_date_of_birth_' . $i ] ) );
- update_post_meta( $post_id, 'passenger_dietry_required_' . $i, wc_clean( $_POST[ 'passenger_dietry_required_' . $i ] ) );
- update_post_meta( $post_id, 'passenger_dietry_preference_' . $i, wc_clean( $_POST[ 'passenger_dietry_preference_' . $i ] ) );
- }
- }
- }
- add_action( 'woocommerce_process_shop_order_meta', 'kia_save_extra_details', 45, 2 );
- */
- function wp_child_enqueue_styles() {
- if ( is_checkout() ) {
- wp_enqueue_style( 'cck-jquery-css', get_stylesheet_directory_uri() . '/js/jquery-ui.min.css' );
- wp_enqueue_script( 'cck-jqueryui', get_stylesheet_directory_uri() . '/js/jquery-ui.min.js', array(), '20160412', true );
- wp_enqueue_script( 'cck-custom', get_stylesheet_directory_uri() . '/js/custom.js', array(), '20160412', true );
- }
- }
- add_action( 'wp_enqueue_scripts', 'wp_child_enqueue_styles' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement