Untitled

 avatar
unknown
plain_text
10 months ago
1.1 kB
19
Indexable
function show_custom_fields_in_new_tab() {
  // Get the current product object
  $product = wc_get_product(get_the_id());

  // Check if the custom fields are empty
  if (empty($product->get_meta('custom_field_1')) && empty($product->get_meta('custom_field_2')) && empty($product->get_meta('custom_field_3'))) {
    return;
  }

  // Create a new tab
  $tabs = array(
    'custom_fields' => array(
      'title' => __( 'Custom Fields', 'your-text-domain' ),
      'callback' => 'display_custom_fields',
    ),
  );

  // Add the tab to the product page
  woocommerce_add_tabs( $tabs );

  // Function to display the custom fields
  function display_custom_fields() {
    echo '<h2>Custom Fields</h2>';
    echo '<ul>';
    echo '<li><strong>Custom Field 1:</strong> ' . $product->get_meta('custom_field_1') . '</li>';
    echo '<li><strong>Custom Field 2:</strong> ' . $product->get_meta('custom_field_2') . '</li>';
    echo '<li><strong>Custom Field 3:</strong> ' . $product->get_meta('custom_field_3') . '</li>';
    echo '</ul>';
  }
}

add_action( 'woocommerce_after_single_product', 'show_custom_fields_in_new_tab' );
Editor is loading...
Leave a Comment