WooCommerce 产品页面默认有3个选项卡:描述,其它信息和评论,对于很多 WordPress 主题或许不太适合,需要修改或者添加选项卡。WooCommerce 官方文档已经给出了详细的文档:
需要将代码添加到主题functions.php文件。
/** * 删除产品数据选项卡 */add_filter( woocommerce_product_tabs, woo_remove_product_tabs, 98 ); function woo_remove_product_tabs( $tabs ) { unset( $tabs ); // 删除描述 unset( $tabs ); // 删除评论 unset( $tabs ); // 删除其它信息 return $tabs;}/** * 重命名选项卡 */add_filter( woocommerce_product_tabs, woo_rename_tabs, 98 );function woo_rename_tabs( $tabs ) { $tabs = __( 产品详情 ); // 重命名描述 $tabs = __( 产品评论 ); // 签名评论 $tabs = __( 产品参数 ); // 签名其它信息 return $tabs; }/** * 更改选项卡顺序 */add_filter( woocommerce_product_tabs, woo_reorder_tabs, 98 );function woo_reorder_tabs( $tabs ) { $tabs = 5; // 评论第一 $tabs = 10; // 描述第二 $tabs = 15; // 其它信息第三 return $tabs;}/** * 自定义选项卡内容 */add_filter( woocommerce_product_tabs, woo_custom_description_tab, 98 );function woo_custom_description_tab( $tabs ) { $tabs = woo_custom_description_tab_content; // 自定义描述回调 return $tabs;}/** * 选项卡内容 */ function woo_custom_description_tab_content() { echo h2Custom Description/h2; echo pHere\s a custom description/p;}/** * 添加一个自定义选项卡 */add_filter( woocommerce_product_tabs, woo_new_product_tab );function woo_new_product_tab( $tabs ) {// 添加一个新选项卡 $tabs = array(title = __( 名称, woocommerce ),priority = 50,callback = woo_new_product_tab_content); return $tabs; }/** * 选项卡内容 */function woo_new_product_tab_content() { // The new tab content echo h2New Product Tab/h2;echo pHere\s your new product tab./p; }
“附加信息”选项卡仅在产品设置了重量、尺寸或属性,并选中“在产品页面上可见”时才会显示。如果在产品没有重量、尺寸或属性时,尝试对该选项卡更改,将出现类似于以下的错误消息:
Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in /mysite/wpcontent/plugins/woocommerce/templates/singleproduct/tabs/tabs.php on line 35
在这种情况下,必须使用 WooCommerce 条件判断:
has_attributes()has_dimensions()has_weight()
比如此段代码:
/** * 检查产品是否有属性、尺寸或重量 */add_filter( woocommerce_product_tabs, woo_rename_tabs, 98 ); function woo_rename_tabs( $tabs ) { global $product;if( $product-has_attributes() || $product-has_dimensions() || $product-has_weight() ) { $tabs = __( 产品参数 ); // 重命名附加信息选项卡}return $tabs;}
参考上面的代码修改成你想要的各种tabs内容。
wordpress网站woocommerce产品增加tabs切换菜单的方法 https://cdnanqi.cn/wangluoyingxiao/17044.html