I’m trying to display WooCommerce cross sells inside a custom tab in single product pages (similar tab as "Reviews" and "Additional information"). For that I tried using WooCommerce function:
woocommerce_cross_sell_display();
But it doesn’t work (I receive no error and no visual result).
Here is what I tried so far:
//Add custom tabs filter
add_filter('woocommerce_product_tabs', 'add_new_default_product_tab' );
function add_new_default_product_tab( $tabs ) {
global $product;
// set the new priority to the "reviews" tab
$tabs['reviews']['priority'] = 20;
// gets the value to use as the title and slug of the new tab
$custom_tab_title = "אביזרים";
$custom_tab_title2 = "אביזרים משלימים";
// if the custom field is set, it adds the new tab
if ( ! empty($custom_tab_title) ) {
$tabs['awp-' . sanitize_title('props')] = array(
'title' => 'אביזרים',
'callback' => 'awp_custom_woocommerce_tabs',
'priority' => 5
);
}
if ( ! empty($custom_tab_title) ) {
$tabs['awp-' . sanitize_title('additional-props')] = array(
'title' => 'אביזרים משלימים',
'callback' => 'awp_custom_woocommerce_tabs2',
'priority' => 10
);
}
return $tabs;
}
//Callback to display upsells (WORKS)
function awp_custom_woocommerce_tabs($key, $tab) {
woocommerce_upsell_display( 3,3 );
}
//Callback to display cross sells (Doesn't work)
function awp_custom_woocommerce_tabs2($key, $tab) {
woocommerce_cross_sell_display();
}
Its wired because upsells working fine, but cross sells (which basically is the same) doesn’t work.
How to display cross sells inside WooCommerce single product pages custom tab?
2
Answers
Check the below code.
Product cross sells are for cart and some changes in
woocommerce_cross_sell_display()
function are required, to get it work in product single pages.This can be done cloning the code from that function in a custom function and changing this line:
with
So your final code is going to be:
Code goes in functions.php file of the active child theme (or active theme). Tested and works.