WordPress教學 – 動態欄位必填檢查

貨運使用「超商取貨」時,地址欄位為不必填,「宅配」時可以多填「貨到日期和時間」,郵局和宅配時,地址欄位為必填。

結帳畫面:

 

超商付款時,請客人填「店號」 & 「名稱」:

相關文章:

woocommerce 超商取貨

 

 

客人看到宅配的訂單畫面:

 

客人看到超商取貨的訂單畫面:

 

宅配後台接到的訂單畫面:

 

超商取貨後台接到的訂單畫面:

 

畫面都看完了,要怎麼實作,首先你要看過這一篇基礎教學:
WordPress教學 – 如何客製化帳單地址表單
http://stackoverflow.max-everyday.com/2017/04/customize-the-wordpress-woocommerce-billing-address-edit-forms/

 

看完上面教學,實作結果(密訣),都在我的  themem/functions.php:

下面的code 有一個是寫死的,我在判斷貨運方式的地 方,所以你套到 你的環境時,要手修改為成你database 裡貨運方式的數值。

<?php
/**
 * Sabino functions and definitions
 *
 * @package Sabino
 */

define( 'SABINO_THEME_VERSION' , '1.1.02' );

// Get help / Premium Page
require get_template_directory() . '/upgrade/upgrade.php';

// Load WP included scripts
require get_template_directory() . '/includes/inc/template-tags.php';
require get_template_directory() . '/includes/inc/extras.php';
require get_template_directory() . '/includes/inc/jetpack.php';

// Load Customizer Library scripts
require get_template_directory() . '/customizer/customizer-options.php';
require get_template_directory() . '/customizer/customizer-library/customizer-library.php';
require get_template_directory() . '/customizer/styles.php';
require get_template_directory() . '/customizer/mods.php';

// Load TGM plugin class
require_once get_template_directory() . '/includes/inc/class-tgm-plugin-activation.php';
// Add customizer Upgrade class
require_once( get_template_directory() . '/includes/sabino-pro/class-customize.php' );

if ( ! function_exists( 'sabino_setup' ) ) :
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which
 * runs before the init hook. The init hook is too late for some features, such
 * as indicating support for post thumbnails.
 */
function sabino_setup() {
 
 /**
 * Set the content width based on the theme's design and stylesheet.
 */
 global $content_width;
 if ( ! isset( $content_width ) ) {
 $content_width = 900; /* pixels */
 }

 /*
 * Make theme available for translation.
 * Translations can be filed in the /languages/ directory.
 * If you're building a theme based on sabino, use a find and replace
 * to change 'sabino' to the name of your theme in all the template files
 */
 load_theme_textdomain( 'sabino', get_template_directory() . '/languages' );

 // Add default posts and comments RSS feed links to head.
 add_theme_support( 'automatic-feed-links' );

 /*
 * Let WordPress manage the document title.
 * By adding theme support, we declare that this theme does not use a
 * hard-coded <title> tag in the document head, and expect WordPress to
 * provide it for us.
 */
 add_theme_support( 'title-tag' );

 /*
 * Enable support for Post Thumbnails on posts and pages.
 *
 * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
 */
 add_theme_support( 'post-thumbnails' );

 // This theme uses wp_nav_menu() in one location.
 register_nav_menus( array(
 'primary-menu' => esc_html__( 'Primary Menu', 'sabino' ),
 'secondary-menu' => esc_html__( 'Header Secondary Menu', 'sabino' ),
 'footer-bar' => esc_html__( 'Footer Bar Menu', 'sabino' )
 ) );

 /*
 * Switch default core markup for search form, comment form, and comments
 * to output valid HTML5.
 */
 add_theme_support( 'html5', array(
 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption',
 ) );
 
 // The custom logo is used for the logo
 add_theme_support( 'custom-logo', array(
 'height' => 280,
 'width' => 145,
 'flex-height' => true,
 'flex-width' => true,
 ) );

 // Set up the WordPress core custom background feature.
 add_theme_support( 'custom-background', apply_filters( 'sabino_custom_background_args', array(
 'default-color' => 'F9F9F9',
 'default-image' => get_template_directory_uri() . '/images/demo/sabino-background-image.jpg',
 'default-size' => 'cover',
 'default-attachment' => 'fixed'
 ) ) );
 
 add_theme_support( 'woocommerce' );
 // remark by max.
 //add_theme_support( 'wc-product-gallery-zoom' );
 add_theme_support( 'wc-product-gallery-lightbox' );
 add_theme_support( 'wc-product-gallery-slider' );
}
endif; // sabino_setup
add_action( 'after_setup_theme', 'sabino_setup' );

