When I use the frontend module to enter products in the shop, some data is not stored, such as product attributes. Why?
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.
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.
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.
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.
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.
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 );
}
}
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?
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,
);
My issue is resolved now. Thank you!