Skip Navigation

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 4 weeks, 1 day ago.

Assisted by: Dražen.

Author Posts
May 1, 2025 at 8:54 am #16985977

marioG-45

Hi Dražen,

Okay Waiting for the solution.
Thanks

May 5, 2025 at 4:33 am #16994196

marioG-45

Hi Dražen,

Any update regarding the variable product?
Thanks

May 5, 2025 at 5:53 am #16994313

Dražen
Supporter

Languages: English (English )

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

Hello,

There were holidays and weekend in Europe, so most of the team was OFF, so it is taking a while.

I will update you when I have some news, should be today or tomorrow.

Regards,
Drazen

May 7, 2025 at 5:44 am #17004228

marioG-45

Hi Dražen,
I hope you are doing well.
Okay I'm waiting for the response. I hope I'll get the solution asap.
Thanks

May 7, 2025 at 12:49 pm #17006646

Dražen
Supporter

Languages: English (English )

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

Hello,

For the label, you should additional hook that reuses your code for the label:

add_filter( 'wcml_custom_prices_strings', 'set_labels_for_price_fields', 10, 2 );

and for saving value, the data for the variation is saved in a separate value - _POST["_custom_variation_purchase_cost"][$code][$post_id].

So finally working code on test site looks like this:

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_variation_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;
}

Regards,
Drazen

May 7, 2025 at 1:04 pm #17006732

marioG-45

Hi Dražen,

Thanks for the update.
I'll test above code tomorrow and let you know.
Thanks

May 7, 2025 at 1:11 pm #17006755

Dražen
Supporter

Languages: English (English )

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

Hello,

sure.

You are free to see the test site, where it works fine:

- hidden link

Regards,
Drazen

May 8, 2025 at 5:53 am #17009804

marioG-45

Hi Dražen,

I have noticed that on the test site you installed the below plugins as well, which I think its necessary.

Plugins:-
1. WooCommerce Multilingual & Multicurrency
2. WPML Multilingual CMS
3. WPML String Translation

On my staging site I have just installed WooCommerce Multilingual & Multicurrency and
now I have installed rest of the two plugin as well.

But for now I just want to work on English language I don't want to add any other languages on the site. So if those two plugin is necessary in that case I want to add any other language on my site which I don't want to add.

So now what happen is if I can't add any other language on site my setup will stay remaining and i can't see that purchase cost field in product.

See the screenshot:-
1. hidden link
2. hidden link

So could you please check it and let me know on urgent basis.
If you are available for call then we will meet on google meet and discuss.
Thanks

May 8, 2025 at 6:29 am #17009887

Dražen
Supporter

Languages: English (English )

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

Hello,

We do not provide support over the phone or similar, it is only via forum/chats.

As for other plugins, you do not need them if you are using only WooCommerce Multilingual & Multicurrency.

For custom code, you will probably need to further adjust it to your custom environment / approach, but as you can see in test site it works fine. You can use that working code as example, but if you need further adjustments to your website / code I am afraid we can not help with custom coding and custom debugging, it is out of scope of our support.

For custom coding you can use service of WPML contractors:
- https://wpml.org/contractors/

Hope this makes sense.

Regards,
Drazen

May 8, 2025 at 6:34 am #17009904

marioG-45

Hello,

Yes but in your testing site you added the two languages.
Could you please create one more testing site and add only multi currency plugin and test it?

So I can show you what exactly happing.
Thanks

May 8, 2025 at 6:37 am #17009905

Dražen
Supporter

Languages: English (English )

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

Hello,

I have already before my reply, disabled plugins and confirmed it works fine without extra plugins and with only WooCommerce Multilingual & Multicurrency.

You are free to check.

Regards,
Drazen

May 8, 2025 at 7:26 am #17010036

marioG-45

Hello

Okay let me check and get back to you.
Thanks

May 8, 2025 at 7:29 am #17010037

Dražen
Supporter

Languages: English (English )

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

Hello,

Sure, but as said before, maybe you have not understood my reply, it is already like that on the test website, and it works fine:

hidden link

You are free to check it out.

Regards,
Drazen

May 8, 2025 at 7:31 am #17010061

marioG-45

Hello,

Okay sure let me check and get back to you.
Thanks

May 8, 2025 at 8:58 am #17010366

marioG-45

Hello,

I have just review the testing site which you provided. In that site I have noticed that for default currency the purchase cost field is not coming why?
Screenshot:- hidden link

Could you please check it and let me know.
Thanks