I am trying to override a WooCommerce function load_country_states(). I am creating a custom WordPress plugin to do it. I read in some answers that one can't simply override a function in PHP. Whats the best way to rewrite a function defined in WordPress theme/plugin in a plugin? I have read this.
The function to modify:
/**
* 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 );
}
After modification it should work like:
/**
* 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 );
}