我想为所有现有产品和新添加的产品永久启用审核复选框。我查看了 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 文件中或任何插件文件中。
此代码已经过测试并且可以工作。
关于php - 为所有 WooCommerce 产品启用永久审查复选框选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39308528/