Support

Account

Home Forums Backend Issues (wp-admin) Add ACF to existing Woocommerce metabox

Solving

Add ACF to existing Woocommerce metabox

  • Has anyone figured out how to add a ACF fields into the existing “Product data” back-end metabox which displays Woocommerce’s fields in a tabbed interface. I don’t like having 2 different boxes in the back-end, this is weird for editors.

    I know you can add fields manually using Woo hooks/php, but I’d rather manage them through ACF.

  • You can manage this by using acf_form() in the admin and setting the “form” argument to false. This will let you insert acf fields into a location in the existing form using the hookes provided by WC.

    I do not think you need to use acf_form_head() when adding fields this way in the admin.

  • Oh interesting, let me give that a shot. Will this take care of saving the values too when the page is submitted?

  • Gave it a shot. With the code below custom field data is not saved from the embedded field group. I’m also noticing ACF fields in other groups (added to the post type the normal way) is also not saved. When the Woocommerce code is removed other field group does save.

        add_action( 'woocommerce_product_options_general_product_data', function() {
            echo '<div class="options_group">';        
            acf_form([
                'field_groups' => [233],
                'form' => false,
            ]); 
            echo '</div>';
        });
  • Are the same field in both what you added above and the other location? You need to remove any fields you are adding into the WC form all other locations.

  • No, both traditional ACF field group and ACF group added into Woocommerce do not contain the same field(s). I’ve found that you need to manually save the ACF fields within woocommerce_process_product_meta. Feels a little dirty, but it works.

        add_action( 'woocommerce_product_options_general_product_data', function() {
            acf_form([
                'post_id' => get_the_ID(),
                'field_groups' => [233],
                'form' => false,
            ]); 
        });
    
        add_action( 'woocommerce_process_product_meta', function($post_id) {
            if (!empty($_POST['acf'])) {
                foreach($_POST['acf'] as $key => $value) {
                    update_field($key, $value, $post_id);
                }
            }
        });
  • From a rendering perspective, the field group within Woocommerce looks pretty bad but can probably be fixed up with custom CSS. I’m kind of surprised there isn’t already more support for using both of these 2 pretty popular plugins together.

  • ACF was never meant to be included in the admin inside of other forms. The entire thing is a work-a-round (hack) to make it work.

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

You must be logged in to reply to this topic.