/**
 * Register widget area.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_sidebar
 */
function sabino_widgets_init() {
 register_sidebar( array(
 'name' => esc_html__( 'Sidebar', 'sabino' ),
 'id' => 'sidebar-1',
 'description' => '',
 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
 'after_widget' => '</aside>',
 'before_title' => '<h4 class="widget-title">',
 'after_title' => '</h4>',
 ) );
 
 register_sidebar(array(
 'name' => __( 'Sabino Footer Standard', 'sabino' ),
 'id' => 'sabino-site-footer-standard',
 'description' => __( 'The footer will divide into however many widgets are placed here.', 'sabino' )
 ));
}
add_action( 'widgets_init', 'sabino_widgets_init' );

/**
 * Enqueue scripts and styles.
 */
function sabino_scripts() {
 wp_enqueue_style( 'sabino-font-default', '//fonts.googleapis.com/css?family=Dosis:200,300,400,500,600,700,800|Open+Sans:300,300i,400,400i,600,600i,700,700i', array(), SABINO_THEME_VERSION );
 
 wp_enqueue_style( 'font-awesome', get_template_directory_uri().'/includes/font-awesome/css/font-awesome.css', array(), '4.7.0' );
 wp_enqueue_style( 'sabino-style', get_stylesheet_uri(), array(), SABINO_THEME_VERSION );
 
 if ( sabino_is_woocommerce_activated() ) :
 wp_enqueue_style( 'sabino-woocommerce-style', get_template_directory_uri().'/templates/css/woocommerce.css', array(), SABINO_THEME_VERSION );
 endif;
 
 wp_enqueue_script( 'caroufredsel', get_template_directory_uri() . "/js/caroufredsel/jquery.carouFredSel-6.2.1-packed.js", array('jquery'), SABINO_THEME_VERSION, true );
 wp_enqueue_script( 'sabino-custom-js', get_template_directory_uri() . "/js/custom.js", array('jquery'), SABINO_THEME_VERSION, true );
 
 wp_enqueue_script( 'sabino-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), SABINO_THEME_VERSION, true );
 
 if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
 wp_enqueue_script( 'comment-reply' );
 }
}
add_action( 'wp_enqueue_scripts', 'sabino_scripts' );

/**
 * Add pingback to header
 */
function sabino_pingback_header() {
 if ( is_singular() && pings_open() ) {
 printf( '<link rel="pingback" href="%s">' . "\n", get_bloginfo( 'pingback_url' ) );
 }
}
add_action( 'wp_head', 'sabino_pingback_header' );

/**
 * To maintain backwards compatibility with older versions of WordPress
 */
function sabino_the_custom_logo() {
 if ( function_exists( 'the_custom_logo' ) ) {
 the_custom_logo();
 }
}

/**
 * Add theme stying to the theme content editor
 */
function sabino_add_editor_styles() {
 add_editor_style( 'style-theme-editor.css' );
}
add_action( 'admin_init', 'sabino_add_editor_styles' );

/**
 * Enqueue admin styling.
 */
function sabino_load_admin_script() {
 wp_enqueue_style( 'sabino-admin-css', get_template_directory_uri() . '/upgrade/css/admin-css.css' );
}
add_action( 'admin_enqueue_scripts', 'sabino_load_admin_script' );

/**
 * Enqueue sabino custom customizer styling.
 */
function sabino_load_customizer_script() {
 wp_enqueue_script( 'sabino-customizer-js', get_template_directory_uri() . '/customizer/customizer-library/js/customizer-custom.js', array('jquery'), SABINO_THEME_VERSION, true );
 wp_enqueue_style( 'sabino-customizer-css', get_template_directory_uri() . '/customizer/customizer-library/css/customizer.css' );
}
add_action( 'customize_controls_enqueue_scripts', 'sabino_load_customizer_script' );

/**
 * Check if WooCommerce exists.
 */
