Support Forum
Hi Metabox Users! I am an Oxygen user and am creating a woocommerce restaurant menu. I am using a script to convert the | pipe symbols into <br/> tags so I can force a long entree in he Product title to break into 2 lines. So far so good. The problem is in the backend, because my client has three menus for different times of the year with many of the same entree names( this becomes entree titles on the menus) but with different ingredients and prices, there are three identical product names for each entree. No problem since each have different slugs and taxonomies and setting so they will only show up on their respective meus. Now that I am in fine tuning mode I realized that in the backend if the client wants to add another entree at checkout he will see three of the same entrees based on the product name--confusion!
My solution was to add a metabox custom field to the products called Entree display name. This way I could rename all the products with unique names so the client could easily choose entree-name-menu1 or entree-name-menu2. Everything works ,thank Heaven! But now my original script to convert long titles into two lines using the pipe symbol is not working. Here is the code:
/*ADD LINE BREAK */
add_filter( 'the_title', 'custom_product_title', 10, 2 );
function custom_product_title( $title, $post_id ){
$post_type = get_post_field( 'post_type', $post_id, true );
if( in_array( $post_type, array( 'product', 'product_variation') ) || in_array() ) {
$title = str_replace( '|', '<br/>', $title ); // we replace "|" by "<br>"
}
return $title;
}
in line 4 -- if( in_array( $post_type, array( 'product', 'product_variation') ) ) {
$title = str_replace( '|', '<br/>', $title ); // we replace "|" by "<br>"
}
how do I target the custom METABOX field display_entree_name which I attached to the Product post type so all the Metabox display entree data will appear correctly formatted on the front end?
Hi,
You can use the helper function to get the field value associated with the post ID in the callback function, or just use the WordPress function get_post_meta()
. For example:
rwmb_meta( 'display_entree_name', '', $post_id )
Read more on the documentation https://docs.metabox.io/functions/rwmb-meta/
Dear Mr. Nguyen,
I am just a rank beginner. I also am in awe of your talent! would this work?
<?php
/*ADD LINE BREAK */
add_filter( 'the_title', 'custom_product_title', 10, 2 );
function custom_product_title( $title, $post_id ){
$post_type = get_post_field( 'post_type', $post_id, true );
if( in_array( $post_type, array( 'product', 'product_variation') ) ) {
$metabox_title = rwmb_meta( 'display_entree_name', '', $post_id );
$title = str_replace( '|', '<br/>', $metabox_title ); // we replace "|" by "<br>"
}
return $title;
}
?>
Maybe this instead?
<?php
/*ADD LINE BREAK */
add_filter( 'the_title', 'custom_product_title', 10, 2 );
function custom_product_title( $title, $post_id ){
$post_type = get_post_field( 'post_type', $post_id, true );
$metabox_title = rwmb_meta( 'display_entree_name', '', $post_id );
if( in_array( $post_type, array( 'product', 'product_variation', $metabox_title) ) ) {
$title = str_replace( '|', '<br/>', $metabox_title ); // we replace "|" by "<br>"
}
return $title;
}
?>
Hi,
Both block of code should work in your case, but I prefer this code.
We should check the post type first, if it does not match then do nothing. No need to get the field value for other post types.
Dear Mr. Nguyen,
I tried both my solutions but it didn't work. Could you post your solution to the forum? Thanks!
I click on the link to see your solution but it just opens the same page as before with my nonworking versions.
I really need your help since I am going live next week.
Hi,
The code works as well on my demo site, here is the screen record https://imgur.com/bGRYRQi
For some reason mine doesn't . I am using ScriptOrganizer from DPlugins and Oxygen. While the script works for woo product names when I try the same script (altered as you suggested) it doesn't work. Should I make the script active on everywhere or hide it in the builder?
Hi,
You can try to add the custom code to the file functions.php in a theme folder after deactivating Oxygen and other plugins then see if it works.