I'm trying to add products using data gathered from a Caldera form. I'm successful in creating the product and adding other attributes, but 'reviews_allowed' is proving to be elusive.

$post_id = wp_insert_post(array(
    'post_title' => 'ProductName '.$data['uniquenumber'],
    'post_type' => 'product',
    'post_status' => 'draft',
    'post_content' => $data[ 'description' ],
    'post_excerpt' => $data[ 'short_description' ]

wp_set_object_terms( $post_id, 'simple,virtual', 'product_type' );
wp_set_object_terms( $post_id, ['services'] ,'product_cat');

wp_set_object_terms( $post_id, 1 ,'reviews_allowed' );  // DOESN'T WORK

//update_post_meta( $post_id, 'reviews_allowed', 'yes');  //CREATES SEPARATE ATTRIBUTE,

// All these work though
update_post_meta( $post_id, 'real_name', $data['first_name']." ".$data['last_name'] );
update_post_meta( $post_id, '_nyp', 'yes' );
update_post_meta( $post_id, '_virtual', 'yes' );
update_post_meta( $post_id, '_visibility', 'visible' );

I've searched throught the WC code and I'm pretty sure the attribute is 'reviews_allowed' and the type is bool. I'm new to this, so I hope/expect this is an easy problem.

Thanks, John


as far as I know reviews_allowed in woocommerce is not an object term, attribute, part of the post meta or anything like that.

It's just simply another name for comment_status in the post itself.

You can enable reviews when inserting a post using the following

$post_id = wp_insert_post(array(
    . . . . 
    'comment_status' => 'open'
| improve this answer | |

You shouldn't use WordPress post functions when working with WooCommerce products. WooCommerce has its own functions that allow products to properly populate its custom lookup tables and will be forward compatible if (when, realistically) WooCommerce products are moved to a custom data structure in a future version.

You can see some examples here, but your code would look like this:

$product = new WC_Product_Simple();
$cat_id  = get_term_by( 'slug', 'services', 'product_cat' );

$product->set_category_ids( [ $cat_id ] );
$product->set_name( 'ProductName ' . $data['uniquenumber'] );
$product->set_status( 'draft' );
$product->set_description( $data[ 'description' ] );
$product->set_short_description( $data[ 'short_description' ] );
$product->set_virtual( true );
$product->set_reviews_allowed( true );
$product->set_catalog_visibility( 'visible' );
$product->update_meta_data( 'real_name' );
$product->update_meta_data( '_nyp' );


With this method you set the reviews allowed property with $product->set_reviews_allowed( true );.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.