• MisterH

    (@hmarksthespot)


    Hi there,

    Edit: What would be a simple way to implement this with ocean hooks?
    https://www.commercegurus.com/woocommerce-acf/

    We are using OceanWP, and wanted to figure out if there was a simple plugin or way to add a product subtitle to the archive page, under every product title or such.

    I have tried this below code in functions.php with some success. But there must be a better way.

    Should anyone have any better suggestions and or implementation that would be great!

    `//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>’;
    }
    }

    • This topic was modified 3 years ago by MisterH.
Viewing 16 replies (of 16 total)
Viewing 16 replies (of 16 total)
  • The topic ‘Product Subtitle for Woocommerce’ is closed to new replies.