How to target a MeTABOX custom field attached to a Woo product
- This topic has 9 replies, 2 voices, and was last updated 2 years, 9 months ago by
Long Nguyen.
-
AuthorPosts
-
July 30, 2022 at 2:18 AM #37277
Guth-Rosenstock-Art
ParticipantHi 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?July 30, 2022 at 2:59 PM #37283Long Nguyen
ModeratorHi,
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/
July 31, 2022 at 2:12 PM #37287Guth-Rosenstock-Art
ParticipantDear 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; } ?>
July 31, 2022 at 2:42 PM #37288Guth-Rosenstock-Art
ParticipantMaybe 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; } ?>
August 2, 2022 at 10:21 AM #37309Long Nguyen
ModeratorHi,
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.August 3, 2022 at 3:35 AM #37320Guth-Rosenstock-Art
ParticipantDear Mr. Nguyen,
I tried both my solutions but it didn't work. Could you post your solution to the forum? Thanks!August 3, 2022 at 3:41 AM #37321Guth-Rosenstock-Art
ParticipantI 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.August 3, 2022 at 12:52 PM #37330Long Nguyen
ModeratorHi,
The code works as well on my demo site, here is the screen record https://imgur.com/bGRYRQi
August 4, 2022 at 5:55 PM #37345Guth-Rosenstock-Art
ParticipantFor 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?
August 5, 2022 at 10:39 AM #37367Long Nguyen
ModeratorHi,
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.
-
AuthorPosts
- You must be logged in to reply to this topic.