Reputation: 155
I'm having trouble adding "Related Products" to a tab and making it work on posts that are used with a short code. Here's the short code and complete code that's being placed in my functions.php
[product_page id="99"]
Here's the code i'm using in my themes functions.php
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
/*
* Register custom tab
*/
function woo_custom_product_tab( $tabs ) {
$custom_tab = array(
'custom_tab' => array(
'title' => __('Custom Tab','woocommerce'),
'priority' => 9,
'callback' => 'woo_custom_product_tab_content'
)
);
return array_merge( $custom_tab, $tabs );
}
/*
* Place content in custom tab (related products in this sample)
*/
function woo_custom_product_tab_content() {
woocommerce_related_products();
}
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tab' );
Here's the error i'm receiving:
Fatal error: Call to a member function get_upsells() on a non-object in public_html/wp-content/plugins/woocommerce/templates/single-product/up-sells.php on line 25
Upvotes: 1
Views: 1773
Reputation: 254398
I think you need to use the global $product object with WC_Product get_related()
method to avoid this error…
Then the solution could be:
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
/*
* Register custom tab
*/
function woo_custom_product_tab( $tabs ) {
$custom_tab = array(
'custom_tab' => array(
'title' => __('Custom Tab','woocommerce'),
'priority' => 9,
'callback' => 'woo_custom_product_tab_content'
)
);
return array_merge( $custom_tab, $tabs );
}
/*
* Place content in custom tab (related products in this sample)
*/
function woo_custom_product_tab_content() {
global $product;
$product->get_related();
}
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tab' );
As this is untested, I doesn't guaranty anything…
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
Upvotes: 2