我想为所有现有产品和新添加的产品永久启用审查复选框。我在WooCommerce的设置,但thatís不可能。我在互联网上搜索,我didnít找到了任何东西。
如何批量编辑所有现有产品以获得评论Enabled?
当我们添加一个新产品时,它也应该被自动检查。
有办法吗?
这可能吗?
这是可能的,但您将需要两个函数。一个用于更新商店中所有现有的产品,您将只使用一次,另一个用于更新所有新发布的产品。
第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' => 'closed'到'open'的新创建产品(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文件或任何插件文件中。
除了LoicTheAztec的出色答案之外,我想为"步骤1"添加一个不同的选项。
你可以只运行一个不需要循环迭代的简单查询:
global $wpdb;
$wpdb->query("UPDATE {$wpdb->posts} SET comment_status = 'open' WHERE post_type = 'product'");
注意故意省略WHERE
子句中的comment_status
和post_status
。未发布的产品是否有open comment状态无关紧要,已经将comment_status设置为open的产品是否被重新设置为open也无关紧要。
只需将上述代码添加到主题的functions.php
文件的底部,然后在运行一次后将它们注释掉:
// Commented out so it won't run
// global $wpdb;
// $wpdb->query("UPDATE {$wpdb->posts} SET comment_status = 'open' WHERE post_type = 'product'");
我知道这是一个老问题,但下面的功能可以帮助某人,如果LoicTheAztec的答案不起作用(就像我一样)。它将默认使用jQuery在"添加产品"页面上检查评论复选框。希望对大家有所帮助,干杯!: -)
add_action( 'woocommerce_product_options_advanced', 'enable_reviews_by_default' );
function enable_reviews_by_default() {
?>
<script>
(function($){
$('input[name=comment_status]').prop('checked', true);
})(jQuery);
</script>
<?php
}
顺便说一下,这是一种立即修复问题的肮脏方法(可逆转)。所有功劳都归Evgeniy所有
https://developer.wordpress.org/reference/functions/comments_open/虽然原来的线程讨论如何禁用注释无处不在,我颠倒效果:)
这使您无法在每个产品的基础上决定,因为覆盖了站点范围内的复选框!
免责声明:风险自负!
add_filter("comments_open"、"__return_true");