Adding multiple tabs to WooCommerce single product pages

前端 未结 3 1993
暖寄归人
暖寄归人 2020-12-01 20:16

I am trying to add three custom tabs to WooCommerce. I have the code below and two of them show up but for some reason the attribute description tab does not show on the pag

相关标签:
3条回答
  • 2020-12-01 20:34

    The only addition I made was to check whether content existed in a tab before adding it. I simply wrapped each new array with an 'if' statement.

    if (!empty(get_the_content())) {
        $tabs['tab'] = array(
            'title'     => __( 'Tab Title', 'woocommerce' ),
            'priority'  => 100,
            'callback'  => 'woo_tab_content'
        );
    }
    
    0 讨论(0)
  • 2020-12-01 20:43

    As you are using 4 times the same hook woocommerce_product_tabs, your problem comes from the highest priority on the first one. Instead you should use it only one time, merging that 4 hooked functions in one.

    Here is your functional tested code, changed a little bit:

    add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
    function woo_custom_product_tabs( $tabs ) {
    
        // 1) Removing tabs
    
        unset( $tabs['description'] );              // Remove the description tab
        // unset( $tabs['reviews'] );               // Remove the reviews tab
        unset( $tabs['additional_information'] );   // Remove the additional information tab
    
    
        // 2 Adding new tabs and set the right order
    
        //Attribute Description tab
        $tabs['attrib_desc_tab'] = array(
            'title'     => __( 'Desc', 'woocommerce' ),
            'priority'  => 100,
            'callback'  => 'woo_attrib_desc_tab_content'
        );
    
        // Adds the qty pricing  tab
        $tabs['qty_pricing_tab'] = array(
            'title'     => __( 'Quantity Pricing', 'woocommerce' ),
            'priority'  => 110,
            'callback'  => 'woo_qty_pricing_tab_content'
        );
    
        // Adds the other products tab
        $tabs['other_products_tab'] = array(
            'title'     => __( 'Other Products', 'woocommerce' ),
            'priority'  => 120,
            'callback'  => 'woo_other_products_tab_content'
        );
    
        return $tabs;
    
    }
    
    // New Tab contents
    
    function woo_attrib_desc_tab_content() {
        // The attribute description tab content
        echo '<h2>Description</h2>';
        echo '<p>Custom description tab.</p>';
    }
    function woo_qty_pricing_tab_content() {
        // The qty pricing tab content
        echo '<h2>Quantity Pricing</h2>';
        echo '<p>Here\'s your quantity pricing tab.</p>';
    }
    function woo_other_products_tab_content() {
        // The other products tab content
        echo '<h2>Other Products</h2>';
        echo '<p>Here\'s your other products tab.</p>';
    }
    

    This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works.


    In the same hook, you can:

    • Remove tabs
    • Add tabs
    • Reorder tabs

    Related official documentation: Editing product data tabs

    0 讨论(0)
  • 2020-12-01 20:54
    function my_simple_custom_product_tab( $tabs ) {
    
        $tabs['my_custom_tab'] = array(
            'title'    => __( 'Custom Tab', 'textdomain' ),
            'callback' => 'my_simple_custom_tab_content',
            'priority' => 50,
        );
    
        return $tabs;
    
    }
    add_filter( 'woocommerce_product_tabs', 'my_simple_custom_product_tab' );
    
    /**
     * Function that displays output for the shipping tab.
     */
    function my_simple_custom_tab_content( $slug, $tab ) {
    
        ?><h2><?php echo wp_kses_post( $tab['title'] ); ?></h2>
        <p>Tab Content</p><?php
    
    }
    
    0 讨论(0)
提交回复
热议问题