嗨,今天我正在与woo-commerce合作,我已经成功地根据用户要求创建了一些自定义结帐字段,但我无法将它们保存在数据库中。
下面是我如何在子主题functions.php
中创建自定义结帐fields...its
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Over Ridding, Removing, Creating New Fields.
function custom_override_checkout_fields( $fields ) {
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_address_2']);
unset($fields['order']['order_comments']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_email']);
$fields['billing']['your_name'] = array(
'type' => 'text',
'label' => __('Full Name', 'woocommerce'),
'placeholder' => _x('Full Name', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['your_phone_number'] = array(
'type' => 'text',
'label' => __('Your Phone Number', 'woocommerce'),
'placeholder' => _x('Your Phone Number', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['recipient_name'] = array(
'type' => 'text',
'label' => __("Recipient's Name", 'woocommerce'),
'placeholder' => _x("Recipient's Name", 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['recipient_company_name'] = array(
'type' => 'text',
'label' => __("Recipient's Company (if any)", 'woocommerce'),
'placeholder' => _x("Recipient's Company (if any)", 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['recipient_phone_number'] = array(
'type' => 'text',
'label' => __("Recipient's Phone Number", 'woocommerce'),
'placeholder' => _x("Recipient's Phone Number", 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['recipient_address'] = array(
'type' => 'text',
'label' => __("Recipient's Address", 'woocommerce'),
'placeholder' => _x("Recipient's Address", 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
在db中,我正在寻找字段。它的wp_postmeta
表。附件是我正在使用订单id搜索的屏幕截图。
现在,我添加了checkout_update_order_meta
操作来更新订单元并存储自定义创建的字段。但它似乎不起作用,因为当我检入具有最新创建的订单id的wp_postmeta
表时,我在那里找不到我的自定义字段。
add_action( 'woocommerce_checkout_update_order_meta', 'some_custom_checkout_field_update_order_meta' );
function some_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['recipient_address'] ) ) {
add_post_meta( $order_id, 'recipient_address', sanitize_text_field( $_POST['recipient_address'] ) );
}
if (!empty($_POST['recipient_phone_number'])) {
update_post_meta($order_id, 'recipient phone number', sanitize_text_field($_POST['recipient_phone_number']));
}
}
这是我第一次处理woocommerce代码,我搜索了很多,当我放弃它的时候,我来到了这里。请帮我解开这个谜团。
请纠正我做错了什么。另外,在这一步之后,我将不得不在wordpress仪表板中的woocommerce >订单>订单详细信息下显示这些自定义字段,所以如果有任何有用的链接,请提供。
提前谢谢。
发布于 2017-05-18 23:29:59
我只是稍微修改了一下你最后的钩子函数,它就能工作了(在WC版本2.6.x和3.0+上)。在 php函数中,最好使用变量(以便与回溯兼容)。
此外,由于此函数将确保meta_key
已经存在,如果不存在,则将改为调用add_post_meta()
,因此最好使用而不是。
这里是与订单元数据相关的wp_postmeta
表的屏幕截图:
如果meta_key
没有像下面这样以下划线开头,它会出现在自定义字段metabox中的后端顺序编辑页面中:
下面是代码:
add_action( 'woocommerce_checkout_update_order_meta', 'saving_checkout_cf_data');
function saving_checkout_cf_data( $order_id ) {
$recipient_address = $_POST['recipient_address'];
if ( ! empty( $recipient_address ) )
update_post_meta( $order_id, 'recipient_address', sanitize_text_field( $recipient_address ) );
$recipient_phone_number = $_POST['recipient_phone_number'];
if ( ! empty( $recipient_phone_number ) )
update_post_meta($order_id, 'recipient_phone_number', sanitize_text_field( $recipient_phone_number ) );
}
代码放在活动子主题(或主题)的function.php文件中,也可以放在任何插件文件中。
如果你想像经典的帐单结帐字段一样有一个以_billing…
开头的meta_key
,你只需要在update_post_meta()
中改变它。例如:
update_post_meta( $order_id,'_billing_recipient_address',sanitize_text_field( $recipient_address ) );
但是在这种情况下,这将不会出现在order编辑页面的自定义Fields metabox中。
发布于 2021-11-10 07:48:12
我需要上传多个字段,我使用了如下数组:
add_action('woocommerce_checkout_update_order_meta', 'tu_funcion');
function tu_funcion($order_id)
{
$arrEnv = array('billing_cif', 'despliegue_nombre', 'despliegue_apellido', 'despliegue_correo');
foreach ($arrEnv as $valor) :
if (!empty($_POST[$valor]))
update_post_meta($order_id, $valor, sanitize_text_field($_POST[$valor]));
endforeach; }
在此之前,我介绍他们结帐时使用:
add_filter ('woocommerce_checkout_fields', 'custom_fields_finish_purchase');
function custom_fields_finish_purchase($fields){
$fields['billing']['billing_cif'] = array (
'type' => 'text',
'label' => 'CIF',
'placeholder' => 'Write here the CIF of the company',
'class' => array ('form-row-wide'),
'required' => true,
);
// more fields here...
return $fields;
}
有关订单的详细信息,您可以使用此fook:
add_action('woocommerce_admin_order_data_after_billing_address', 'your_function', 10, 1);
function your_function{
echo '<p><strong>CIF:</strong> ' . get_post_meta($order->get_id(), 'billing_cif', true) . '</p>';}
对于写邮件,你可以使用这个add_action钩子:
'woocommerce_email_after_order_table‘
https://stackoverflow.com/questions/44057357
复制