if ( ! function_exists( 'sabino_is_woocommerce_activated' ) ) :
 function sabino_is_woocommerce_activated() {
 if ( class_exists( 'woocommerce' ) ) { return true; } else { return false; }
 }
endif; // sabino_is_woocommerce_activated

// If WooCommerce exists include ajax cart
if ( sabino_is_woocommerce_activated() ) {
 require get_template_directory() . '/includes/inc/woocommerce-cart.php';
}

/**
 * Add classed to the body tag from settings
 */
function sabino_add_body_class( $classes ) {
 // Blog Pages
 if ( get_theme_mod( 'sabino-blog-leftsidebar' ) ) {
 $classes[] = 'sabino-blog-leftsidebar';
 }
 if ( get_theme_mod( 'sabino-blog-archive-leftsidebar' ) ) {
 $classes[] = 'sabino-blog-archives-leftsidebar';
 }
 if ( get_theme_mod( 'sabino-blog-single-leftsidebar' ) ) {
 $classes[] = 'sabino-blog-single-leftsidebar';
 }
 
 // WooCommerce Pages
 if ( get_theme_mod( 'sabino-woocommerce-shop-leftsidebar' ) ) {
 $classes[] = 'sabino-shop-leftsidebar';
 }
 if ( get_theme_mod( 'sabino-woocommerce-shop-archive-leftsidebar' ) ) {
 $classes[] = 'sabino-shop-archives-leftsidebar';
 }
 if ( get_theme_mod( 'sabino-woocommerce-shop-single-leftsidebar' ) ) {
 $classes[] = 'sabino-shop-single-leftsidebar';
 }
 
 if ( get_theme_mod( 'sabino-woocommerce-shop-fullwidth' ) ) {
 $classes[] = 'sabino-shop-full-width';
 }
 if ( get_theme_mod( 'sabino-woocommerce-shop-archive-fullwidth' ) ) {
 $classes[] = 'sabino-shop-archives-full-width';
 }
 if ( get_theme_mod( 'sabino-woocommerce-shop-single-fullwidth' ) ) {
 $classes[] = 'sabino-shop-single-full-width';
 }
 
 return $classes;
}
add_filter( 'body_class', 'sabino_add_body_class' );

/**
 * If set, add inline style for page featured image
 */
function sabino_page_featured_image_inline_css() {
 wp_enqueue_style( 'sabino-style', get_stylesheet_uri() );
 
 $sabino_page_featured_image = '';
 
 if ( is_home() || is_archive() || is_search() || is_single() ) {
 if ( sabino_is_woocommerce_activated() ) {
 if ( is_woocommerce() ) {
 $shop = get_option( 'woocommerce_shop_page_id' );
 $sabino_page_featured_image = wp_get_attachment_url( get_post_thumbnail_id( $shop ) );
 } else {
 $page = get_option( 'page_for_posts' );
 $sabino_page_featured_image = wp_get_attachment_url( get_post_thumbnail_id( $page ) );
 }
 } else {
 $page = get_option( 'page_for_posts' );
 $sabino_page_featured_image = wp_get_attachment_url( get_post_thumbnail_id( $page ) );
 }
 } elseif ( is_page() ) {
 $page = get_queried_object();
 $sabino_page_featured_image = wp_get_attachment_url( get_post_thumbnail_id( $page->ID ) );
 } else {
 $page_id = get_the_ID();
 $sabino_page_featured_image = wp_get_attachment_url( get_post_thumbnail_id( $page_id ) );
 }
 
 if ( $sabino_page_featured_image ) {
 $sabino_page_featured_img = htmlspecialchars_decode( 'body { background-image: url(' . $sabino_page_featured_image . ') !important; }' );
 wp_add_inline_style( 'sabino-style', $sabino_page_featured_img );
 }
}
add_action( 'wp_enqueue_scripts', 'sabino_page_featured_image_inline_css' );

/**
 * Add classes to the blog list for styling.
 */
function sabino_add_post_classes ( $classes ) {
 global $current_class;
 
 if ( is_home() || is_archive() || is_search() ) :
 $sabino_blog_layout = 'blog-left-layout';
 if ( get_theme_mod( 'sabino-set-blog-layout' ) ) {
 $sabino_blog_layout = get_theme_mod( 'sabino-set-blog-layout' );
 }
 $classes[] = $sabino_blog_layout;
 
 $classes[] = $current_class;
 $current_class = ( $current_class == 'blog-alt-odd' ) ? sanitize_html_class( 'blog-alt-even' ) : sanitize_html_class( 'blog-alt-odd' );
 endif;
 
 return $classes;
}
global $current_class;
$current_class = sanitize_html_class( 'blog-alt-odd' );
add_filter ( 'post_class' , 'sabino_add_post_classes' );

