【问题标题】:Add a custom field to WooCommerce Billing form?向 WooCommerce 计费表单添加自定义字段?
【发布时间】:2017-07-09 13:18:23
【问题描述】:

我获得此代码以将自定义字段添加到 WooCommerce 计费表单。

显示了该字段,但问题是该字段没有 label 也没有 placeholder 也没有 class name

我在这里缺少什么? 我将此代码添加到我的子主题中的functions.php。

/*******************************
  CUSTOM BILLING FIELD
 ******************************** */
add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');

function custom_woocommerce_billing_fields($fields)
{

    $fields['billing']['billing_options'] = array(
        'label' => __('NIF', 'woocommerce'), // Add custom field label
        'placeholder' => _x('Your NIF here....', 'placeholder', 'woocommerce'), // Add custom field placeholder
        'required' => false, // if field is required or not
        'clear' => false, // add clear or not
        'type' => 'text', // add field type
        'class' => array('my-css')    // add class name
    );

    return $fields;
}

【问题讨论】:

  • 您可以将自定义计费字段的代码添加到get_default_address_fields() 文件中的class-wc-countries.php 函数

标签: php wordpress woocommerce checkout hook-woocommerce


【解决方案1】:

如果您使用的是woocommerce_billing_fields,那么您不需要 指定将自动分配给帐单的字段 字段。但是,如果您使用的是woocommerce_checkout_fields,那么只有 您需要指定您想要shippingbilling 的字段。

对于woocommerce_billing_fields

add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');

function custom_woocommerce_billing_fields($fields)
{

    $fields['billing_options'] = array(
        'label' => __('NIF', 'woocommerce'), // Add custom field label
        'placeholder' => _x('Your NIF here....', 'placeholder', 'woocommerce'), // Add custom field placeholder
        'required' => false, // if field is required or not
        'clear' => false, // add clear or not
        'type' => 'text', // add field type
        'class' => array('my-css')    // add class name
    );

    return $fields;
}


对于woocommerce_checkout_fields
add_filter('woocommerce_checkout_fields', 'custom_woocommerce_billing_fields');

function custom_woocommerce_billing_fields($fields)
{
    $fields['billing']['billing_options'] = array(
        'label' => __('NIF', 'woocommerce'), // Add custom field label
        'placeholder' => _x('Your NIF here....', 'placeholder', 'woocommerce'), // Add custom field placeholder
        'required' => false, // if field is required or not
        'clear' => false, // add clear or not
        'type' => 'text', // add field type
        'class' => array('my-css')   // add class name
    );

    return $fields;
}

参考:

【讨论】:

  • 我不工作。帐单表单中的所有字段都将被删除。并且没有添加新字段。没有显示错误。
  • @JPashs:我还没有从我的大脑中测试我的代码,但是你使用的钩子和数组参数中存在逻辑错误woocommerce_billing_fields 并再次添加['billing'],作为结果你没有得到想要的输出。只需用这个woocommerce_checkout_fields 替换你的钩子并保持你提到的功能是问题然后它也会起作用。
  • 我试过这个:pastebin.com/raw/JrnHfuVT 但还不行。您能告诉我我缺少什么并更改答案中的代码吗?这样我才能接受你的回答。谢谢。
  • 它有效。谢谢。但是我创建了一个填写这个新字段的新订单,然后在仪表板中以管理员身份检查了订单后,我没有看到为该字段发送的值。我需要查看订单中发送的值。我还需要在这里做什么?
  • 其实字段是保存的,只是没有显示而已。例如,在管理订单数据中添加一个动作就可以了add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 ); function my_custom_checkout_field_display_admin_order_meta($order){ echo '<p><strong>'.__('Custom field title').':</strong> ' . get_post_meta( $order->get_id(), '_shipping_custom_field', true ) . '</p>'; }
猜你喜欢
  • 2015-06-01
  • 1970-01-01
  • 2019-05-26
  • 2020-12-30
  • 1970-01-01
  • 2019-07-29
  • 2015-07-12
  • 2019-07-17
  • 1970-01-01
相关资源
最近更新 更多