覆盖WordPress

时间:2017-12-28 10:32:51

标签: php wordpress plugins

我试图覆盖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 );
}

2 个答案:

答案 0 :(得分:1)

实际上你不能简单地覆盖一个功能。它只有在被if ( ! function_exists() )条件包围的情况下才有可能。在你的情况下,它似乎是一个类方法,并覆盖它(在纯PHP中)你应该扩展类并覆盖该方法。在你的WordPress场景中它更简单,因为你可以使用一个简单的过滤器。您的代码应该是:

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' );

确保不要简单地复制/粘贴代码,因为$this变量仅在类范围内可用。你可以把这个函数放到一个简单的插件中,它就会开始工作。

希望它有所帮助!

答案 1 :(得分:0)

我试图阅读更多关于此的内容,并提出了这个有效的解决方案。我们可以使用WordPress过滤器将hack添加到特定事件中。我修改了代码,现在可以了。

    /**
     * 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' );
相关问题