我拥有一家电子商务公司,我想把库存不足3的产品藏起来。
这就是我想说的:
add_action( 'woocommerce_product_query', 'react2wp_hide_products_with_stock_higher_than_two' );
function react2wp_hide_products_with_stock_higher_than_two( $q ){
$meta_query = $q->get( 'meta_query' );
$meta_query[] = array(
'key' => '_stock',
'value' => 2,
'compare' => '>'
);
$q->set( 'meta_query', $meta_query );
}你曾经设置过类似的代码吗?这行有什么错误吗?
发布于 2020-03-26 08:38:13
你已经接近了,加上type
'type' => 'numeric' // specify it for numeric values
function react2wp_hide_products_with_stock_higher_than_two( $q, $query ) {
// Get any existing meta query
$meta_query = $q->get( 'meta_query' );
// Define an additional meta query
$meta_query[] = array(
'key' => '_stock',
'value' => 2,
'type' => 'numeric', // specify it for numeric values
'compare' => '>'
);
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
add_action( 'woocommerce_product_query', 'react2wp_hide_products_with_stock_higher_than_two', 10, 2 );https://stackoverflow.com/questions/60861883
复制相似问题