Добавьте данные на пользовательскую вкладку на странице продукта WooCommerce

Чтобы добавить данные на настраиваемую вкладку на странице продукта WooCommerce, вы можете использовать различные методы. Вот несколько примеров с фрагментами кода:

Метод 1. Использование настраиваемого поля

  1. Добавьте следующий код в файл function.php вашей темы:
// Add custom tab to product page
function custom_product_tab( $tabs ) {
    $tabs['custom_tab'] = array(
        'title'    => __( 'Custom Tab', 'text-domain' ),
        'priority' => 50,
        'callback' => 'custom_tab_content',
    );
    return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'custom_product_tab' );
// Custom tab content
function custom_tab_content() {
    echo '<h2>' . __( 'Custom Tab', 'text-domain' ) . '</h2>';
    echo '<p>' . get_post_meta( get_the_ID(), 'custom_tab_content', true ) . '</p>';
}
// Save custom tab data
function save_custom_tab_data( $post_id ) {
    if ( isset( $_POST['custom_tab_content'] ) ) {
        update_post_meta( $post_id, 'custom_tab_content', sanitize_text_field( $_POST['custom_tab_content'] ) );
    }
}
add_action( 'woocommerce_process_product_meta', 'save_custom_tab_data' );
// Display custom tab data in product editor
function display_custom_tab_data() {
    global $post;
    $custom_tab_content = get_post_meta( $post->ID, 'custom_tab_content', true );
    ?>
    <div class="options_group">
        <p class="form-field">
            <label for="custom_tab_content"><?php _e( 'Custom Tab Content', 'text-domain' ); ?></label>
            <textarea name="custom_tab_content" id="custom_tab_content" rows="5"><?php echo esc_textarea( $custom_tab_content ); ?></textarea>
        </p>
    </div>
    <?php
}
add_action( 'woocommerce_product_data_panels', 'display_custom_tab_data' );

Метод 2. Использование собственного плагина

  1. Создайте новый файл плагина (например, custom-product-tab.php) и добавьте следующий код:
/*
Plugin Name: Custom Product Tab
*/
// Add custom tab to product page
function custom_product_tab( $tabs ) {
    $tabs['custom_tab'] = array(
        'title'    => __( 'Custom Tab', 'text-domain' ),
        'priority' => 50,
        'callback' => 'custom_tab_content',
    );
    return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'custom_product_tab' );
// Custom tab content
function custom_tab_content() {
    echo '<h2>' . __( 'Custom Tab', 'text-domain' ) . '</h2>';
    echo '<p>' . get_post_meta( get_the_ID(), 'custom_tab_content', true ) . '</p>';
}
// Save custom tab data
function save_custom_tab_data( $post_id ) {
    if ( isset( $_POST['custom_tab_content'] ) ) {
        update_post_meta( $post_id, 'custom_tab_content', sanitize_text_field( $_POST['custom_tab_content'] ) );
    }
}
add_action( 'woocommerce_process_product_meta', 'save_custom_tab_data' );
// Display custom tab data in product editor
function display_custom_tab_data() {
    global $post;
    $custom_tab_content = get_post_meta( $post->ID, 'custom_tab_content', true );
    ?>
    <div class="options_group">
        <p class="form-field">
            <label for="custom_tab_content"><?php _e( 'Custom Tab Content', 'text-domain' ); ?></label>
            <textarea name="custom_tab_content" id="custom_tab_content" rows="5"><?php echo esc_textarea( $custom_tab_content ); ?></textarea>
        </p>
    </div>
    <?php
}
add_action( 'woocommerce_product_data_panels', 'display_custom_tab_data' );

Эти методы позволяют добавить пользовательскую вкладку на страницу продукта WooCommerce и сохранять/отображать данные для этой вкладки. Вы можете изменить фрагменты кода в соответствии со своими требованиями.