我在向标签添加“相关产品”时遇到问题,并使其适用于与短代码一起使用的帖子。这是我的functions.php
中的简短代码和完整代码[product_page id =“99”]
这是我在主题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' );
以下是我收到的错误:
致命错误:在第25行的public_html / wp-content / plugins / woocommerce / templates / single-product / up-sells.php中的非对象上调用成员函数get_upsells()
答案 0 :(得分:2)
我认为您需要使用WC_Product get_related()
method的全局$ product对象来避免此错误...
然后解决方案可能是:
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' );
由于这是未经测试的,我不保证任何事情......
代码进入活动子主题(或主题)的function.php文件。或者也可以在任何插件php文件中。