正在覆盖includes文件夹中的WooCommerce函数


Overriding WooCommerce function in includes folder

我想对WooCommerce/includes/中名为class-wc-product-variation.php的文件中的WooCommCommerce中的一个函数进行一些修改

我想要修改的功能是:

public function variation_is_visible() {
    $visible = true;
    // Published == enabled checkbox
    if ( get_post_status( $this->variation_id ) != 'publish' ) {
        $visible = false;
    }
    // Out of stock visibility
    elseif ( get_option('woocommerce_hide_out_of_stock_items') == 'yes' && ! $this->is_in_stock() ) {
        $visible = false;
    }
    // Price not set
    elseif ( $this->get_price() === "" ) {
        $visible = false;
    }
    return apply_filters( 'woocommerce_variation_is_visible', $visible, $this->variation_id, $this->id );
}

我需要在其中添加另一行,如下所示:

    elseif ( get_option('woocommerce_hide_out_of_stock_items') != 'yes' && ! $this->is_in_stock() ) {
        $visible = false;
    }

如何在不更改核心文件的情况下做到这一点?

您永远不应该修改插件中的核心文件。在给定的函数中,有可用的滤波器woocommerce_variation_is_visible。根据您的需要使用该过滤器进行自定义。核心文件中的代码为:

return apply_filters( 'woocommerce_variation_is_visible', $visible, $this->variation_id, $this->id );