私はWooCommerce関数load_country_states()をオーバーライドしようとしています。私はそれを行うカスタムWordPressのプラグインを作成しています。私はsome answersで、PHPの関数を単純に上書きすることはできないと読んでいます。 WordPressのテーマ/プラグインで定義された関数をプラグインに書き直すにはどうすればいいですか?私はthisを読んだ。修正するWordPressで関数をオーバーライドする
機能:実はあなたは、単に関数をオーバーライドすることはできません
/**
* 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);
}
WordPressフィルタは返品として何かを期待していることに注意してください。私はあなたのコードがうまくいくのを見てうれしいですが、一般的な場合、元のクラスの 'states'プロパティはおそらく正しい値を受け取らないでしょう;) その結果として何らかの問題に直面した場合、あなたの関数にパラメータを渡し、値を変更せずにそれを返します。 –