从 Woocommerce 档案页面隐藏价格为零或为空的产品
Hide products with zero or empty price from Woocommerce archives pages
我正在使用 Woocommerce 设置批发商店,但无法解决以下问题。
我想自动隐藏或删除所有价格设置为零或产品根本没有价格的产品!
我在网上冲浪,发现以下代码完全可以满足我的需要,但我将其放入我的子主题 functions.php 文件中,但它对我不起作用!
在 WooCommerce 中隐藏价格设置为零的产品
add_action( 'woocommerce_product_query', 'react2wp_hide_products_higher_than_zero' );
function react2wp_hide_products_higher_than_zero( $q ){
$meta_query = $q->get( 'meta_query' );
$meta_query[] = array(
'key' => '_price',
'value' => 0,
'compare' => '>'
);
$q->set( 'meta_query', $meta_query );
}
在 WooCommerce 中隐藏没有价格的产品
add_action( 'woocommerce_product_query', 'react2wp_hide_products_without_price' );
function react2wp_hide_products_without_price( $q ){
$meta_query = $q->get( 'meta_query' );
$meta_query[] = array(
'key' => '_price',
'value' => '',
'compare' => '!='
);
$q->set( 'meta_query', $meta_query );
}
来源 - https://react2wp.com/woocommerce-hide-products-without-price-simple-fix/
我正在使用 Woocommerce 3.4.5 和 Flatsome 主题(更改主题也没有帮助)
如何找出代码不起作用的原因?
感谢任何帮助。
正如疯狂猜测:
将 react2wp_hide_products_without_price()
更改为 'value'=> ''
,更改为 'value'=> 0,
Update - Added an addition to auto move to trash products with zero or empty price
要将存档页面中没有价格或零价格的产品隐藏为商店,请使用以下命令:
add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
function shop_only_instock_products( $meta_query, $query ) {
// In frontend only
if( is_admin() ) return $meta_query;
$meta_query['relation'] = 'OR';
$meta_query[] = array(
'key' => '_price',
'value' => '',
'type' => 'numeric',
'compare' => '!='
);
$meta_query[] = array(
'key' => '_price',
'value' => 0,
'type' => 'numeric',
'compare' => '!='
);
return $meta_query;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
但是使用像您的第一个代码片段这样的东西会得到相同的结果(价格大于零):
add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
function shop_only_instock_products( $meta_query, $query ) {
// In frontend only
if( is_admin() ) return $meta_query;
$meta_query[] = array(
'key' => '_price',
'value' => 0,
'type' => 'numeric',
'compare' => '>'
);
return $meta_query;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
Moving products to trash (without deleting them) that have Zero or empty price.
以下代码会将价格为 0 或 empty 的所有产品的状态更新为 trash
使用非常简单的直接 SQL 查询.
您每次需要 运行 一次即可。
// Function that will add products to trash that have Zero price or no price
function auto_add_product_to_trash(){
global $wpdb;
$number_of_products_processed = $wpdb->query("
UPDATE {$wpdb->prefix}posts as a
JOIN {$wpdb->prefix}postmeta AS b ON a.ID = b.post_id
JOIN {$wpdb->prefix}postmeta AS c ON a.ID = c.post_id
SET a.post_status = 'trash'
WHERE a.post_status LIKE 'publish' AND a.post_type LIKE 'product'
AND b.meta_key LIKE '_price' AND c.meta_key LIKE '_sale_price'
AND ( b.meta_value LIKE '' OR b.meta_value LIKE '0' )
AND c.meta_value != '0'
");
// Return the number of products added to trash
return $number_of_products_processed;
}
// Execute the function - Run it just once and comment it
auto_add_product_to_trash();
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
Now when going to your products in Backend, all products with zero or empty price are in trash. You can review them one by one and delete them easily:
This way everything is secure as you can restore any product added to trash. If you restore a product, you will have to set its status to published (restored products get a "draft" status)
您可以在管理员上进行。按价格排序 - 会将所有标价的商品放在一起,将没有标价的商品放在一起。批量删除您所在的页面,并通过更改屏幕选项 > 每页项目数来操纵您看到的内容
我正在使用 Woocommerce 设置批发商店,但无法解决以下问题。
我想自动隐藏或删除所有价格设置为零或产品根本没有价格的产品!
我在网上冲浪,发现以下代码完全可以满足我的需要,但我将其放入我的子主题 functions.php 文件中,但它对我不起作用!
在 WooCommerce 中隐藏价格设置为零的产品
add_action( 'woocommerce_product_query', 'react2wp_hide_products_higher_than_zero' );
function react2wp_hide_products_higher_than_zero( $q ){
$meta_query = $q->get( 'meta_query' );
$meta_query[] = array(
'key' => '_price',
'value' => 0,
'compare' => '>'
);
$q->set( 'meta_query', $meta_query );
}
在 WooCommerce 中隐藏没有价格的产品
add_action( 'woocommerce_product_query', 'react2wp_hide_products_without_price' );
function react2wp_hide_products_without_price( $q ){
$meta_query = $q->get( 'meta_query' );
$meta_query[] = array(
'key' => '_price',
'value' => '',
'compare' => '!='
);
$q->set( 'meta_query', $meta_query );
}
来源 - https://react2wp.com/woocommerce-hide-products-without-price-simple-fix/
我正在使用 Woocommerce 3.4.5 和 Flatsome 主题(更改主题也没有帮助)
如何找出代码不起作用的原因?
感谢任何帮助。
正如疯狂猜测:
将 react2wp_hide_products_without_price()
更改为 'value'=> ''
,更改为 'value'=> 0,
Update - Added an addition to auto move to trash products with zero or empty price
要将存档页面中没有价格或零价格的产品隐藏为商店,请使用以下命令:
add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
function shop_only_instock_products( $meta_query, $query ) {
// In frontend only
if( is_admin() ) return $meta_query;
$meta_query['relation'] = 'OR';
$meta_query[] = array(
'key' => '_price',
'value' => '',
'type' => 'numeric',
'compare' => '!='
);
$meta_query[] = array(
'key' => '_price',
'value' => 0,
'type' => 'numeric',
'compare' => '!='
);
return $meta_query;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
但是使用像您的第一个代码片段这样的东西会得到相同的结果(价格大于零):
add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
function shop_only_instock_products( $meta_query, $query ) {
// In frontend only
if( is_admin() ) return $meta_query;
$meta_query[] = array(
'key' => '_price',
'value' => 0,
'type' => 'numeric',
'compare' => '>'
);
return $meta_query;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
Moving products to trash (without deleting them) that have Zero or empty price.
以下代码会将价格为 0 或 empty 的所有产品的状态更新为 trash
使用非常简单的直接 SQL 查询.
您每次需要 运行 一次即可。
// Function that will add products to trash that have Zero price or no price
function auto_add_product_to_trash(){
global $wpdb;
$number_of_products_processed = $wpdb->query("
UPDATE {$wpdb->prefix}posts as a
JOIN {$wpdb->prefix}postmeta AS b ON a.ID = b.post_id
JOIN {$wpdb->prefix}postmeta AS c ON a.ID = c.post_id
SET a.post_status = 'trash'
WHERE a.post_status LIKE 'publish' AND a.post_type LIKE 'product'
AND b.meta_key LIKE '_price' AND c.meta_key LIKE '_sale_price'
AND ( b.meta_value LIKE '' OR b.meta_value LIKE '0' )
AND c.meta_value != '0'
");
// Return the number of products added to trash
return $number_of_products_processed;
}
// Execute the function - Run it just once and comment it
auto_add_product_to_trash();
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
Now when going to your products in Backend, all products with zero or empty price are in trash. You can review them one by one and delete them easily:
This way everything is secure as you can restore any product added to trash. If you restore a product, you will have to set its status to published (restored products get a "draft" status)
您可以在管理员上进行。按价格排序 - 会将所有标价的商品放在一起,将没有标价的商品放在一起。批量删除您所在的页面,并通过更改屏幕选项 > 每页项目数来操纵您看到的内容