Skip Navigation

[Resolved] Display custom field on Woocommerce cart and checkout page

This support ticket is created 2 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 8 replies, has 2 voices.

Last updated by winyS 2 years, 11 months ago.

Assisted by: Shane.

Author
Posts
#2272365
Schermafbeelding 2022-01-21 om 15.08.47.png
Schermafbeelding 2022-01-21 om 15.08.17.png

I have created a custom field in Toolset for the date of a workshop (= Woocommerce product).
I can display this field in the Shop archive and on the single product page.
I want to display it also on the cart and checkout page, to confirm for the client that the date of the workshop he will order is right.

I have followed this tutorial: https://stackoverflow.com/questions/55555783/display-a-product-custom-field-value-in-woocommerce-cart-and-checkout-table
and added the following code to my functions.php:
/* Display Toolset Datum workshop field
https://stackoverflow.com/questions/55555783/display-a-product-custom-field-value-in-woocommerce-cart-and-checkout-table
*/

// Display in cart and checkout pages
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // Get the WC_Product Object

if ( $value = $product->get_meta('wpcf-datum-workshop') ) {
$product_name .= '<br><small>'.$value.'</small>';
}
return $product_name;
}

// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data','rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_item_data, $cart_item ) {
if( isset( $cart_item['wpcf-datum-workshop'] ) ) {
$cart_item_data[] = array( "name" => __( "Datum workshop", "woocommerce" ), "value" => $cart_item['wpcf-datum-workshop'] );
}
return $cart_item_data;
}

Now the date field appears ion the cart page but not with the right value (should be a date). It shows some random number, like "1642723200".

The link to my site is hidden link. It is a development site and you will need a login to view it. If necessary I can give you admin access.

#2272499

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Winy,

Thank you for getting in touch.

Our Toolset date field is stored as a Timestamp and as such it will need to be converted to a human readable date using the date() function from PHP. I've modified your code below to convert this to a human readable date.

// Display in cart and checkout pages
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // Get the WC_Product Object

if ( $value = $product->get_meta('wpcf-datum-workshop') ) {
$product_name .= '<br><small>'.$value.'</small>';
}
return $product_name;
}

// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data','rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_item_data, $cart_item ) {
if( isset( $cart_item['wpcf-datum-workshop'] ) ) {
$cart_item_data[] = array( "name" => __( "Datum workshop", "woocommerce" ), "value" => $cart_item['wpcf-datum-workshop'] );
}
return date("F j, Y",$cart_item_data);
}


For more information on the date function you can have a look at the link below.
hidden link

Thanks,
Shane

#2274045

Thank you, Shane,
I applied your code in my functions.php. However, the date is not displayed. I still see the random number instead.
Even in incognito window, after reloading, clearing cookies and history.
If necessary I can give you admin access.

#2274143

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Winy,

That's quite odd, it would mean that the code hasn't been updated if its still displaying the date as a timestamp.

I've enabled the private fields for your next response. Please let me know where you've added the custom code for the checkout page.

Thanks,
Shane

#2275223

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Winy,

Thank you for the credentials.

However can you recall anywhere else where you may have a copy of this code? I removed it from the functions.php file yet still the value remains on the frontend.

Ideally once the function is removed then the value should also be removed as well. Perhaps this function is being called elsewhere on the site.

Thanks,
Shane

#2275243

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Winy,

I managed to get it to work now.

After inspecting the code a bit further I was able to find the exact section where you were rendering the product name. It turns out that the code was concatenating the date timestamp onto the product name rather than displaying it separately.

So I had to just move the date function to where the string was being called .

// Display in cart and checkout pages
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // Get the WC_Product Object
 
if ( $value = $product->get_meta('wpcf-datum-workshop') ) {
$product_name .= '<br><small>'.date("j F Y",$value).'</small>';
}
return $product_name;
}
// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data','rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_item_data, $cart_item ) {
if( isset( $cart_item['wpcf-datum-workshop'] ) ) {
$cart_item_data[] = array( "name" => __( "Datum workshop", "woocommerce" ), "value" => $cart_item['wpcf-datum-workshop'] );
}
return date("j F Y",$cart_item_data);
}

Please let me know if everything is ok now.

Thanks,
Shane

#2275269

Hi Shane,
That's great, thank you!
One little thing: The date is displayed in English. How can I have it display in my language (Dutch; NL)?

#2275303

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Winy,

I've update the code to set the language locale.

The date should now be in Dutch.

Thanks,
Shane

#2275359

My issue is resolved now. Thank you!