简体   繁体   English

WooCommerce选择性地显示缺货的产品

[英]WooCommerce show out of stock products SELECTIVELY

How can I selectively show out of stock products in a category view? 如何在类别视图中有选择地显示缺货产品? I know I can do it in the WooCommerce options panel for all products but I need to control this more. 我知道我可以在所有产品的WooCommerce选项面板中做到这一点,但我需要对此进行更多控制。 I'm thinking along the lines of adding something into the functions.php of my theme such as: 我正在考虑向主题的functions.php中添加一些内容,例如:

add_action( 'xyz', function() {
    global $product;

    if ( !$product->is_in_stock() ) {
        //Need to make it viewable here but selectively, not globally;
    }
});

A single checkbox toggle on the product page would be perfect, eg "Show even if stock level is zero". 产品页面上的单个复选框切换将是完美的,例如“即使库存水平为零也显示”。

Note - With > 500 products, I need to have a checkbox for those few that I need visible, not the other way round. 注意-对于500多种产品,我需要为那些我需要看到的少数产品提供一个复选框,而不是相反。

Any ideas? 有任何想法吗?

Well that was a heck of a lot trickier than I thought it was going to be. 嗯,这比我想的要复杂得多。 The solution comes in three parts. 解决方案分为三个部分。

First, you must add a checkbox to the admin product metabox. 首先,您必须将一个复选框添加到管理产品metabox。 I thought it would be appropriate if we placed it near the stock status inputs. 我认为如果将其放在库存状态输入附近会比较合适。

add_action( 'woocommerce_product_options_stock_status', 'so_27971630_hide_if_out_of_stock' );

function so_27971630_hide_if_out_of_stock(){
    woocommerce_wp_checkbox( array( 'id' => '_hide_if_out_of_stock', 'wrapper_class' => 'show_if_simple show_if_variable', 'label' => __( 'Hide this product from archives when out of stock?', 'your-plugin-domain' ) ) );
}

Then we need to save this data. 然后我们需要保存该数据。 Normally, I'd save a checkbox as 'yes' versus 'no' like WooCommerce does. 通常,我会像WooCommerce一样将复选框保存为“是”与“否”。 However, getting the product query correct, required that the meta exist when you wanted to hide the item and not exist at all otherwise... hence the if/else update_post_meta() versus delete_post_meta() 但是,要使产品查询正确无误,则需要在您要隐藏商品时该meta存在,而在其他情况下则根本不存在...因此,if / else update_post_meta()delete_post_meta()

add_action( 'woocommerce_process_product_meta', 'so_27971630_save_product_meta' );

function so_27971630_save_product_meta( $post_id ){
    if( isset( $_POST['_hide_if_out_of_stock'] ) ) {
        update_post_meta( $post_id, '_hide_if_out_of_stock', 'yes' );
    } else {
        delete_post_meta( $post_id, '_hide_if_out_of_stock' );
    }
}

Finally, we need to tweak the product query. 最后,我们需要调整产品查询。 WooCommerce builds a custom query for products in its WC_Query class. WooCommerce在其WC_Query类中为产品构建自定义查询。 Basically what I've done is in the case where you aren't mass hiding all out of stock items via the plugin option, this code will modify the meta query so that any item that does not have the meta key _hide_if_out_of_stock will be shown. 基本上,我要做的是在您没有通过plugin选项大量隐藏所有缺货商品的情况下,此代码将修改元查询,以便显示任何不带有元键_hide_if_out_of_stock That is a counter-intuitive way of sayingn that any product where the box "hide when out of stock" is checked will be hidden. 这是一种违反直觉的说法,即选中“在缺货时隐藏”框的任何产品都将被隐藏。

add_action( 'woocommerce_product_query', 'so_27971630_product_query' );

function so_27971630_product_query( $q ){

    $meta_query = $q->get( 'meta_query' );

    if ( get_option( 'woocommerce_hide_out_of_stock_items' ) == 'no' ) {
        $meta_query[] = array(
                    'key'       => '_hide_if_out_of_stock',
                    'compare'   => 'NOT EXISTS'
                );
    }

    $q->set( 'meta_query', $meta_query );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM