WooCommerce 选择性地显示缺货产品 [英] WooCommerce show out of stock products SELECTIVELY

查看:38
本文介绍了WooCommerce 选择性地显示缺货产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在类别视图中有选择地显示缺货产品?我知道我可以在 WooCommerce 选项面板中为所有产品执行此操作,但我需要对此进行更多控制.我正在考虑在我的主题的functions.php中添加一些东西,例如:

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. I'm thinking along the lines of adding something into the functions.php of my theme such as:

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".

注意 - 对于超过 500 种产品,我需要为那些我需要可见的少数产品设置一个复选框,而不是相反.

Note - With > 500 products, I need to have a checkbox for those few that I need visible, not the other way round.

有什么想法吗?

推荐答案

嗯,这比我想象的要复杂得多.解决方案分为三个部分.

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

然后我们需要保存这些数据.通常,我会像 WooCommerce 那样将复选框保存为是"与否".但是,要使产品查询正确,需要在您想隐藏项目时元存在,否则根本不存在......因此,if/else update_post_meta()delete_post_meta()

Then we need to save this data. Normally, I'd save a checkbox as 'yes' versus 'no' like WooCommerce does. 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()

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

最后,我们需要调整产品查询.WooCommerce 在其 WC_Query 类中为产品构建自定义查询.基本上我所做的是在您没有通过插件选项大量隐藏所有缺货商品的情况下,此代码将修改元查询,以便任何没有元键 _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. 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. 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 );
}

这篇关于WooCommerce 选择性地显示缺货产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