我正在尝试使用 ACF 将自定义属性添加到 WooCommerce 中产品的“附加信息”选项卡。我希望管理员能够上传一个 PDF,然后应该在“其他信息”选项卡中的表格中进行设置和链接。
我在 WooCommerce 的插件目录中找到了属性的默认模板,更准确地说是 woocommerce/templates/single-product/product-attributes.php
。 This is how the template looks .我可以轻松地将 the_field("pdf")
放在那里以显示自定义字段,但我遇到的问题是附加信息选项卡只会显示是否已通过 WooCommerce 添加属性.
有什么方法可以为何时显示“附加信息”选项卡添加附加条件标签?如果我能以某种方式添加 if (get_field("pdf"))
到该代码,我认为这将得到解决。
最佳答案
更新:在 Woocommerce 中,如果存在任何尺寸、重量或产品属性(设置为显示在产品上),将显示“附加信息”选项卡……
因此,如果选项卡被隐藏并且您在 single-product/product-attributes.php
模板上添加了一个自定义字段 get_field("pdf")
要显示具有值,您可以使用以下方法强制显示“附加信息”标签:
add_filter( 'woocommerce_product_tabs', 'woo_customize_tabs', 100, 1 );
function woo_customize_tabs( $tabs ) {
if( ! isset($tabs['additional_information']) && null !== get_field("pdf") ){
$tabs['reviews']['priority'] = 30;
$reviews = $tabs['reviews'];
unset($tabs['reviews']);
$tabs['additional_information'] = array(
'title' => __( 'Additional information', 'woocommerce' ),
'priority' => '20',
'callback' => 'woocommerce_product_additional_information_tab',
);
$tabs['reviews'] = $reviews;
}
return $tabs;
}
代码进入事件子主题(或事件主题)的 function.php 文件。经过测试并有效。
关于php - 将自定义字段添加到 "Additional Information"选项卡 (WooCommerce),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49617342/