2017-12-28 8 views
2

WooCommerce 함수 load_country_states()를 재정의하려고합니다. 내가 할 사용자 지정 WordPress 플러그인을 만들고 있어요. 나는 some answers에서 PHP에서 함수를 단순히 오버 라이딩 할 수 없다고 읽었습니다. 플러그인에서 WordPress theme/Plugin에 정의 된 함수를 다시 작성하는 가장 좋은 방법은 무엇입니까? 나는 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); 
} 

수정 후. 그것은 if (! function_exists()) 조건부로 둘러싸인 경우에만 가능합니다. 귀하의 경우 그것은 클래스 메서드로 보인다 및 평범한 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' 속성은 아마도 올바른 값을받지 못할 것입니다;) 그 결과로 어떤 문제에 직면하면, 귀하의 함수에 매개 변수를 그것의 가치를 변경하지 않고 반환합니다. –