[英]WP_Query issue when trying to get out of stock products in WooCommerce
I'm using shortcode to display all sold out WooCommerce
items on a WordPress
page.我正在使用简码在
WordPress
页面上显示所有售罄的WooCommerce
项目。 It works fine, except for throwing an error:它工作正常,除了抛出错误:
Warning: array_filter() expects parameter 1 to be array, null given in C:\xampp\htdocs\wordpress_4\wp-content\plugins\woocommerce\includes\class-wc-query.php on line 688
I figure that this means array_filter()
expects an array, but is given null
from the following code in my functions.php
:我认为这意味着
array_filter()
需要一个数组,但是我的functions.php
的以下代码给出了null
:
add_shortcode( 'out_of_stock_products', 'show_out_of_stock_products_shortcode' );
function show_out_of_stock_products_shortcode() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'outofstock',
)
),
'fields' => 'ids',
);
$product_ids = get_posts( $args );
$product_ids = implode( ",", $product_ids );
return do_shortcode("[products ids='$product_ids']");
}
Shortcode: [out_of_stock_products]
简码:
[out_of_stock_products]
Here's the WooCommerce
function that expects the array but is given null
:这是
WooCommerce
函数,它需要数组但被赋予null
:
public function get_meta_query( $meta_query = array(), $main_query = false ) {
if ( ! is_array( $meta_query ) ) {
$meta_query = array();
}
return array_filter( apply_filters( 'woocommerce_product_query_meta_query', $meta_query, $this ) ); //line 688
}
I'm not sure how to fix this.我不知道如何解决这个问题。 Maybe there is a better way to display sold out products altogether, though I like this version.
也许有更好的方式来展示售罄的产品,尽管我喜欢这个版本。
Could anyone help me fix this?谁能帮我解决这个问题?
Source: https://www.businessbloomer.com/woocommerce-display-stock-products-shortcode/来源: https : //www.businessbloomer.com/woocommerce-display-stock-products-shortcode/
Edit:编辑:
As @LoicTheAztec suggests, another bit of code than the ones posted above is responsible:正如@LoicTheAztec 所建议的,除了上面发布的代码之外,还有一些代码是负责的:
add_filter( 'woocommerce_product_query_meta_query', 'show_only_instock_products', 10, 2 );
show_only_instock_products( $meta_query, $query ) {
if ( is_woocommerce() ) {
$meta_query[] = array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '!='
);
return $meta_query;
}
}
I am using this to hide sold products from WooCommerce
.我正在使用它来隐藏
WooCommerce
已售出的产品。
Update 2更新 2
To get out of stock products, you can also use instead:要获得缺货的产品,您还可以使用:
WC_Query
using wc_get_products()
function as follows:WC_Query
使用wc_get_products()
函数,如下所示:add_shortcode( 'out_of_stock_products', 'show_out_of_stock_products_shortcode' );
function show_out_of_stock_products_shortcode() {
$product_ids = (array) wc_get_products( array(
'status' => 'publish',
'limit' => -1,
'stock_status' => 'outofstock',
'return' => 'ids',
) );
$product_ids = empty($product_ids) ? '' : implode( ',', $product_ids );
return empty($product_ids) ? '' : do_shortcode("[products ids='$product_ids']");
}
2). 2)。 A
WP_Query
with a tax query like:带有税务查询的
WP_Query
,例如:
add_shortcode( 'out_of_stock_products', 'show_out_of_stock_products_shortcode' );
function show_out_of_stock_products_shortcode() {
$product_ids = (array) get_posts( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array( array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => array('outofstock'),
) ),
) );
$product_ids = empty($product_ids) ? '' : implode( ',', $product_ids );
return empty($product_ids) ? '' : do_shortcode("[products ids='$product_ids']");
}
Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.
测试和工作。
Related: Hide "out of stock" products with custom meta data In Woocommerce相关: 在 Woocommerce 中使用自定义元数据隐藏“缺货”产品
Addition: The problem comes from your edit (last code).另外:问题来自您的编辑(最后一个代码)。
Also since WooCommerce 3, you should use instead the following:同样从 WooCommerce 3 开始,您应该使用以下内容:
add_filter( 'woocommerce_product_query_tax_query', 'show_only_instock_products', 10, 2 );
show_only_instock_products( $tax_query, $query ) {
if ( ! is_admin() ) {
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => array('outofstock'),
'operator' => 'NOT IN'
);
}
return $tax_query;
}
Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.
测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.