Here is what you can do. Add the below code to theme functions.php file. The code will add the custom title option under Product data -> General options. It will display the default title if custom title is not added in admin.
Adding a custom field in the back-end
// Display the custom text field
function mos_create_custom_field() {
$args = array(
'id' => 'shop_page_field_title',
'label' => __( 'Shop Page product title' ),
'class' => 'mos-custom-field',
'desc_tip' => true,
'description' => __( 'Shop Page product title.', 'ctwc' ),
);
woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_general_product_data', 'mos_create_custom_field' );
add_action( 'woocommerce_product_options_inventory_product_data', 'mos_create_custom_field' );
Saving the custom field value
// Save the custom field
function mos_save_custom_field( $post_id ) {
$product = wc_get_product( $post_id );
$title = isset( $_POST['shop_page_field_title'] ) ? $_POST['shop_page_field_title'] : '';
$product->update_meta_data( 'shop_page_field_title', sanitize_text_field( $title ) );
$product->save();
}
add_action( 'woocommerce_process_product_meta', 'mos_save_custom_field' );
Displaying on Shop page
// remove the default action for product title on shop page
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title' );
// add the custom action to show the custom product title
add_action( 'woocommerce_shop_loop_item_title', 'custom_woocommerce_template_loop_product_title', 99 );
function custom_woocommerce_template_loop_product_title() {
global $post;
$product = wc_get_product( $post->ID );
$title = $product->get_meta( 'shop_page_field_title' );
if( $title ) {
// Only display our field if we've got a value for the field title
echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $title ) . '</h2>';
} else {
// else display the defaul title
echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $product->get_title() ) . '</h2>';
}
}
Code is tested and working fine. Hope it helps
5
solved Use Different Product Title For Shop/Category Pages