'Gravity Forms: Add WooCommerce product attributes with the Advanced Post Creation Add-On

I want to let users add products with the help of a form. To achieve that, I'm using Gravity Forms in combination with the Advanced Post Creation Add-On.

Everything works fine so far. I'm able to add the basic fields like title, price, categories, images and so on.

The only thing I'm not able to solve are the product attributes. The plugin gives me the opportunity to match the product attributes with a form field. See the image:

enter image description here

That option kind of works. It adds the attribute to the product itself. I could see it if I filter the products by the attribute in the backend.

But the attribute doesn't appear in the product data area of the product. See the following image:

enter image description here

I guess I also need to add the attribute to the product meta in any way?!

There is a code example in the docs (https://docs.gravityforms.com/advanced-post-creation-add-on-using-third-party-post-types/#handling-fields-unable-to-be-mapped-2) which I already use to map the price and categories:

add_action( 'gform_advancedpostcreation_post_after_creation', 'update_product_information', 10, 4 );
function update_product_information( $post_id, $feed, $entry, $form ){

    //update the prices set for the product
    update_post_meta( $post_id, '_regular_price', $entry['7'] );
    update_post_meta( $post_id, '_price', $entry['7'] );

    //update the category
    wp_set_object_terms( $post_id, $entry['6'], 'product_cat' );

}

I also extended it with the following code to map the short description:

//update the excerpt
$the_post = array(
    'ID'           => $post_id,//the ID of the Post
    'post_excerpt' => $entry['60'],
);
wp_update_post( $the_post );

Is there any way to add a similar code to add the attributes to the post meta?



Solution 1:[1]

Please try this

add_action( 'gform_advancedpostcreation_post_after_creation', 'update_product_information', 10, 4 );

function update_product_information( $post_id, $feed, $entry, $form ){

      //update the prices set for the product
      update_post_meta( $post_id, '_regular_price', $entry['13'] );
      update_post_meta( $post_id, '_price', $entry['13'] );

      // here 5 is attribute id

      wp_set_object_terms(5, $entry['17'], 'pa_age-range' , false);
      $thedata = array('pa_age-range' => array(
      'name' => 'pa_age-range',
      'value' => $entry['17'],
      'is_visible' => '1',
      'is_taxonomy' => '1'
      ));

      update_post_meta($post_id, '_product_attributes', $thedata);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1