我在将“相关产品”添加到标签上并使其在使用短代码的帖子上无法正常工作时遇到了麻烦。这是放置在我的functions.php中的短代码和完整代码
[product_page id =“ 99”]
这是我在主题theme.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()
我认为您需要将全局$ product对象与WC_Productget_related()
方法一起使用,以避免出现此错误…
那么解决方案可能是:
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文件中使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句