【发布时间】:2018-06-08 21:41:45
【问题描述】:
我正在尝试覆盖 WooCommerce 函数 load_country_states()。我正在创建一个自定义 WordPress 插件来做到这一点。我在some answers 中读到,不能简单地覆盖 PHP 中的函数。在插件中重写 WordPress 主题/插件中定义的函数的最佳方法是什么?我已阅读this。
要修改的函数:
/**
* Load the states.
*/
public function load_country_states() {
global $states;
// States set to array() are blank i.e. the country has no use for the state field.
$states = array(
'AF' => array(),
'AT' => array(),
'AX' => array(),
'BE' => array(),
'BI' => array(),
'CZ' => array(),
'DE' => array(),
'DK' => array(),
'EE' => array(),
'FI' => array(),
'FR' => array(),
'IS' => array(),
'IL' => array(),
'KR' => array(),
'NL' => array(),
'NO' => array(),
'PL' => array(),
'PT' => array(),
'SG' => array(),
'SK' => array(),
'SI' => array(),
'LK' => array(),
'SE' => array(),
'VN' => array(),
);
// Load only the state files the shop owner wants/needs.
$allowed = array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() );
if ( ! empty( $allowed ) ) {
foreach ( $allowed as $code => $country ) {
if ( ! isset( $states[ $code ] ) && file_exists( WC()->plugin_path() . '/i18n/states/' . $code . '.php' ) ) {
include( WC()->plugin_path() . '/i18n/states/' . $code . '.php' );
}
}
}
$this->states = apply_filters( 'woocommerce_states', $states );
}
修改后应该是这样的:
/**
* Load the states.
*/
public function load_country_states() {
global $states;
// Load only the state files the shop owner wants/needs.
$allowed = array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() );
if ( ! empty( $allowed ) ) {
foreach ( $allowed as $code => $country ) {
if ( ! isset( $states[ $code ] ) && file_exists( WC()->plugin_path() . '/i18n/states/' . $code . '.php' ) ) {
include( WC()->plugin_path() . '/i18n/states/' . $code . '.php' );
}
}
}
$this->states = apply_filters( 'woocommerce_states', $states );
}
【问题讨论】: