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.
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
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.
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.
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);
}