|
<?php // only copy if needed |
|
|
|
/** |
|
* If there are upsells or related items for the product, add them in a new tab. |
|
* @See https://www.skyverge.com/?p=4242 |
|
* |
|
* @param string[] $tabs the product tabs |
|
* @return string[] maybe updated tabs array |
|
*/ |
|
function sv_wc_maybe_add_upsell_tab( $tabs ) { |
|
global $product; |
|
|
|
// compatibility check for pre / post WC 3.0 |
|
$upsells = is_callable( array( $product, 'get_upsell_ids' ) ) ? $product->get_upsell_ids() : $product->get_upsells(); |
|
$related = is_callable( array( $product, 'get_cross_sell_ids' ) ) ? $product->get_cross_sell_ids() : $product->get_cross_sells(); |
|
|
|
// if we have no related items, don't add a new tab |
|
if ( empty( $upsells ) && empty( $related ) ) { |
|
return $tabs; |
|
} |
|
|
|
// green light! remove the current upsells |
|
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 ); |
|
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 ); |
|
|
|
// first, make sure that we're dealing with an array rather than null |
|
$new_tabs = is_null( $tabs ) ? array() : $tabs; |
|
|
|
// now add the upsells back in tabulated formation |
|
$new_tabs['upsells'] = array( |
|
'title' => __( 'Related Items', 'woocommerce-upsells-tab' ), |
|
'priority' => 25, |
|
'callback' => 'sv_wc_output_upsells_related_items', |
|
); |
|
|
|
return $new_tabs; |
|
} |
|
add_filter( 'woocommerce_product_tabs', 'sv_wc_maybe_add_upsell_tab' ); |
|
|
|
|
|
// Callback to output related products and upsells. |
|
function sv_wc_output_upsells_related_items() { |
|
woocommerce_upsell_display(); |
|
woocommerce_output_related_products(); |
|
} |