/**
 * Adjust is_home query if sabino-blog-cats is set
 */
function sabino_set_blog_queries( $query ) {
 $blog_query_set = '';
 if ( get_theme_mod( 'sabino-blog-cats' ) ) {
 $blog_query_set = get_theme_mod( 'sabino-blog-cats' );
 }
 
 if ( $blog_query_set ) {
 // do not alter the query on wp-admin pages and only alter it if it's the main query
 if ( !is_admin() && $query->is_main_query() ){
 if ( is_home() ){
 $query->set( 'cat', $blog_query_set );
 }
 }
 }
}
add_action( 'pre_get_posts', 'sabino_set_blog_queries' );

/**
 * Display recommended plugins with the TGM class
 */
function sabino_register_required_plugins() {
 $plugins = array(
 // The recommended WordPress.org plugins.
 array(
 'name' => __( 'Page Builder', 'sabino' ),
 'slug' => 'siteorigin-panels',
 'required' => false,
 ),
 array(
 'name' => __( 'WooCommerce', 'sabino' ),
 'slug' => 'woocommerce',
 'required' => false,
 ),
 array(
 'name' => __( 'Widgets Bundle', 'sabino' ),
 'slug' => 'siteorigin-panels',
 'required' => false,
 ),
 array(
 'name' => __( 'Contact Form 7', 'sabino' ),
 'slug' => 'contact-form-7',
 'required' => false,
 ),
 array(
 'name' => __( 'Breadcrumb NavXT', 'sabino' ),
 'slug' => 'breadcrumb-navxt',
 'required' => false,
 ),
 array(
 'name' => __( 'Meta Slider', 'sabino' ),
 'slug' => 'ml-slider',
 'required' => false,
 )
 );
 $config = array(
 'id' => 'sabino',
 'menu' => 'tgmpa-install-plugins',
 'message' => '',
 );

 tgmpa( $plugins, $config );
}
add_action( 'tgmpa_register', 'sabino_register_required_plugins' );

/**
 * Register a custom Post Categories ID column
 */
function sabino_edit_cat_columns( $sabino_cat_columns ) {
 $sabino_cat_in = array( 'cat_id' => 'Category ID <span class="cat_id_note">For the Default Slider</span>' );
 $sabino_cat_columns = sabino_cat_columns_array_push_after( $sabino_cat_columns, $sabino_cat_in, 0 );
 return $sabino_cat_columns;
}
add_filter( 'manage_edit-category_columns', 'sabino_edit_cat_columns' );

/**
 * Print the ID column
 */
function sabino_cat_custom_columns( $value, $name, $cat_id ) {
 if( 'cat_id' == $name ) 
 echo $cat_id;
}
add_filter( 'manage_category_custom_column', 'sabino_cat_custom_columns', 10, 3 );

/**
 * Insert an element at the beggining of the array
 */
function sabino_cat_columns_array_push_after( $src, $sabino_cat_in, $pos ) {
 if ( is_int( $pos ) ) {
 $R = array_merge( array_slice( $src, 0, $pos + 1 ), $sabino_cat_in, array_slice( $src, $pos + 1 ) );
 } else {
 foreach ( $src as $k => $v ) {
 $R[$k] = $v;
 if ( $k == $pos )
 $R = array_merge( $R, $sabino_cat_in );
 }
 }
 return $R;
}


// Hook in
add_filter( 'woocommerce_billing_fields' , 'custom_override_billing_address_fields' );

