John Davidson

javascript - Display WooCommerce tab based on product attribute value

0 comments
Message:


I have the code below to add a tab to display additional product data when available in an external database. It works great, however, I only want the tab to display when the attribute "pa_data-quality" equals "icecat" (to avoid having a blank tab when data is not available). I can't seem to get it to work with if statements, and can't seem to find anything in WC documentation.


function woo_new_product_tab( $tabs ) {

$tabs['featurestab'] = array(
'title' => __( 'From the Manufacturer', 'woocommerce' ),
'priority' => 0,
'callback' => 'icecat_tab_content');
unset( $tabs['description'] ); // Remove the description tab
return $tabs;
}

function icecat_tab_content() {
echo '<div id="Container3"></div>
<div id="Container4"></div>
<div id="Container5"></div>
<div id="Container6"></div>
<div id="Container7"></div>
<div id="Container9"></div>
<div id="Container10"></div>
<div id="Container11"></div>';
}

add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );


My Answer:
value "external_data" is set to "true" for the product. How can I modify the code to achieve this?

javascript
add_filter( 'woocommerce_product_tabs', 'add_external_data_tab' );

function add_external_data_tab( $tabs ) {
global $product;

// Check if product has attribute "external_data" set to "true"
$external_data = $product->get_attribute( 'external_data' );

if ( $external_data == 'true' ) {
$tabs['external_data_tab'] = array(
'title' => __( 'External Data', 'woocommerce' ),
'priority' => 50,
'callback' => 'display_external_data_tab_content'
);
}

return $tabs;
}

function display_external_data_tab_content() {
// Display content for external data tab
}


In the code above, I have added a check to see if the product attribute "external_data" is set to "true" before adding the tab to the product page. If the attribute is set to "true", the tab will be displayed, otherwise it will not be added to the product tabs.

This should achieve the desired functionality of displaying the tab only when the attribute value is set to "true" for the product.

Rate this post

4 of 5 based on 5282 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us