If this solution does not resolve your issue or seems outdated, please check the related known issues and verify that you have installed the latest versions of themes and plugins. If the problem persists, we highly recommend opening a new support ticket at WPML support forum.
This is the technical support forum for WPML - the multilingual WordPress plugin.
Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.
Background of the issue:
I am working on a site under development and trying to add an extra field using wcml_custom_prices_fields. The field appears properly, but after saving, its value does not appear. I am following the documentation at https://wpml.org/wcml-hook/wcml_custom_prices_fields/.
Symptoms:
The extra field added using wcml_custom_prices_fields appears properly but does not retain its value after saving.
Questions:
Why does the value of the extra field not appear after saving?
I'm using the wcml_custom_prices_fields hook to create a custom field and try to store data. The field appears in the product, but after saving, the data is not storing.
I found more issue in the variable product. its only showing the currency symbol not showing the field label. Also its not saving the value just like the previous response.
Screenshot - hidden link
You can check below video for more understanding.
- hidden link
For code, I shared with you, as I have this is example code you need to adjust it to your custom field names.
So, to make it more understandable, use hook wcml_custom_prices_fields to create custom fields, then use wcml_update_custom_prices_values to update these fields.
Note I am on vacation today, so I might not reply until tomorrow. You can also open new chat and ask my colleague to help out in the meantime.
I am trying to save custom field data for a WooCommerce variable product using the WPML plugin. I have added custom price fields using the following code:
add_filter( 'wcml_custom_prices_fields', 'add_custom_price_fields', 10, 2 );
function add_custom_price_fields( $fields, $product_id ) {
// Add a new custom price field
$fields[] = '_purchase_cost';
return $fields;
}
add_filter( 'wcml_custom_prices_fields_labels', 'set_labels_for_price_fields', 10, 2 );
function set_labels_for_price_fields( $labels, $product_id ){
// Edit the label of a custom price field
$labels[ '_purchase_cost' ] = __( 'Purchase cost', 'woocommerce-multilingual' );
return $labels;
}
add_filter( 'wcml_update_custom_prices_values', 'add_purchase_cost_to_custom_prices', 10, 3 );
function add_purchase_cost_to_custom_prices( $custom_prices, $code, $post_id ) {
$purchase_cost = get_post_meta( $post_id, '_purchase_cost'.'_' . $code, true );
if (isset($_POST["_custom_purchase_cost"][$code])){
$purchase_cost = $_POST["_custom_purchase_cost"][$code];
}
$custom_prices['_purchase_cost'] = $purchase_cost;
return $custom_prices;
}
Screenshot: hidden link
Symptoms:
After saving the variable custom field data, the product page reloads, and I can't see anything. When I comment out the code, things work fine, but I can't see the custom field.
Questions:
Why does the product page reload and not display anything after saving the variable custom field data?
How can I save custom field data for a WooCommerce variable product without causing the page to reload incorrectly?