WooCommerce结账页面-将自定义字段复选框值输入电子邮件

时间:2020-04-09 作者:woodwork3r

我已在签出页面中添加了一个复选框:

add_action(\'woocommerce_after_order_notes\', \'client_already_field\');

function client_already_field( $checkout ) {

    echo \'<div id="client-already-field"><h3>\'.__(\'CLIENT ALREADY? \').\'</h3>\';

    woocommerce_form_field( \'client_already_checkbox_yes\', array(
        \'type\'          => \'checkbox\',
        \'class\'         => array(\'input-checkbox-yes\'),
        \'label\'         => __(\'YES!\'),
        \'required\'  => false,
        ), $checkout->get_value( \'client_already_checkbox_yes\' ));

    echo \'</div>\';
}
并将其保存到order meta(这两个函数都起作用):

//1
add_action(\'woocommerce_checkout_update_order_meta\', \'client_already_order_meta_yes\');

function client_already_order_meta_yes( $order_id ) {
    if ($_POST[\'client_already_checkbox_yes\']) update_post_meta( $order_id, \'Client Already:Yes\', esc_attr($_POST[\'client_already_checkbox_yes\']));
}

// 2
function client_already_order_meta_yes( $order_id ) {
    if( !empty( $_POST[\'client_already_checkbox_yes\'] ) && $_POST[\'client_already_checkbox_yes\'] == 1 )
        update_post_meta( $order_id, \'Client Already:YES\', 1 ); 
}
但当我勾选方框并完成交易时,收到的管理订单电子邮件中没有显示任何内容。我尝试过的事情:

// 1
add_filter( \'woocommerce_email_order_meta_fields\', \'client_already_email\', 10, 3 );

function client_already_email( $fields, $sent_to_admin, $order_obj ) {

    $is_yes = get_post_meta( $order_obj->get_order_number(), \'client_already_checkbox_yes\', true );

    if( empty( $is_yes ) )
        return $fields;

    $fields[\'client_already_checkbox_yes\'] = array(
        \'label\' => __( \'Client Already\' ),
        \'value\' => \'Yes\'
    );
    return $fields;
}

// 2
function client_already_email( $fields, $sent_to_admin, $order_obj ) {

    $is_yes = get_post_meta( $order_obj->get_order_number(), \'client_already_checkbox_yes\', true );

    if( empty( $is_yes ) )
        return $fields;

    echo "Client Already? Yes";

    );
    return $fields;
}

// 3
function client_already_email( $fields, $sent_to_admin, $order ) {
    $fields[\'client_already_checkbox_yes\'] = array(
        \'label\' => __( \'Client Already? YES\' ),
        \'value\' => get_post_meta( $order->id, \'client_already_checkbox_yes\', true ),
    );
    return $fields;
}
我也尝试过其他一些类似的事情。我知道我缺少了从复选框中正确检索和评估值,并在知道值后将其粘贴到电子邮件中的内容。

我已经在这个结帐页面中添加了一些其他字段,这些字段将保存到meta并添加到交易电子邮件中,我不确定这里缺少了什么。

我对PHP不太感兴趣,所以我正在熟悉但不太舒服的领域中行走。

1 个回复
SO网友:woodwork3r

我不知道我怎么会错过答案,这比我尝试的东西要简单得多。

从…起the docs:

/* To use: 
1. Add this snippet to your theme\'s functions.php file
2. Change the meta key names in the snippet
3. Create a custom field in the order post - e.g. key = "Tracking Code" value = abcdefg
4. When next updating the status, or during any other event which emails the user, they will see this field in their email
*/
add_filter(\'woocommerce_email_order_meta_keys\', \'my_custom_order_meta_keys\');

function my_custom_order_meta_keys( $keys ) {
     $keys[] = \'Tracking Code\'; // This will look for a custom field called \'Tracking Code\' and add it to emails
     return $keys;
}
就我而言

add_filter(\'woocommerce_email_order_meta_keys\', \'client_already_email_yes\');

function client_already_email_yes( $keys ) {
     $keys[] = \'Client Already:YES\'; // This will look for a custom field called \'Tracking Code\' and add it to emails
     return $keys;
}
这将从选中的复选框中返回值1,并将其粘贴在电子邮件文本的末尾。虽然不好看,但已经够好了。

相关推荐

将GravityForm数据存储在phpmyadmin(MySQL)中

我目前正在Wordpress上为客户开发一个小型门户,用户必须提交表单(使用Gravity表单创建)。我希望能够在提交时将这些表单中的数据存储到我在phpmyadmin上创建的自定义数据库中。我已经设置了所有的表和结构,但我不知道如何传递数据。我知道我最终不得不使用“gform\\u after\\u submission”挂钩,我只是不知道从哪里开始。谢谢