2017-08-28 82 views
1

我需要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"><h2>' . __('Membership') . '</h2>'; 

    woocommerce_form_field('my_field_name', array(
     'type'   => 'select', 
     'class'   => array('wps-drop'), 
     'label'   => __('Membership purchased'), 
     'options'  => array(
      'blank'  => __('Select membership ordered', 'wps'), 
      'premium' => __('Premium Membership', 'wps'), 
      'gold' => __('Gold Membership', 'wps'), 
      'silver' => __('Silver Membership', 'wps'), 
      'bronze' => __('Bronze Membership', 'wps') 
     ) 
    ), $checkout->get_value('my_field_name')); 

    echo '</div>'; 

} 

/** 
* Process the checkout 
*/ 
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); 

function my_custom_checkout_field_process() { 
    // Check if set, if its not set add an error. 
    if (! $_POST['my_field_name'] =='blank') 
     wc_add_notice(__('Please select status.'), 'error'); 
} 

建立的自定義字段然後我根據選擇的值設置電子郵件收據:

add_filter('woocommerce_email_recipient_new_order', 'new_order_conditional_email_recipient', 10, 2); 
function new_order_conditional_email_recipient($recipient, $order) { 

    // Get the order ID (retro compatible) 
    $order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id; 

    // Get the custom field value (with the right $order_id) 
    $my_field_name = get_post_meta($order_id, 'my_field_name', true); 

    if ($my_field_name == "premium") 
     $recipient .= ', [email protected]'; 
    elseif ($my_field_name == "gold") 
     $recipient .= ', [email protected]'; 
    elseif ($my_field_name == "silver") 
      $recipient .= ', [email protected]'; 
    elseif ($my_field_name == "bronze") 
     $recipient .= ', [email protected]'; 

    return $recipient; 
} 

當我使用此代碼,雖然,沒有收據的誰應該會收到他們指定的電子郵件。代碼有什麼問題?

回答

1

您錯過了將選定的自定義字段值保存在訂單元數據中。我也重新審視你的代碼位:

// Add custom checkout field 
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field'); 
function my_custom_checkout_field($checkout) { 
    echo '<div id="my_custom_checkout_field"><h2>' . __('Membership') . '</h2>'; 

    woocommerce_form_field('my_field_name', array(
     'type'  => 'select', 
     'class'  => array('wps-drop'), 
     'label'  => __('Membership purchased'), 
     'required' => true, // Missing 
     'options' => array(
      ''   => __('Select membership ordered', 'wps'), 
      'premium' => __('Premium Membership', 'wps'), 
      'gold'  => __('Gold Membership', 'wps'), 
      'silver' => __('Silver Membership', 'wps'), 
      'bronze' => __('Bronze Membership', 'wps') 
     ) 
    ), $checkout->get_value('my_field_name')); 
    echo '</div>'; 
} 

// Process the checkout 
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); 
function my_custom_checkout_field_process() { 
    // Check if set, if its not set add an error. 
    if (empty($_POST['my_field_name'])) 
     wc_add_notice(__('Please select status.'), 'error'); 
} 

// Save the custom checkout field in the order meta 
add_action('woocommerce_checkout_update_order_meta', 'my_custom_field_checkout_update_order_meta', 10, 1); 
function my_custom_field_checkout_update_order_meta($order_id) { 

    if (! empty($_POST['my_field_name'])) 
     update_post_meta($order_id, 'my_field_name', $_POST['my_field_name']); 
} 

add_filter('woocommerce_email_recipient_new_order', 'new_order_conditional_email_recipient', 10, 2); 
function new_order_conditional_email_recipient($recipient, $order) { 

    // Get the order ID (retro compatibility) 
    $order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id; 

    // Get the custom field value (with the right $order_id) 
    $my_field_name = get_post_meta($order_id, 'my_field_name', true); 

    if ($my_field_name == "premium") 
     $recipient .= ', [email protected]'; 
    elseif ($my_field_name == "gold") 
     $recipient .= ', [email protected]'; 
    elseif ($my_field_name == "silver") 
     $recipient .= ', [email protected]'; 
    elseif ($my_field_name == "bronze") 
     $recipient .= ', [email protected]'; 

    return $recipient; 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

在WooCommerce 3+中測試並正常工作。

正如你將看到收件人取決於客戶選擇的「新秩序」的電子郵件通知正確添加

相關問題