// Our hooked in function - $address_fields is passed via the filter!
function custom_override_billing_address_fields( $fields ) {
 //$fields['billing_company']['required'] = false;
 //$fields['billing_company']['type'] = 'hidden';
 
 //$fields['billing_country']['required'] = false;
 //$fields['billing_country']['type'] = 'hidden';

 //$fields['billing_address_1']['label'] = '帳單地址';
 //$fields['billing_address_2']['required'] = false;
 //$fields['billing_address_2']['type'] = 'hidden';

 $fields['billing_last_name']['required'] = false;
 $fields['billing_last_name']['type'] = 'hidden';

/*
 $fields['billing_CVSStoreID'] = array(
 'label' => __('門市店號', 'woocommerce'),
 'placeholder' => _x('門市店號', 'placeholder', 'woocommerce'),
 'required' => false,
 'class' => array('form-row-first'),
 'clear' => true
 );

 $fields['billing_purchaserStore'] = array(
 'label' => __('門市名稱', 'woocommerce'),
 'placeholder' => _x('門市名稱', 'placeholder', 'woocommerce'),
 'required' => false,
 'class' => array('form-row-last'),
 'clear' => true
 );
*/

 return $fields;
}

// Hook in
add_filter( 'woocommerce_shipping_fields' , 'custom_override_shipping_address_fields' );

// Our hooked in function - $address_fields is passed via the filter!
function custom_override_shipping_address_fields( $address_fields ) {
 //$address_fields['shipping_company']['required'] = false;
 //$address_fields['shipping_company']['type'] = 'hidden';
 
 //$address_fields['shipping_country']['required'] = false;
 //$address_fields['shipping_country']['type'] = 'hidden';

 $address_fields['shipping_address_1']['label'] = '收件地址';
 //$address_fields['shipping_address_2']['required'] = false;
 //$address_fields['shipping_address_2']['type'] = 'hidden';

 $address_fields['shipping_last_name']['required'] = false;
 $address_fields['shipping_last_name']['type'] = 'hidden';

 $address_fields['shipping_email']['required'] = false;
 $address_fields['shipping_email']['type'] = 'hidden';

 $address_fields['shipping_phone']['required'] = true;
 $address_fields['shipping_phone']['type'] = 'text';
 $address_fields['shipping_phone']['label'] = '收件人電話';
 $address_fields['shipping_first_name']['label'] = '收件人';

/*
 $address_fields['shipping_CVSStoreID'] = array(
 'label' => __('門市店號', 'woocommerce'),
 'placeholder' => _x('門市店號', 'placeholder', 'woocommerce'),
 'required' => false,
 'class' => array('form-row-first'),
 'clear' => true
 );

 $address_fields['shipping_purchaserStore'] = array(
 'label' => __('門市名稱', 'woocommerce'),
 'placeholder' => _x('門市名稱', 'placeholder', 'woocommerce'),
 'required' => false,
 'class' => array('form-row-last'),
 'clear' => true
 );
*/

 return $address_fields;
}


// WooCommerce 台灣結帳表單 城市下拉選項
add_filter('woocommerce_states', 'cwp_woocommerce_tw_states');
function cwp_woocommerce_tw_states($states) {
 $states['TW'] = array(
 '基隆市' => '基隆市',
 '台北市' => '台北市',
 '新北市' => '新北市',
 '宜蘭縣' => '宜蘭縣',
 '桃園市' => '桃園市',
 '新竹市' => '新竹市',
 '新竹縣' => '新竹縣',
 '苗栗縣' => '苗栗縣',
 '台中市' => '台中市',
 '彰化縣' => '彰化縣',
 '南投縣' => '南投縣',
 '雲林縣' => '雲林縣',
 '嘉義市' => '嘉義市',
 '嘉義縣' => '嘉義縣',
 '台南市' => '台南市',
 '高雄市' => '高雄市',
 '屏東縣' => '屏東縣',
 '花蓮縣' => '花蓮縣',
 '台東縣' => '台東縣',
 '澎湖' => '澎湖',
 '金門' => '金門',
 '馬祖' => '馬祖',
 '離島地區' => '離島地區',
 );
 return $states;
}


