Oguz
Oguz

Reputation: 3

Add specific attributes to secondary product tab on WordPress?

I'm trying to add some attributes to my second product tab but i couldn't. I have 6 attributes on my Woocommerce Product page in Wordpress and I want to show 3 of them in the first tab and show the other three on the second tab. Someone who can guide me the right way?

add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );

function woo_rename_tabs( $tabs ) {
    global $product;

    if( $product->has_attributes() ) { 
        $tabs['additional_information']['title'] = __( 'Product Info' );
    }
return $tabs;
}

add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );

function woo_new_product_tab( $tabs ) {

    $tabs['test_tab'] = array(
        'title'     => __( 'Details', 'woocommerce' ),
        'priority'  => 50,
        'callback'  => 'woo_new_product_tab_content'
    );
return $tabs;
}

function woo_new_product_tab_content() {
    // The new tab content
    ????Please help on this lines?????
}

Upvotes: 0

Views: 1034

Answers (1)

kashalo
kashalo

Reputation: 3562

you need to alter the product-attributes.php which you can find in

woocommerce > single-product > product-attributes.php

if you don't have one in your child theme create one and copy the product-attributes.php content to new file.

we will use this file to include certain attribute to be displayed in the additional info tab.

then create another product-attributes.php file in the same directory and we will call it product-attributes-custom.php

we will use this file to display the rest of your attribute.

Now in your new function add the following line:

function woo_new_product_tab_content()
{
    global $product;
    wc_get_template('single-product/product-attributes-custom.php', array(
        'product' => $product,
        'attributes' => array_filter($product->get_attributes(), 'wc_attributes_array_filter_visible'),
        'display_dimensions' => '',
    ));

}

now open the first product-attributes.php file and go to line 40 where you can find the following :

<?php foreach ($attributes as $attribute): ?>

and change it to following :

<?php foreach ($attributes as $attribute):
$att = ['att1' , 'att2' , 'att3' ]; // Here you can define what attribute you want to display in the tab content 
if (in_array($attribute->get_name(), $att)):
?>

and then find the line with following code:

<?php endforeach; ?>

and change it to :

<?php endif;endforeach;?>

and finally open the second file product-attributes-custom.php

and do the same as above as previous one and add the rest of attribute you want to display in the new tab :

<?php foreach ($attributes as $attribute): ?>

and change it to following :

<?php foreach ($attributes as $attribute):
$att = ['att4' , 'att5', 'att6']; // Here you can define what attribute you want to display in the tab content 
if (in_array($attribute->get_name(), $att)):
?>

and then find the line with following code:

<?php endforeach; ?>

and change it to :

<?php endif;endforeach;?>

That's it :)

Upvotes: 1

Related Questions