Wordpress WooCommerce store - Subtitle under Products Titles

Prima picture Prima · Aug 18, 2015 · Viewed 8.1k times · Source

I've been reading WP forums and trying different plugins for over a week now with no luck, so I decided to give it a try here.

I'm creating a WP website with a premium theme, that supports a woocommerce. What I need to do is the following:

  • Create a subtitle area (which would be named REG. NO:), so I could not only write Title of the product, but subtitle also. So when you would open a single product page, it would be like:

This_is_my_product_title REG.NO: this_is_my_reg_no

  • another issue is, I would need a REG.NO: (subtitle) to be an external hyperlink to a different website.

Greatest thanks to anyone who could help me out.

Answer

Anand Shah picture Anand Shah · Aug 18, 2015

If you want to go pure WooCommerce way, here's the gist.

1 - Add custom field ( this code goes in functions.php )

add_action( 'woocommerce_product_options_general_product_data', 'my_custom_field' );

function my_custom_field() {

woocommerce_wp_text_input( 
    array( 
        'id'          => '_subtitle', 
        'label'       => __( 'Subtitle', 'woocommerce' ), 
        'placeholder' => 'Subtitle....',
        'description' => __( 'Enter the subtitle.', 'woocommerce' ) 
    )
);

}

The field will appear as shown in this screen grab : http://i.imgur.com/fGC86DA.jpg

2 - Save the field's data when product is saved. ( this code goes in functions.php )

add_action( 'woocommerce_process_product_meta', 'my_custom_field_save' );

function my_custom_field_save( $post_id ){  

    $subtitle = $_POST['_subtitle'];
    if( !empty( $subtitle ) )
        update_post_meta( $post_id, '_subtitle', esc_attr( $subtitle ) );

}

3 - Edit single product template and display the field's value

<?php 
global $post;
echo get_post_meta( $post->ID, '_subtitle', true );
?>