Add Unique Content For Each Product Under Title on the WooCommerce Archive Page

In a WooCommerce store, showcasing unique content alongside product titles on the shop page can provide valuable information to customers and help highlight key features or promotions. In this guide, we’ll demonstrate how to add a custom content field to the product editing screen, allowing you to enter unique content for each product. This content will then be displayed under the product title on the shop page, enhancing the product presentation and improving the shopping experience for your customers.

/**
* Snippet Name:  Show unique content for each product on shop page under title
* Snippet Author:	  wpsnippets.dev
*/

// adding custom field
function wps_add_custom_field() {
   echo '<div class="options_group">';
   woocommerce_wp_text_input (array (
      'id'                => 'custom_content_under_loop_title',
      'value'             => get_post_meta (get_the_ID(), 'custom_content_under_loop_title', true),
      'label'             => 'Custom Content',
      'description'       => 'Content to be placed between the title and price on archive pages'
  ));
   echo '</div>';
}
add_action ('woocommerce_product_options_advanced', 'wps_add_custom_field');

// updating custom field when post is updated
function wps_save_custom_content_field ($id, $post) {
      update_post_meta ($id, 'custom_content_under_loop_title', $_POST['custom_content_under_loop_title']);
}
add_action ('woocommerce_process_product_meta', 'wps_save_custom_content_field', 10, 2);

// adding content under title
function wps_add_custom_content_under_title() {

    global $product;
    echo $custom_loop_content_under_title = $product->get_meta      ('custom_content_under_loop_title');
};
add_action( 'woocommerce_after_shop_loop_item_title', 'wps_add_custom_content_under_title', 1, 0 );

Code Explanation

  • Custom Field Addition:
    • The function wps_add_custom_field adds a custom text input field to the advanced options section of the product editing screen.
    • This field is labeled “Custom Content” and is intended for entering content to be displayed on archive pages.
  • Custom Field Saving:
    • The function wps_save_custom_content_field saves the content entered in the custom field when the product is updated or saved.
  • Content Display:
    • The function wps_add_custom_content_under_title retrieves the custom content for each product and echoes it out under the product title on the shop page.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top