php - Woocommerce 自定义字段不保存

标签 php wordpress

我在 woocommerce 中有 3 个自定义字段:

事务编号,发票编号和处理案件的律师(代码在后面,如果你不想看到它就向下滚动,它不是那么重要)

add_action('woocommerce_after_order_notes', 'matter_reference_number_func');

function matter_reference_number_func( $checkout ) {

    echo '<div id="matter_ref"><h3>'.__('Matter reference number').'</h3>';

    woocommerce_form_field( 'matter_reference_number', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'placeholder'       => __('Matter reference number'),
        ), $checkout->get_value( 'matter_ref_num' ));

    echo '</div>';

}

add_action('woocommerce_after_order_notes', 'invoice_number_func');

function invoice_number_func( $checkout ) {

    echo '<div id="inv_num"><h3>'.__('Invoice number').'</h3>';

    woocommerce_form_field( 'invoice_number', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'placeholder'       => __('Invoice number'),
        ), $checkout->get_value( 'invoice_num' ));

    echo '</div>';

}

add_action('woocommerce_after_order_notes', 'sol_deal_func');

function sol_deal_func( $checkout ) {

    echo '<div id="sol_deal"><h3>'.__('Solicitor dealing with matter').'</h3>';

    woocommerce_form_field( 'matter_reference_number', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'placeholder'       => __('Solicitor dealing with matter'),
        ), $checkout->get_value( 'sol_deal' ));

    echo '</div>';

}

但是现在,当我尝试保存这些值时,它们不会出现在给客户的电子邮件中,也不会出现在管理员中,甚至不会出现在确认页面上。根据the docs这应该足以做到这一点;但事实并非如此。

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'matter_ref_num_checkout_field_update_order_meta');

function matter_ref_num_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['matter_ref_num']) update_post_meta( $order_id, 'Matter Reference Number', esc_attr($_POST['matter_ref_num']));
}

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'invoice_num_checkout_field_update_order_meta');

function invoice_num_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['invoice_num']) update_post_meta( $order_id, 'Invoice Number', esc_attr($_POST['invoice_num']));
}

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'sol_deal_checkout_field_update_order_meta');

function sol_deal_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['sol_deal']) update_post_meta( $order_id, 'Solicitor Dealing With Matter', esc_attr($_POST['sol_deal']));
}

最佳答案

您正在使用 Hook 来更新帖子元数据(即帖子 ID 和订单 ID)。如果您检查您的数据库,您应该能够在适当的帖子 ID 元下看到这些字段。

如果你想将它们添加到电子邮件中,你还需要使用另一个 Hook :

woocommerce_email_order_meta_keys

这是来自 woocommerce 文档的引用:

/**
 * Add the field to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');

function my_custom_checkout_field_order_meta_keys( $keys ) {
    $keys[] = 'My Field';
    return $keys;
}

您实际上只需要为每个操作使用一个钩子(Hook)并将您的代码放在那里,例如:

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta');

function custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['matter_ref_num']) update_post_meta( $order_id, 'Matter Reference Number', esc_attr($_POST['matter_ref_num']));
    if ($_POST['invoice_num']) update_post_meta( $order_id, 'Invoice Number', esc_attr($_POST['invoice_num']));
    if ($_POST['sol_deal']) update_post_meta( $order_id, 'Solicitor Dealing With Matter', esc_attr($_POST['sol_deal']));
}

希望这对您有所帮助。

关于php - Woocommerce 自定义字段不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15297223/

相关文章:

php - ReCaptcha 与 Laravel 5 表单类未找到错误

php - 使用 php.ini 设置环境

PHP网页解析到MYSQL数据库

css - 小时文本的透明度

css - 从导航栏中删除元素

php - 如何在wordpress中使用API

php - AJAX 和 jQuery 与 MVC

php - MySQL:如果任何行具有值,则检查整列

php - Wordpress:修改内容功能

html - CSS 永远不会加载。无法编辑我的网站。缓存?