Skip Navigation

[Resolved] Frontend form doesn’t insert attribute values in admin product dashboard

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user was using a form to create products from the frontend, the attributes were not appearing in the products on the backend, and they were appearing in the WCFM dashboard.

Solution:
It turns out that WooCommerce saves the attributes in the taxonomy terms table when a product is created. And If it is a variable product it also saves the attributes in a custom field on the product (_product_attributes). I think that WooCommerce pulls the values of this custom field in the backend and that WCFM pulls the attributes from the taxonomy terms table.

Syncing this will need custom code. Check the example here https://toolset.com/forums/topic/frontend-form-doesnt-insert-attribute-values-in-admin-product-dashboard/#post-1917575 and the additional explanation https://toolset.com/forums/topic/frontend-form-doesnt-insert-attribute-values-in-admin-product-dashboard/#post-1917785

This support ticket is created 3 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 9 replies, has 2 voices.

Last updated by giuseppeU 3 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#1914429

When I use the frontend module to enter products in the shop, some data is not stored, such as product attributes. Why?

#1914861

Hello and thank you for contacting the Toolset support.

Currently, Toolset WooCommerce Forms does not allow the use of attributes as fields in the form. As you can read in this article, only simple and external products can be created using Toolset Forms. https://toolset.com/course-lesson/creating-front-end-forms-for-adding-woocommerce-products/

We used to have an article that lists the possible fields to use, but this article is now under redaction and hopefully will be released soon.

As a workaround, you may want to use generic fields and custom code to add attributes to products. Check the following article:
- hidden link

I hope this helps. Let me know if you have any questions.

#1915083
attribute-list.jpg
vendor-admin.jpg
general-admin.jpg

Okay, but take a look at this problem. I hope that the images I attach can clarify what I have discovered.
When I check the product in the general admin dashboard, the woocommerce one, the attributes are not in, so they have not been stored (general-admin image).
But when I check the product in the Store Vendor dashboard (we use WCFM as multivendor plugin), the attributes are there (vendor-admin image)!
Not only. When I check the list of attributes and the relative values, I realize that the new value inserted through the frontend module exists and is correctly stored (attribute-list image). The new value is KORG.
Can you check it out too, please?
If needed, I can give you access to the site's backend.
Thanks anyway for your kindness and attention.

#1915151

Of course, I'll try, but keep in mind that we do not recommend adding attributes to products from the frontend as we are still supporting only simple and external products that usually do not need attributes.
Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

Please provide as many details as possible:
- URL where we can see the form.
- URL of the Store Vendor dashboard.
- If possible, record a screencast of adding a new product using a Toolset form.
And any information you might think would be interesting.

#1916583

It seems that WooCommerce saves the attributes in the taxonomy terms table when a product is created. And If it is a variable product it also saves the attributes in a custom field on the product (_product_attributes). I think that WooCommerce pulls the values of this custom field in the backend and that WCFM pulls the attributes from the taxonomy terms table.

I tried some tests where I build the value of this custom field using a shortcode. After testing it, the product displays the attributes in the backend. So I tried to run some code when the form is submitted but it did not work. I have also tried when a product is created to no avail. So, I'll need to debug this locally in order to see what's going on, can you allow me to take a copy of your website and work on it locally?

For reference, you can check the shortcode that I was testing in the Toolset Custom code section.

#1917101

Of course, you can! Go ahead. I think that finding a solution to this problem can be useful not only to me but to you as well.

#1917575

I worked on this locally and I was able to find the issue in the code. I added it to your website. Please check with a new product and let me know if that's what you expect.


function my_update_product_attributes_meta( $product_id ) {
  $brands = array_shift( wc_get_product_terms( $product_id, 'pa_marchio', array( 'fields' => 'names' ) ) );
  $regions = array_shift( wc_get_product_terms( $product_id, 'pa_area-geografica', array( 'fields' => 'names' ) ) );
  $terms = array(
    "pa_area-geografica" => $regions,
    "pa_marchio" => $brands
  );
  
  $product_attributes = array();
  
  // Loop through the attributes array
  foreach ($terms as $name => $value) {
    // Relate post to a custom attribute, add term if it does not exist
    // wp_set_object_terms($post_id, $value, $name, true);
    // Create product attributes array
    $product_attributes[] = array(
      'name' => $name, // set attribute name
      'value' => $value, // set attribute value
      'is_visible' => 1,
      'is_variation' => 0,
      'is_taxonomy' => 1
    );
  }
  
  // delete_post_meta( $product_id, '_product_attributes' );
  $meta = update_post_meta( $product_id, '_product_attributes', $product_attributes );
}



add_action( 'cred_save_data', 'my_save_data_action', 500, 2 );
function my_save_data_action($post_id, $form_data) {
  // if a specific form
  if ( $form_data['id'] == 878 ) {
    // my_update_product_attributes_meta( $product_id );
    my_update_product_attributes_meta( $post_id );
  }
}
#1917687

Very good job, Jamal! It works fine!
So, if I have to add a new attribute, for example MODELLO, I should just have to add a new variable like this:
$modello= array_shift( wc_get_product_terms( $product_id, 'pa_modello', array( 'fields' => 'names' ) ) );
to the code script. Correct?

#1917785

You need to add that line, and another line to the $terms array:

  $brands = array_shift( wc_get_product_terms( $product_id, 'pa_marchio', array( 'fields' => 'names' ) ) );
  $regions = array_shift( wc_get_product_terms( $product_id, 'pa_area-geografica', array( 'fields' => 'names' ) ) );
  $modello= array_shift( wc_get_product_terms( $product_id, 'pa_modello', array( 'fields' => 'names' ) ) );
  $terms = array(
    "pa_area-geografica" => $regions,
    "pa_marchio" => $brands,
    "pa_modello" => $modello,
  );
#1917805

My issue is resolved now. Thank you!