Overriding WooCommerce function in includes folder

I am looking to make some modifications to a function in WooCommerce, on a file called class-wc-product-variation.php in woocommerce/includes/

The function I’m looking to modify is:

Read More
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 );
}

I need to add another elseif line in there like so:

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

How do I do this without making changes to the core files?

Related posts

Leave a Reply

1 comment

  1. You should never modify the core files in plugin. In the given function, there is filter woocommerce_variation_is_visible available. Use that filter to customize as per your need.
    Code in core file is:

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