add_filter('woocommerce_default_address_fields', 'cwp_custom_address_fields');
function cwp_custom_address_fields($fields) {

 $fields2['first_name'] = $fields['first_name'];
 $fields2['first_name']['class'] = array('form-row-wide');
 //$fields2['last_name'] = $fields['last_name'];
 $fields2['state'] = $fields['state'];
 $fields2['state']['class'] = array('form-row-first');
 $fields2['state']['required'] = false;
 $fields2['postcode'] = $fields['postcode'];
 $fields2['postcode']['class'] = array('form-row-last');
 $fields2['postcode']['required'] = false;
 $fields2['city'] = $fields['city'];
 $fields2['city']['label'] = '鄉鎮[市]區';
 $fields2['city']['required'] = false;
 
 //$fields2['city']['class'] = array('form-row-first');
 $fields2['address_1'] = $fields['address_1'];
 $fields2['address_1']['required'] = false;

 //$fields2['address_1']['class'] = array('form-row-last');
 $fields2['email'] = $fields['email'];
 $fields2['phone'] = $fields['phone'];
 //$fields2['country'] = $fields['country'];
 //$fields2['company'] = $fields['company'];
 //$fields2['address_2'] = $fields['address_2'];

 return $fields2;
}


// format address for Taiwan
//--------------------------------------------------------------------------------------------------
add_filter( 'woocommerce_localisation_address_formats' , 'woo_tw_address_formats' );
function woo_tw_address_formats() {
 
 $address = array('default' => "{first_name}\n({postcode}) {state}{city}\n{address_1}");

 return $address;
}



// let’s save the new field to order custom fields
//--------------------------------------------------------------------------------------------------
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_save');
function my_custom_checkout_field_save( $order_id )
{
 // custom field
 $purchaserStore = 'purchaserStore' ;
 $CVSStoreID = 'CVSStoreID' ;
 // save custom field to order 
 if( !empty($_POST['purchaserStore']) && !empty($_POST['CVSStoreID']) ){
 update_post_meta( $order_id, $purchaserStore , sanitize_text_field( $_POST['purchaserStore'] ) );
 update_post_meta( $order_id, $CVSStoreID , sanitize_text_field( $_POST['CVSStoreID'] ) );
 }

 //指定日期和時間
 $deliveryDate = 'deliveryDate' ;
 $deliveryTime = 'deliveryTime' ;
 if ( ! empty( $_POST['deliveryDate'] ) ) {
 update_post_meta( $order_id, $deliveryDate , sanitize_text_field( $_POST['deliveryDate'] ) );
 }
 if ( ! empty( $_POST['deliveryTime'] ) ) {
 update_post_meta( $order_id, $deliveryTime , sanitize_text_field( $_POST['deliveryTime'] ) );
 }
}

// add new field
//--------------------------------------------------------------------------------------------------
add_action('woocommerce_before_order_notes', 'my_custom_checkout_field_form' );
function my_custom_checkout_field_form( $checkout ) {
?>
<p class="form-row form-row-first woocommerce-validated" id="CVSStoreID_field" data-sort="" style="display: block;"><label for="CVSStoreID" class="">門市店號 <abbr class="required" title="必要欄位">*</abbr></label><input class="input-text " name="CVSStoreID" id="CVSStoreID" placeholder="門市店號" value="" type="text"></p>
<p class="form-row form-row-last woocommerce-validated" id="purchaserStore_field" data-sort="" style="display: block;"><label for="purchaserStore" class="">門市名稱 <abbr class="required" title="必要欄位">*</abbr></label><input class="input-text " name="purchaserStore" id="purchaserStore" placeholder="門市名稱" value="" type="text"></p>
<p class="form-row form-row-wide" id="queryStoreBlock" style="display: block;"> <a href="https://emap.pcsc.com.tw/" target="emap"><span class="menu-search"><i class="fa fa-search search-btn"></i></span> 7-11 門市店號查詢</a>
</p>
<p class="form-row form-row-first woocommerce-validated" id="deliveryDate_field" data-sort="" style="display: block;"><label for="deliveryDate" class="">指定日期</label> <input class="input-text " name="deliveryDate" id="deliveryDate" placeholder="指定宅配貨到日期" value="" type="text"></p>
<p class="form-row form-row-last woocommerce-validated" id="deliveryTime_field" data-sort="" style="display: block;"><label for="deliveryTime" class="">指定時間</label> <input class="input-text " name="deliveryTime" id="deliveryTime" placeholder="指定宅配貨到時間" value="" type="text"></p>
<p class="form-row form-row-wide woocommerce-validated"><hr/></p>
<?php
}


