我想为所有现有产品和新添加的产品永久启用审核复选框.我查看了WooCommerce设置,但那是不可能的.我在互联网上搜索,但我找不到任何东西.
如何批量修改所有现有产品以启用评论?
当我们添加新产品时,它也应该自动检查.
有办法吗?
可能吗?
提前致谢.
解决方法:
This is possible, but you will need 2 functions. One to update all existing products inn your shop, that you will use only one time, and the other for all newly published products.
第1步 – 在function.php上使用一次,然后转到前端并导航到任何页面.完成后,请注释此代码或将其删除.您所有现有产品都已更新.
// Updating all products that have a 'comment_status' => 'closed' to 'open'
function updating_existing_products_once(){
$args = array(
// WC product post type
'post_type' => 'product',
// all posts
'numberposts' => -1,
'comment_status' => 'closed',
'post_status' => 'publish',
);
$shop_products = get_posts( $args );
foreach( $shop_products as $item){
$product = new WC_Product($item->ID);
wp_update_post( array(
'ID' => $item->ID,
'comment_status' => 'open',
) );
}
}
// After usage comment this line below
updating_existing_products_once();
第2步 – 此功能将更新具有’comment_status’=>的新创建产品’关闭’到’开放'(对WooCommerce的评论)……
add_action('transition_post_status', 'creating_a_new_product', 10, 3);
function creating_a_new_product($new_status, $old_status, $post) {
if( $old_status != 'publish' && $new_status == 'publish' && !empty($post->ID) && in_array( $post->post_type, array( 'product') ) ) {
if ($post->comment_status != 'open' ){
$product = new WC_Product($post->ID);
wp_update_post( array(
'ID' => $post->ID,
'comment_status' => 'open',
) );
}
}
}
此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中.
此代码经过测试和运行.