Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save shamimsdp/69b93abea02ff7849063bd8b98e48d47 to your computer and use it in GitHub Desktop.

Select an option

Save shamimsdp/69b93abea02ff7849063bd8b98e48d47 to your computer and use it in GitHub Desktop.

Revisions

  1. @bekarice bekarice created this gist Mar 18, 2017.
    45 changes: 45 additions & 0 deletions move-wc-related-items-to-product-tab.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?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();
    }