php 类Woo_Provinces #wordpress

Posted

tags:

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

<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * The class to handle Vietnam Provinces
 *
 * @since    1.0
 *
 */
class Woo_Provinces {

	/**
	 * Constructor: Add filters
	 */
	public function __construct() {
		add_filter( 'woocommerce_states', array( $this, 'add_provinces' ) );
		add_filter( 'woocommerce_get_country_locale', array( $this, 'edit_vn_locale' ) );
		add_filter( 'woocommerce_localisation_address_formats', array( $this, 'edit_vn_address_formats' ) );

		// Enqueue province scripts
		add_action( 'wp_enqueue_scripts', array( $this, 'load_provinces_scripts' ) );
	}

	/**
	 * Change the address format of Vietnam, add {state} (or "Province" in Vietnam)
	 *
	 * @param $array
	 *
	 * @return array
	 */
	public function edit_vn_address_formats( $array ) {

		$array['VN'] = "{name}\n{company}\n{address_1}\n{city}\n{state}\n{country}";

		return $array;

	}

	/**
	 * Change the way displaying address fields in the checkout page when selecting Vietnam
	 *
	 * @param $array
	 *
	 * @return array
	 */
	public function edit_vn_locale( $array ) {
		$array['VN']['state']['label']    = __( 'Province' );
		$array['VN']['state']['required'] = true;

		$array['VN']['city']['label']      = __( 'District' );
		$array['VN']['postcode']['hidden'] = true;

		return $array;
	}


	/**
	 * Add 63 provinces of Vietnam
	 *
	 * @param $states
	 *
	 * @return array
	 */
	public function add_provinces( $states ) {
		/**
		 * @source: https://vi.wikipedia.org/wiki/Tỉnh_thành_Việt_Nam and https://en.wikipedia.org/wiki/Provinces_of_Vietnam
		 */
		$states['VN'] = array(
			'AN-GIANG'        => __( 'An Giang' ),
			'BA-RIA-VUNG-TAU' => __( 'Ba Ria - Vung Tau' ),
			'BAC-LIEU'        => __( 'Bac Lieu' ),
			'BAC-KAN'         => __( 'Bac Kan' ),
			'BAC-GIANG'       => __( 'Bac Giang' ),
			'BAC-NINH'        => __( 'Bac Ninh' ),
			'BEN-TRE'         => __( 'Ben Tre' ),
			'BINH-DUONG'      => __( 'Binh Duong' ),
			'BINH-DINH'       => __( 'Binh Dinh' ),
			'BINH-PHUOC'      => __( 'Binh Phuoc' ),
			'BINH-THUAN'      => __( 'Binh Thuan' ),
			'CA-MAU'          => __( 'Ca Mau' ),
			'CAO-BANG'        => __( 'Cao Bang' ),
			'CAN-THO'         => __( 'Can Tho' ),
			'DA-NANG'         => __( 'Da Nang' ),
			'DAK-LAK'         => __( 'Dak Lak' ),
			'DAK-NONG'        => __( 'Dak Nong' ),
			'DONG-NAI'        => __( 'Dong Nai' ),
			'DONG-THAP'       => __( 'Dong Thap' ),
			'DIEN-BIEN'       => __( 'Dien Bien' ),
			'GIA-LAI'         => __( 'Gia Lai' ),
			'HA-GIANG'        => __( 'Ha Giang' ),
			'HA-NAM'          => __( 'Ha Nam' ),
			'HA-NOI'          => __( 'Ha Noi' ),
			'HA-TINH'         => __( 'Ha Tinh' ),
			'HAI-DUONG'       => __( 'Hai Duong' ),
			'HAI-PHONG'       => __( 'Hai Phong' ),
			'HOA-BINH'        => __( 'Hoa Binh' ),
			'HAU-GIANG'       => __( 'Hau Giang' ),
			'HUNG-YEN'        => __( 'Hung Yen' ),
			'HO-CHI-MINH'     => __( 'Ho Chi Minh' ),
			'KHANH-HOA'       => __( 'Khanh Hoa' ),
			'KIEN-GIANG'      => __( 'Kien Giang' ),
			'KON-TUM'         => __( 'Kon Tum' ),
			'LAI-CHAU'        => __( 'Lai Chau' ),
			'LAO-CAI'         => __( 'Lao Cai' ),
			'LANG-SON'        => __( 'Lang Son' ),
			'LAM-DONG'        => __( 'Lam Dong' ),
			'LONG-AN'         => __( 'Long An' ),
			'NAM-DINH'        => __( 'Nam Dinh' ),
			'NGHE-AN'         => __( 'Nghe An' ),
			'NINH-BINH'       => __( 'Ninh Binh' ),
			'NINH-THUAN'      => __( 'Ninh Thuan' ),
			'PHU-THO'         => __( 'Phu Tho' ),
			'PHU-YEN'         => __( 'Phu Yen' ),
			'QUANG-BINH'      => __( 'Quang Binh' ),
			'QUANG-NAM'       => __( 'Quang Nam' ),
			'QUANG-NGAI'      => __( 'Quang Ngai' ),
			'QUANG-NINH'      => __( 'Quang Ninh' ),
			'QUANG-TRI'       => __( 'Quang Tri' ),
			'SOC-TRANG'       => __( 'Soc Trang' ),
			'SON-LA'          => __( 'Son La' ),
			'TAY-NINH'        => __( 'Tay Ninh' ),
			'THAI-BINH'       => __( 'Thai Binh' ),
			'THAI-NGUYEN'     => __( 'Thai Nguyen' ),
			'THANH-HOA'       => __( 'Thanh Hoa' ),
			'THUA-THIEN-HUE'  => __( 'Thua Thien - Hue' ),
			'TIEN-GIANG'      => __( 'Tien Giang' ),
			'TRA-VINH'        => __( 'Tra Vinh' ),
			'TUYEN-QUANG'     => __( 'Tuyen Quang' ),
			'VINH-LONG'       => __( 'Vinh Long' ),
			'VINH-PHUC'       => __( 'Vinh Phuc' ),
			'YEN-BAI'         => __( 'Yen Bai' ),
		);

		return $states;

	}

	/**
	* Enqueue provinces scripts
	*
	* Arrange the address field orders to the Vietnam standard in the checkout page: Country - Province - District - Address
	* @author 	Longkt
	* @since 	1.4
	*/
	public function load_provinces_scripts() {
		// Enqueue province style
		wp_enqueue_style( 'woo-provinces-style', get_stylesheet_directory_uri().'/hoangweb/provinces.css'  );

		// Enqueue province script
		wp_enqueue_script( 'woo-provinces-script', get_stylesheet_directory_uri ().'/hoangweb/provinces.js', array( 'jquery' ), '1.0', true );
	}
}

以上是关于php 类Woo_Provinces #wordpress的主要内容,如果未能解决你的问题,请参考以下文章

WordPress搭建

WordPress获取某个分类关联的标签

带有正则表达式通配符的 WordPress PHP 重定向

需要 php 编码特殊字符而不是 html 标签,以便包含在 wordpress 扩展 rss 文件中

使用多个查询重定向

如何搭建本地WordPress