2017-12-28 34 views
2

私は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); 
} 

答えて

1

/** 
* 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); 
} 

を修正した後、それは次のように動作するはずです。条件付きで囲まれているのは、の場合のみ可能です。あなたの場合、それはクラスメソッドと思われる(プレーンPHPで)あなたはクラスを拡張し、メソッドをオーバーライドする必要がありますオーバーライドするようです。 WordPressのシナリオでは、シンプルなフィルタを使用できるため、シンプルです。 $this変数がクラススコープの内側にのみ使用可能ですので、ない単純なコピー/、コードを貼り付けていることを確認してください

public function so48005823_load_country_states($states) { 
    // make your magic here and 
    // don't forget to return the $states var 

    return $states; 
} 
add_filter('woocommerce_states', 'so48005823_load_country_states'); 

:あなたのコードは次のようにする必要があります。この関数を単純なプラグインに入れることができます。

希望すると助かります!

0

私はこれについてさらに詳しくお読みになり、この解決策を考え出しました。 WordPressフィルターを使用して、特定のイベントにハックを追加することができます。私はコードを修正し、現在は動作します。

/** 
    * Load the states. 
    */ 
function load_country_states_modified() { 
     global $states; 
     // Load only the state files the shop owner wants/needs. 
     $foobar = new WC_Countries; // correct 
     $allowed = array_merge($foobar->get_allowed_countries(), $foobar->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); 
    } 
    add_filter('woocommerce_states', 'load_country_states_modified'); 
+0

WordPressフィルタは返品として何かを期待していることに注意してください。私はあなたのコードがうまくいくのを見てうれしいですが、一般的な場合、元のクラスの 'states'プロパティはおそらく正しい値を受け取らないでしょう;) その結果として何らかの問題に直面した場合、あなたの関数にパラメータを渡し、値を変更せずにそれを返します。 –

関連する問題