为WooCommerce产品页添加自定义选项卡

WooCommerce默认有产品描述、评论等选项卡,往往我们对这些选项卡觉得不够用。比如,我们需要添加个发货时间说明、额外的运费说明,还需要加个联系客服询价等等内容就无处加了。因此,我们需要给默认选项卡位置添加一些自定义选项卡来显示相关内容。上例截图中加了自定义询价选项卡。那么如何才能为WooCommerce产品页添加自定义选项卡呢?方法是用wordpress的add_filter配合custom field来实现。以下是实现代码:
/*
WooCommerce添加自定义选项卡
代码来源: www.wpzxbj.com
*/
add_filter( 'woocommerce_product_tabs', 'wc_add_features_tab' );
function wc_add_features_tab( $tabs ){
global $product;
$content = get_post_meta( $product->id, 'product_features', true );
if( !empty($content) ) {
$tabs[ 'features' ] = array(
'title' => 'Features', //标签名
'priority' => 1,
'callback' => 'wc_features_tabs_panel_content',
'content' => $content, // custom field
);
}
return $tabs;
}
function wc_features_tabs_panel_content( $key, $tab ){
echo '' . $tab['title'] . '
';
echo $tab['content'];
}
代码说明:以上代码将为产品页添加一个Features的选项卡,内容可以用custom field来添加。将以上代码插入主题function.php,并创建对应的custom field。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END

















