Support

Account

Home Forums Front-end Issues Add custom fields to WooCommerce Reviews

Solving

Add custom fields to WooCommerce Reviews

  • Hi,

    I am already using a Plugin to enhance Woocommerce Reviews and now I am trying to add some more fields to it using ACF.

    I was able to add the wanted fields to the review form in the backend and to display them in the final review in the frontend.

    What I still fail at is to make the fields available for all Users in the frontend form. So only the Admin is able to use them at the moment.

    This is how far I got. (I am completely new to the following, there might be a lot of mistakes in this code)

    Using this code I am able to show the field group I created to frontend Users:

    <?php acf_form_head(); ?>
    <?php
    acf_form(array(
    ‘post_id’ => get_the_ID(),
    ‘field_groups’ => array(21112),
    ‘post_title’ => false,
    ‘post_content’ => false,
    ‘submit_value’ => __(‘Update meta’)
    ));
    ?>

    The Problems:
    – this creates a new submit button “update meta” (I can only rename it, not disable it)
    – the original Submit button of the Review form stops working
    – when I use the new “Update meta” button nothing gets saved

    I tried to solve the last issue by adding this to functions.php.
    (w2rr_review is the Post type the Plugin creates)

    function new_review_meta( $post_id )
    {
    if( $post_id == ‘new_post’ ) {
    // Create a new post
    $post = array(
    ‘post_type’ => ‘w2rr_review’,
    ‘post_status’ => ‘publish’
    );

    // insert the post
    $post_id = wp_insert_post( $post );

    return $post_id;
    }
    else {
    return $post_id;
    }
    }

    add_filter(‘acf/save_post’ , ‘new_review_meta’);

    Any tipps on how to solve this are very much appreciated. Thanks for any reply!

  • You’re adding another form to the page, hence why you’re having issues. You just need to amend the existing woo review form

    If you want to add the fields to the existing Woo Reviews form, when you create the ACF fields , under Rules set the field group to Comments (under the form heading) is equal to then select Product.

    Testing this on a fresh install of WP, Woo and the 21 theme, the new fields show on the product review forms.

  • Hi jarvis,

    thanks a lot for your input! You are right for vanilla Woocommerce Reviews. I tried it myself and it works. The problem is, I am already using a plugin that enhances the Woocommerce Reviews and I try to extend this plugin with those custom fields. The method you explain doesn’t work with this plugin. Only in the backend but not in the frontend form.

    I worked on this issue and managed to get a bit further.
    (with help of this artivle https://medium.com/techcompose/how-to-add-advanced-custom-fields-acf-to-a-frontend-form-2b89c8cfdcee)

    <?php
    $new_post= array(
    ‘post_id’ => ‘new_post’,
    ‘field_groups’ => array(21112),
    ‘post_title’ => false,
    ‘post_content’ => false,
    ‘form’ => false
    );
    acf_form( $new_post);
    ?>

    This way the I do not get an additional submit button and the original submit button still works. I can submit reviews this way. The only problem remaining. Only the original Inputs get saved. The Inputs of the new custom fields do not.

    I assume I am still doing something wrong in the code that goes into functions.php

    add_filter(‘acf/pre_save_post’, ‘save_post_from_frontend’);
    function save_post_from_frontend( $post_id) {

    //Check if user loggin or can publish post
    if( ! ( is_user_logged_in() || current_user_can(‘publish_posts’) ) ) {
    return;
    }

    // check if this is to be a new post
    if( $post_id!= ‘new_post’) {
    return $post_id;
    }

    $post= array(
    ‘post_type’ => ‘w2rr_review’, // Your post type ( post, page, custom post type )
    ‘post_status’ => ‘publish’, // (publish, draft, private, etc.)
    );
    // insert the post
    $post_id= wp_insert_post( $post);
    // Save the fields to the post
    do_action( ‘acf/save_post’, $post_id);
    return $post_id;

    }

  • The new ACF fields won’t save because you’re not triggering the additional form with the ACF fields.

    From what I can see you have 2 options:
    1) I’m not sure what the enhanced review plugin does, but maybe you can remove it and look to add the functionality another way, therefore, allowing you to add the ACF fields as mentioned above OR
    2) You see if the enhanced review plugin has a hook/action on save, therefore, once the review form is triggered, you run a function to get the field data from the ACF fields and use update_field() to insert the data

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.