Skip to content Skip to sidebar

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

Problem:
You are 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.
Solution:
We recommend adjusting your code as follows:

add_filter( 'wcml_custom_prices_fields', 'add_custom_price_fields', 10, 2 );<br />function add_custom_price_fields( $fields, $product_id ) {<br />// Add a new custom price field<br />$fields[] = '_purchase_cost';<br />return $fields;<br />}<br /><br />add_filter( 'wcml_custom_prices_strings', 'set_labels_for_price_fields', 10, 2 );<br /><br />add_filter( 'wcml_custom_prices_fields_labels', 'set_labels_for_price_fields', 10, 2 );<br />function set_labels_for_price_fields( $labels, $product_id ){<br />// Edit the label of a custom price field<br />$labels[ '_purchase_cost' ] = __( 'Purchase cost', 'woocommerce-multilingual' );<br />return $labels;<br />}<br /><br />add_filter( 'wcml_update_custom_prices_values', 'add_purchase_cost_to_custom_prices', 10, 3 );<br />function add_purchase_cost_to_custom_prices( $custom_prices, $code, $post_id ) {<br />    $type = get_post_type($post_id);<br />    if ( 'product' == $type ) {<br />        $purchase_cost = get_post_meta($post_id, '_purchase_cost' . '_' . $code, true);<br />               if (isset($_POST["_custom_purchase_cost"][$code])) {<br />            $purchase_cost = $_POST["_custom_purchase_cost"][$code];<br />        }<br />    } elseif ( 'product_variation' == $type ) {<br />        $purchase_cost = get_post_meta($post_id, '_purchase_cost' . '_' . $code, true);<br />        if (isset($_POST["_custom_variation_purchase_cost"][$code])) {<br />            $purchase_cost = $_POST["_custom_variation_purchase_cost"][$code][$post_id];<br />        }<br />    }<br />$custom_prices['_purchase_cost'] = $purchase_cost;<br />return $custom_prices;<br />}

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.

Tagged: 

This topic contains 22 replies, has 0 voices.

Last updated by marioG-45 2 months, 2 weeks ago.

Assisted by: Dražen.

Author Posts
May 8, 2025 at 9:16 am #17010488

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hello,

That is expected, since from WPML and our hooks we/you are adding a custom field to the 2nd currency/language.

"These fields are displayed in the Set prices in other currencies manually section when editing/creating original products."

For default one it is part of Wocommerce , you can use regular approach as you would use if not any WPML plugins are there.

It is not something related to WPML or WPML affects.

Regards,
Drazen

May 9, 2025 at 5:27 am #17014438

marioG-45

Hi,

Okay.
Today I have just created one simple product to test its working fine or not and you can see that in your testing site. After saving the Purchase cost (€) its not showing the saved price.
Screenshot:- hidden link
So it means - Now the simple product is not working.

Could you please check it and let me know.
Thanks

May 9, 2025 at 5:33 am #17014486

marioG-45

Hi,

Could you please do me a fever give one single proper working code where all type of product will save the proper custom fields data like simple, variable, composite, group each and every types of product.

Thanks

May 9, 2025 at 5:57 am #17014562

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hello,

let me re-check with our 2nd tier and get back to you.

Regards,
Drazen

May 9, 2025 at 6:32 am #17014611

marioG-45

Hello,

Okay. Please ensure that all cases are thoroughly reviewed and address the issue conclusively. This issue has been ongoing for the past two weeks, so let's resolve it and move forward.

Thanks for help.

May 9, 2025 at 10:19 am #17015888

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hello,

It was my mistake when adjusting the code, now it works fine. Correct should code be:

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_strings', 'set_labels_for_price_fields', 10, 2 );

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 ) {
    $type = get_post_type($post_id);
    if ( 'product' == $type ) {
        $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];
        }
    } elseif ( 'product_variation' == $type ) {
        $purchase_cost = get_post_meta($post_id, '_purchase_cost' . '_' . $code, true);
        if (isset($_POST["_custom_variation_purchase_cost"][$code])) {
            $purchase_cost = $_POST["_custom_variation_purchase_cost"][$code][$post_id];
        }
    }
$custom_prices['_purchase_cost'] = $purchase_cost;
return $custom_prices;
}

Please check.

Regards,
Drazen

May 9, 2025 at 10:31 am #17015941

marioG-45

Hello Drazen,

Okay Let me check and get back to you.
Thanks

May 9, 2025 at 10:56 am #17016052

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hello,

Sure, take your time.

Regards,
Drazen

May 13, 2025 at 5:26 am #17025062

marioG-45

Hello Drazen,

It's looks like working fine. So I'm closing this ticket.
If I need any help then let you know.

Thanks for your help.