使用Wordpress / Woocommerce .
我有这个代码,在结帐页面添加了一个名为“NIF / CIF”的自定义结算字段 . 它工作正常,但它的 Value 不会保存在客户帐户“帐单地址”数据中 .
客户首次下单后,所有帐单邮寄地址值都会保存在他的帐户中:地址,州,国家等 . 但我的自定义字段未保存 .
我想在我的函数中缺少一行代码来保存它在数据库中的值,但我不知道如何从这开始 .
/*******************************
CUSTOM BILLING FIELD
*********************************/
add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields)
{
$fields['nif_cif'] = array(
'label' => __('NIF/CIF', 'woocommerce'), // Add custom field label
'placeholder' => _x('NIF/CIF', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => true, // 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;
}
2 回答
通过'woocommerce_billing_fields'钩子添加额外的字段是不够的 . 你错过了两件事 .
使用'woocommerce_after_checkout_validation' hook正确验证NIF / CIF字段
使用'woocommerce_checkout_order_processed' hook按顺序保存数据
以下是如何保存自定义字段的示例: