我在跟踪this tutorial 关于如何在变体产品中添加额外字段。
我只需要添加一个额外的文本字段。管理部分工作正常-但是,该字段正在显示和保存/更新,the single product page doesn\'t - the field won\'t show.
所以在我的functions.php
我补充道:
add_action( \'woocommerce_product_after_variable_attributes\', \'variation_settings_fields\', 10, 3 );
add_action( \'woocommerce_save_product_variation\', \'save_variation_settings_fields\', 10, 2 );
function variation_settings_fields( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input(
array(
\'id\' => \'_text_field[\' . $variation->ID . \']\',
\'label\' => __( \'Some label\', \'woocommerce\' ),
\'placeholder\' => \'\',
\'desc_tip\' => \'true\',
\'description\' => __( \'Some description.\', \'woocommerce\' ),
\'value\' => get_post_meta( $variation->ID, \'_text_field\', true )
)
);
}
function save_variation_settings_fields( $post_id ) {
$text_field = $_POST[\'_text_field\'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, \'_text_field\', esc_attr( $text_field );
}
}
add_filter( \'woocommerce_available_variation\', \'load_variation_settings_fields\' );
function load_variation_settings_fields( $variations ) {
$variations[\'text_field\'] = get_post_meta( $variations[ \'variation_id\' ], \'_text_field\', true );
return $variations;
}
并且在
plugins/plugins/woocommerce/templates/single-product/add-to-cart/variation.php
我添加了(主题没有自己的
variation.php
):
<div class="woocommerce-variation-custom-text-field">
{{{ data.variation.text_field }}}
</div>
如何在单个产品页面上显示自定义字段?
谢谢
Update: 如果检查单个产品页面,则会显示脚本:
<script type="text/template" id="tmpl-variation-template">
<div class="woocommerce-variation-description">
{{{ data.variation.variation_description }}}
</div>
<div class="woocommerce-variation-price">
{{{ data.variation.price_html }}}
</div>
<div class="woocommerce-variation-custom-text-field">
{{{ data.variation.text_field }}}
</div>
<div class="woocommerce-variation-availability">
{{{ data.variation.availability_html }}}
</div>
</script>
主题是
belise.
Update: 我在看定制woocommerce的主题functions.php
(belise/inc/woocommerce/functions.php)文件,逻辑似乎就在那里。
/**
* Shop page woocommerce-Price-amount amountption on product archives
*/
function belise_woocommerce_template_single_excerpt() {
global $post;
if ( ! $post->post_excerpt ) {
return;
}
?>
<div class="woocommerce-Price-amount amountption" itemprop="woocommerce-Price-amount amountption">
<!-- if I add <p>test</p> it is printed on the product page -->
<!-- I would like for the custom field element to be printed here -->
<?php echo apply_filters( \'woocommerce_short_woocommerce-Price-amount amountption\', $post->post_excerpt ); ?>
</div><?php
}
Answer - @ClemC第一个解决方案有效,由于浏览器缓存,该字段未显示。