根据产品下拉菜单向企业发送电子邮件

时间:2020-10-15 20:40:55

标签: php html wordpress woocommerce

我有一个Woocommerce产品,如果要从该产品页面上的下拉字段中选择标准的Woocommerce订单确认电子邮件,我希望将其发送给网站管理员和企业。

背包$ 50

选择一个选项

  • 业务至上
  • 第二商业
  • 商业第三

如果他们选择 Business First ,则订单确认电子邮件将发送到 Business First的电子邮件电子邮件info@businessfirst.com

这是我到目前为止的代码

HTML

<select name="wapf[field_5f87355d578fd]" class="wapf-input" data-is-required="" data-field-id="5f87355d578fd">
    <option value="">Choose an option</option>
    <option value="business_1">Business 1</option>
    <option value="business_2">Business 2</option>
    <option value="business_3">Business 3</option>
</select>
       

PHP

我已将其添加到functions.php

function sv_conditional_email_recipient( $recipient, $order ) {

    // Bail on WC settings pages since the order object isn't yet set yet
    // Not sure why this is even a thing, but shikata ga nai
    $page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
    if ( 'wc-settings' === $page ) {
        return $recipient; 
    }

    // just in case
    if ( ! $order instanceof WC_Order ) {
        return $recipient; 
    }

    $items = $order->get_items();

    // check if a shipped product is in the order   
    foreach ( $items as $item ) {
        $product = $order->get_product_from_item( $item );

        // adds our extra recipient if customer selected Business 1 from the dropdown on this product order
        if (select(name="wapf[field_5f87355d578fd]") value == "business_1") {
            $recipient .= ', info@businessfirst.com';
            return $recipient;
        }

        // adds our extra recipient if customer selected Business 2 from the dropdown on this product order
        if (select(name="wapf[field_5f87355d578fd]") value == "business_2") {
            $recipient .= ', info@businesssecond.com';
            return $recipient;
        }
    }

    return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );

我认为我已经接近了,但是我不太正确。

1 个答案:

答案 0 :(得分:0)

您是否只希望在他们选择业务1时将其邮寄给他们,或者他们也应能够邮寄业务2或3?如果是这样,则需要将if语句放入函数中,其他情况下应加上两个else if。

此外,如果您试图获取字段的值,则那里不能有空格。 select(name="wapf[field_5f87355d578fd]") value

This blogpost准确描述了我的想法。