2016-01-13 124 views
0

我很难搞清楚如何将新的自定义帐单字段添加到我的管理员通知电子邮件中。我不是一个PHP专家(无论如何),所以我不知道我搞砸了什么,或者它是因为它试图将该字段附加到一般的Woo商务管理通知电子邮件或从预订插件。 我的自定义字段在结算页面上工作正常,它只是将其纳入管理员电子邮件中,并且最好还会放到后端管理订单管理区域中?老实说,我在圣诞节前一直在做这件事,完全失去了我在做什么和在哪里做的事情。Woocommerce预订 - 为管理员电子邮件添加自定义字段

预先感谢您!

这是我的代码。

/** 
* Add the field to the checkout 
**/ 
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field'); 

function my_custom_checkout_field($checkout) { 

    echo '<div id="my_custom_checkout_field"><h3>'.__('Referral Source').'</h3>'; 

    /** 
    * Output the field. This is for 1.4. 
    * 
    * To make it compatible with 1.3 use $checkout->checkout_form_field instead: 

    $checkout->checkout_form_field('my_field_name', array( 
     'type'   => 'text', 
     'class'   => array('my-field-class orm-row-wide'), 
     'label'   => __('Fill in this field'), 
     'placeholder' => __('Enter a number'), 
     )); 
    **/ 
    woocommerce_form_field('my_field_name', array( 
     'type'   => 'text', 
     'class'   => array('my-field-class form-row-wide'), 
     'label'   => __('How did you hear about us?'), 
     'placeholder' => __('Please enter how you were referred to our clinic.'), 
     'required' => true, 
     ), $checkout->get_value('my_field_name')); 

    echo '</div>'; 

    } 

/** 
* Update the user meta with field value 
**/ 
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta'); 
function my_custom_checkout_field_update_user_meta($user_id) { 
    if ($user_id && $_POST['my_field_name']) update_user_meta($user_id, 'my_field_name', esc_attr($_POST['my_field_name'])); 
} 
/** 
* Update the order meta with field value 
**/ 
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta'); 
function my_custom_checkout_field_update_order_meta($order_id) { 
    if ($_POST['my_field_name']) update_post_meta($order_id, 'My Field', esc_attr($_POST['my_field_name'])); 
} 
/** 
* 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[] = 'Referral Source'; 
    return $keys; 
} 

UPDATE:

因此,这里是我现在在哪儿。它接缝工作,但我认为我有一个双倍的功能在那里。我相信最后一个功能是将自定义字段添加到我的电子邮件中的功能。

/** 
* Update the user meta with field value 
**/ 
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta'); 
function my_custom_checkout_field_update_user_meta($user_id) { 
    if ($user_id && $_POST['my_field_name']) update_user_meta($user_id, 'my_field_name', esc_attr($_POST['my_field_name'])); 
} 
/** 
* Update the order meta with field value 
**/ 
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta'); 
function my_custom_checkout_field_update_order_meta($order_id) { 
    if ($_POST['my_field_name']) update_post_meta($order_id, 'Referral Source', esc_attr($_POST['my_field_name'])); 
} 
/** 
* 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[] = 'Referral Source'; 
    return $keys; 
} 


// WooCommerce 2.3+ 
    function my_custom_email_order_meta_fields($fields, $sent_to_admin, $order) { 
     $fields['_some_field'] = array(
        'label' => __('Referral Source'), 
        'value' => get_post_meta($order->id, 'my_field_name', true), 
       ); 
     return $fields; 
    } 
    add_filter('woocommerce_email_order_meta_fields', 'my_custom_email_order_meta_fields', 10, 3); 



add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys'); 

function my_custom_order_meta_keys($keys) { 
    $keys[] = 'Referal Source'; 
    return $keys; 
} 
+0

你不保存任何名为领域'推荐Source',因此,没有什么可以显示。我有一个关于[自定义结帐]的教程(http://www.kathyisawesome.com/woocommerce-customize-checkout-fields/),它应该引导您完成整个过程。 – helgatheviking

回答

0

此方法在WooCommerce 2.3中进行了更新,但它应该与您的方法相兼容。它看起来像在woocommerce_email_order_meta_keys过滤器上添加到阵列的密钥与您更新后的元键名称不匹配返回到基本数组。就像您拥有它,但是您的密钥与您在my_custom_checkout_field_update_order_meta()函数中更新的元键不匹配。

下面是如何在关键2.3+ WC添加到电子邮件:

// WooCommerce 2.3+ 
    function kia_email_order_meta_fields($fields, $sent_to_admin, $order) { 
     $fields['_some_field'] = array(
        'label' => __('Some field'), 
        'value' => get_post_meta($order->id, '_some_field', true), 
       ); 
     $fields['_another_field'] = array(
        'label' => __('Another field'), 
        'value' => get_post_meta($order->id, '_another_field', true), 
       ); 
     return $fields; 
    } 
    add_filter('woocommerce_email_order_meta_fields', 'kia_email_order_meta_fields', 10, 3); 
+0

这也适用于由woo Bookings插件驱动的订单电子邮件吗? – Jenna

+0

基于这个例子,这是否正确?我很抱歉,我的php并不好,(http://imgur.com/ZqeBKnr) – Jenna

+0

它工作吗?这是最终的答案。如果它没有做你想要的东西,那么你可以编辑你的问题来粘贴你正在尝试的新代码,或者这个问题值得它发布一个新问题。代码截图很难使用。如果你没有告诉我们哪部分失败,那么很难调查。 – helgatheviking

相关问题