• Resolved stoicassistant

    (@nickschmitt)


    On the WooCommerce archives page, I want to add a subheading underneath each product’s title.

    See this image for reference. Red boxes indicate subheading placement.

    I tried to follow this guy, Add a custom field value below product title in WooCommerce archives pages, but the Custom Product Text Field is not available to me.

    OceanWP has a subheading field, so I tried to change the code from the above linked post, and came up with this, but it doesn’t work:

    add_action( 'woocommerce_after_shop_loop_item_title', 'subheading_display_below_title', 2 );
    function subheading_display_below_title(){
        global $product;
    
        // Get the custom field value
        $subheading = get_post_meta( $product->get_id(), '_subheading', true );
    
        // Display
        if( ! empty($subheading) ){
            echo '<p class="subheading">'.$subheading.'</p>';
        }
    }

    How can I add my OceanWP product subheading beneath my product title on the product archive page?

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,

    Try to use ocean_after_archive_product_title hook and check it works or not.

    Thread Starter stoicassistant

    (@nickschmitt)

    I changed woocommerce_after_shop_loop_item_title to ocean_after_archive_product_title but it didn’t work. Any idea how I can fix this?

    add_action( 'ocean_after_archive_product_title', 'subheading_display_below_title', 2 );
    function subheading_display_below_title(){
        global $product;
    
        // Get the custom field value
        $subheading = get_post_meta( $product->get_id(), 'subheading', true );
    
        // Display
        if( ! empty($subheading) ){
            echo '<p class="subheading">'.$subheading.'</p>';
        }
    
    }

    Thank you.

    Thread Starter stoicassistant

    (@nickschmitt)

    I found the solution, I combined your suggestion with someone else’s help in my post on Stackoverflow. Thank you very much for your help. Here is the full code snippet:

    //Register the product custom field
    add_action( 'woocommerce_product_options_general_product_data', 'my_woocommerce_product_subheading' );
    
    function my_woocommerce_product_subheading() {
        $args = array(
            'label' => 'Subheading', // Text in Label
            'placeholder' => 'My Subheading',
            'id' => 'product_subheading', // required
            'name' => 'product_subheading',
            'desc_tip' => 'The product subheading',
        );
        woocommerce_wp_text_input( $args );
    }
    
    //Save the custom field as product custom post meta
    add_action( 'woocommerce_process_product_meta', 'my_woocommerce_save_product_subheading' );
    
    function my_woocommerce_save_product_subheading( $post_id ) {
        $product = wc_get_product( $post_id );
        $title = isset( $_POST['product_subheading'] ) ? $_POST['product_subheading'] : '';
        $product->update_meta_data( 'product_subheading', sanitize_text_field( $title ) );
        $product->save();
    }
    
    //Display the custom field on product page loop below the title
    add_action( 'ocean_after_archive_product_title', 'subheading_display_below_title', 2 );
    function subheading_display_below_title(){
        global $product;
    
        // Get the custom field value
        $subheading = get_post_meta( $product->get_id(), 'product_subheading', true );
    
        // Display
        if( ! empty($subheading) ){
            echo '<p class="subheading">'.$subheading.'</p>';
        }
    }

    Glad to hear that it is fixed and thank you for adding the solution here.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add product subheading underneath product title on archive page’ is closed to new replies.