在Woocommerce中向结账表单添加自定义字段

3

我想在Woocommerce的结账表单中添加自定义字段。虽然该字段的显示方式正确,但名称和ID属性是错误的。以下是创建我的字段的函数。

// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
     $fields['billing']['billing_infos'] = array(
        'type'      => 'textarea',
        'label'     => __('Notes de la commande', 'woocommerce'),
    'placeholder'   => _x('Commentaires concernant votre commande', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     return $fields;
}

以下是我在表单中的称呼方式:

<?php woocommerce_form_field( $checkout->checkout_fields['billing'], $checkout->checkout_fields['billing']['billing_infos'], $checkout->get_value( 'billing_infos' ) ); ?>

当我检查我的字段时,我得到的是这样的信息:
<p class="form-row form-row-wide woocommerce-validated" id="Array_field"><label for="Array" class="">Notes de la commande</label><textarea name="Array" class="input-text " id="Array" placeholder="Commentaires concernant votre commande" rows="2" cols="5"></textarea>
                </p>
2个回答

4

我解决了我的问题。我调用的函数的第一个参数是错误的。

以下是正确的调用方法:

<?php woocommerce_form_field( 'billing_infos', $checkout->checkout_fields['billing']['billing_infos'], $checkout->get_value( 'billing_infos' ) ); ?>

我应该把这行代码放在哪里?放在 function.php 文件中。 - Harish Kumar

-2

5
虽然那个链接可能对提问者有帮助,但你不应该将URL作为答案发布。如果链接更改或网站下线,你的答案就变得无用了。相反,当你有足够的声望后,你应该将其作为评论发布。谢谢。 - Cthulhu
此外,它的 WordPress 插件链接永远不会改变。 - Sunil Prajapati

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接