// dynmanic field check for 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.
 global $woocommerce;
 $shipping_method = $woocommerce->session->get( 'chosen_shipping_methods' );

 if ($shipping_method[0] == "flat_rate:5") {
 if (! $_POST['CVSStoreID'])
 {
 $field_label = '門市店號';
 wc_add_notice( apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ), $field_label ) , 'error'); 
 }
 if (! $_POST['purchaserStore'])
 {
 $field_label = '門市名稱';
 wc_add_notice( apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ), $field_label ) , 'error'); 
 }
 }
 else {
 if (! $_POST['ship_to_different_address']) {
 if (! $_POST['billing_postcode']) 
 {
 $field_label = '郵遞區號';
 $field_label = sprintf( __( 'Billing %s', 'woocommerce' ), strtolower( $field_label ) );
 wc_add_notice( apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ), $field_label ) , 'error');
 }
 if (! $_POST['billing_city'])
 {
 $field_label = '鄉鎮[市]區';
 $field_label = sprintf( __( 'Billing %s', 'woocommerce' ), strtolower( $field_label ) );
 wc_add_notice( apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ), $field_label ) , 'error');
 }
 if (! $_POST['billing_state'])
 {
 $field_label = '縣 / 市';
 $field_label = sprintf( __( 'Billing %s', 'woocommerce' ), strtolower( $field_label ) );
 wc_add_notice( apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ), $field_label ) , 'error');
 }
 if (! $_POST['billing_address_1'])
 {
 $field_label = '地址';
 $field_label = sprintf( __( 'Billing %s', 'woocommerce' ), strtolower( $field_label ) );
 wc_add_notice( apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ), $field_label ) , 'error');
 }
 }
 else {
 if (! $_POST['shipping_postcode']) 
 {
 $field_label = '郵遞區號';
 $field_label = sprintf( __( 'Shipping %s', 'woocommerce' ), strtolower( $field_label ) );
 wc_add_notice( apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ), $field_label ) , 'error');
 }
 if (! $_POST['shipping_city'])
 {
 $field_label = '鄉鎮[市]區';
 $field_label = sprintf( __( 'Shipping %s', 'woocommerce' ), strtolower( $field_label ) );
 wc_add_notice( apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ), $field_label ) , 'error');
 }
 if (! $_POST['shipping_state'])
 {
 $field_label = '縣 / 市';
 $field_label = sprintf( __( 'Shipping %s', 'woocommerce' ), strtolower( $field_label ) );
 wc_add_notice( apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ), $field_label ) , 'error');
 }
 if (! $_POST['shipping_address_1'])
 {
 $field_label = '收件地址';
 $field_label = sprintf( __( 'Shipping %s', 'woocommerce' ), strtolower( $field_label ) );
 wc_add_notice( apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ), $field_label ) , 'error');
 }
 }
 
 }
}


// show our shipping address
//--------------------------------------------------------------------------------------------------
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_admin_shipping_address', 10, 2); 
function my_custom_admin_shipping_address($order){
 $deliveryDate = get_post_meta( $order->id, 'deliveryDate', true );
 $deliveryTime = get_post_meta( $order->id, 'deliveryTime', true );
 if ( ! empty($deliveryDate) || ! empty($deliveryTime)) {
 echo '<p><strong>'.__('宅配指定時間/時間').':</strong> ' . $deliveryDate . ' / '. $deliveryTime .'</p>';
 }

 $CVSStoreID = get_post_meta( $order->id, 'CVSStoreID', true );
 $purchaserStore = get_post_meta( $order->id, 'purchaserStore', true );
 if ( ! empty($CVSStoreID) && ! empty($purchaserStore)) {
 echo '<p><strong>'.__('7-11超商店號/名稱').':</strong> ' . $CVSStoreID . ' / '. $purchaserStore .'</p>';
 }
}


// If you wish to display the custom field value on the admin order edition page, you can add this code:
//--------------------------------------------------------------------------------------------------
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_admin_billing_address', 10, 1 );
function my_custom_admin_billing_address($order){
 $atm_code = get_post_meta( $order->id, 'atm_code', true );
 $atm_datetime = get_post_meta( $order->id, 'atm_datetime', true );
 if(strlen($atm_code)==5 && is_numeric($atm_code)) {
 echo '<p><strong>'.__('ATM匯款').':</strong> (' . $atm_code . ')'. $atm_datetime .'</p>';
 }
 else {
 echo '<p><strong>'.__('ATM匯款').':</strong> 待匯款</p>';
 }
}


?>

 

相關文章

寫留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *