Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

php - Hide "out of stock" products with custom meta data In Woocommerce

I'm working on a WooCommerce webshop at the moment and I've added a custom meta field named external_stock where WP All Import imports the stock that is available at our supplier for all of our products every 3 hours. The amount of products we've got in our actual store is being entered in the normal stock field.

What I'm trying to achieve is that the products from which the normal stock and the external_stock are both 0 are not being displayed in the webshop.

I've already edited a plugin in a way that whenever our stock is 0 but the external stock is > 0 the product page displays 'Available within x days' and when both stocks are 0 it will display 'Out Of Stock', but customers can still order the 'Out Of Stock' products, and that's why I want to hide them.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update for Woocommerce 3

Since Woocommerce 3, product stock status is not anymore set as product meta data.

It's now handle by product_visibility custom taxonomy under outofstock term.

So you will need to use a Tax query instead, to hide out of stock products:

add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
    // Get any existing Tax query
    $tax_query = $q->get( 'tax_query');
    
    // Define an additional tax query 
    $tax_query = array(
        'taxonomy' => 'product_visibility',
        'field'    => 'slug',
        'terms'   => array('outofstock'),
        'compare' => 'NOT IN',
    );
    
    // Set the new merged tax query
    $q->set( 'tax_query', $tax_query );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

You can use any WooCommerce conditional tag in an if statement to target for example specific product category or product tag archive pages.


For products containing specific meta data, you will use:

add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
    // Get any existing Tax query
    $tax_query = $q->get( 'tax_query');
    
    // Get any existing meta query
    $meta_query = $q->get( 'meta_query');
    
    // Define an additional tax query 
    $tax_query = array(
        'taxonomy' => 'product_visibility',
        'field'    => 'slug',
        'terms'   => array('outofstock'),
        'compare' => 'NOT IN',
    );
    
    // Define an additional meta query 
    $meta_query = array(
        'key'     => 'external_stock',
        'value'   => '0', //  <===  Set here your desired value (if needed)
        'compare' => '>', //  <===  Set Here the correct compare argument (if needed)
    );
    
    // Set the new merged tax query
    $q->set( 'tax_query', $tax_query );
    
    // Set the new merged meta query
    $q->set( 'meta_query', $meta_query );
}

Original answer:

You could try this custom function hooked in woocommerce_product_query action hook:

add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
    // Get any existing meta query
    $meta_query = $q->get( 'meta_query');
    
    // Define an additional meta query 
    $q->set( 'meta_query', array( array(
        'key'     => '_stock_status',
        'value'   => 'outofstock',
        'compare' => 'NOT LIKE',
    ) ) );
    
    // Set the new merged meta query
    $q->set( 'meta_query', $meta_query );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Code is tested and works.

It will remove all "out of stock" products from shop and archives pages. But it will not hide "out of stock" variations in single product pages for variable products.

For your custom meta_key external_stock, you will have to add it this way:

add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
    // Get any existing meta query
    $meta_query = $q->get( 'meta_query');
    
    $meta_query = array( 
        'relation' => 'AND', // can be also 'OR'
        array(
            'key'     => '_stock_status',
            'value'   => 'outofstock',
            'compare' => 'NOT LIKE',
        ),
        array(
            'key'     => 'external_stock',
            'value'   => '0', //  <===  Set here your desired value (if needed)
            'compare' => '>', //  <===  Set Here the correct compare argument (if needed)
    ) );
    
    // Set the new merged meta query
    $q->set( 'meta_query', $meta_query );
}

This is untested and need to be set and tested by you


Official documentation: WordPress Class Reference WP_Query - Custom Field Parameters


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...