Forked from simonlk/Woocommerce - output product variations in tab
Last active
February 24, 2018 14:02
-
-
Save Winsiders/44c3dea8735b798469020d973c1f3f77 to your computer and use it in GitHub Desktop.
Output Woocommerce product variations as a table within a tab on the single product page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add to functions.php | |
/*=================================================== | |
Created by sk from Renegade Empire with help | |
from these sources: | |
http://docs.woothemes.com/document/editing-product-data-tabs/ | |
http://www.sean-barton.co.uk/2013/03/remove-woocommerce-20-reviews-tab/#.UYnWe7XfB6N | |
http://www.sean-barton.co.uk/2013/03/sb-add-woocommerce-tabs-wordpress-plugin/#.UYrYL7XfB6M | |
Modified and updated by W. Loiseau From http://winsiders.fr - Work on WC 3+ | |
====================================================*/ | |
/*=================================================== | |
Options | |
====================================================*/ | |
$re_wcvt_options = array( | |
'tab_title' => 'Product Variations', // change the tile of the tab | |
'sku_title' => 'REF #', // change the sku column heading | |
'show_price' => 'yes', // show price column: yes or no | |
'show_description' => 'yes', // show description column: yes or no | |
'tab_priority' => '20', // 5 is good to make the tab appear first | |
); | |
/*=================================================== | |
Add the tab | |
====================================================*/ | |
add_filter( 'woocommerce_product_tabs', 're_woo_product_variations_tab' ); | |
function re_woo_product_variations_tab($tabs) { | |
global $product, $re_wcvt_options; | |
if ( is_product() and $product->get_type() == 'variable') { | |
// Adds the new tab | |
if( current_user_can( 'shop_manager' ) || current_user_can('administrator') ){ | |
$tabs['variations_table'] = array( | |
'title' => __( $re_wcvt_options['tab_title'], 'woocommerce' ), | |
'priority' => 50, | |
'callback' => 're_woo_product_variations_tab_content' | |
); | |
} | |
} | |
return $tabs; | |
} | |
/*=================================================== | |
Build the tab content | |
====================================================*/ | |
function re_woo_product_variations_tab_content() { | |
global $product, $post, $re_wcvt_options; | |
$available_variations = $product->get_available_variations(); | |
$attributes = $product->get_attributes(); | |
?> | |
<table class="table table-striped table-hover table-bordered varations-table tablesorter"> | |
<thead> | |
<tr> | |
<th><?php echo $re_wcvt_options['sku_title']; ?></th> | |
<?php | |
// Show description if option is set to yes | |
if ($re_wcvt_options['show_description'] == 'yes') { | |
?> | |
<th>Description</th> | |
<?php | |
} | |
foreach ( $attributes as $name => $options) : | |
$attr_name = $options['name']; | |
if( $attributes[$name]['variation']==true ){ | |
echo '<th>'.wc_attribute_label($attr_name).'</th>'; | |
} | |
endforeach; | |
// Show price if option is set to yes | |
if ($re_wcvt_options['show_price'] == 'yes') { ?> | |
<th>Prix</th> | |
<?php } ?> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
foreach ($available_variations as $prod_variation) : | |
$post_id = $prod_variation['variation_id']; | |
$post_object = get_post($post_id); | |
?> | |
<tr> | |
<td> | |
<?php | |
$sku = $prod_variation['sku']; | |
echo '<a href="'.get_permalink($post_object->ID).'">'.$sku."</a>"; | |
?> | |
</td> | |
<?php | |
// Show description if option is set to yes | |
if ($re_wcvt_options['show_description'] == 'yes') : ?> | |
<td> | |
<?php | |
$variation_desc = $prod_variation['variation_description']; | |
echo $variation_desc; | |
?> | |
</td> | |
<?php endif; | |
?> | |
<?php foreach ($prod_variation['attributes'] as $attr_name => $attr_value) : ?> | |
<td> | |
<?php | |
// Get the correct variation values | |
if (strpos($attr_name, '_pa_')){ // variation is a pre-definted attribute | |
$attr_name = substr($attr_name, 10); | |
$attr = get_term_by('slug', $attr_value, $attr_name); | |
$attr_value = $attr->name; | |
} | |
echo $attr_value; | |
?> | |
</td> | |
<?php endforeach;?> | |
<?php | |
// Show price if option is set to yes | |
if ($re_wcvt_options['show_price'] == 'yes') { ?> | |
<td> | |
<?php | |
$price = get_post_meta( $post_object->ID, '_price', true); | |
echo wc_price($price); | |
?> | |
</td> | |
<?php } ?> | |
</tr> | |
<?php | |
endforeach; | |
?> | |
</tbody> | |
</table> | |
<?php | |
} | |
/*=================================================== | |
Tab Position | |
====================================================*/ | |
add_filter( 'woocommerce_product_tabs', 're_woo_move_variation_table_tab', 98); | |
function re_woo_move_variation_table_tab($tabs) { | |
global $re_wcvt_options; | |
if( current_user_can( 'shop_manager' ) || current_user_can('administrator') ){ | |
if ($tabs['variations_table']) { | |
$tabs['variations_table']['priority'] = $re_wcvt_options['tab_priority']; | |
} | |
} | |
return $tabs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment