【问题标题】:Send Email to Business Based on Product Dropdown Selection根据产品下拉选择向企业发送电子邮件
【发布时间】:2021-01-30 09:31:12
【问题描述】:

我有一个 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 );

我想我已经接近了,但我没有完全正确。

【问题讨论】:

  • “不太正确”是什么意思?有没有抛出错误?
  • 这个过滤器是在哪里定义的? new_order 它不在 WooCommerce 中,也不是一个名为 select() 的函数
  • @HowardE 我刚刚更新了我的代码以反映正确的过滤器。 select() 区域是我有点迷路的地方。我要做的是在购买产品时向我选择的电子邮件地址发送一封额外的新订单电子邮件,其中用户从select dropdown 中选择name="wapf[field_5f87355d578fd]"option,其值为@ 987654329@
  • 您在执行此操作时没有启用调试,因为select 不是函数。这应该会引发某种错误。您需要找到每个订单项目的存储数据的值。这条语句(select(name="wapf[field_5f87355d578fd]") value == "business_1") 永远不会返回true。
  • 整个问题的问题在于产品页面上&lt;select&gt; 的 HTML 输出与产品页面无关,而是 - 数据存储在哪里?它是作为订单项元数据存储的,还是什么?您是使用插件添加此下拉列表还是 ACF?将商品添加到购物车后,它曾在某个时间点 $_POST[ 'wapf[field_5f87355d578fd]'] 的事实不再相关......附加信息存储在哪里?这是您检索该字段所需的内容。所以我会先调试购物车数据,然后是订单数据。

标签: php html wordpress woocommerce


【解决方案1】:

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

此外,如果您尝试获取字段的值,则不能在其中使用空格。 select(name="wapf[field_5f87355d578fd]") value

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

【讨论】:

  • 我还希望能够通过电子邮件发送 Business 2、Business 3 等...
  • 我提到的博客确切地告诉你如何做到这一点。 :)
  • 该死的差不多了。我刚刚更新了它。我认为// adds our extra recipient if there's customer selected Business 1 from the dropdown on any product 下的 if 语句不正确我试图弄清楚如何仅在用户从产品下拉列表中选择一个选项时发送电子邮件。我正在尝试使用的产品在这里:rootmeals.wpengine.com/product/meals-donation 我正在尝试通过 Agency 下拉列表实现此目的的字段。
  • 那么你应该添加一个if语句来检查select字段是否为空,如果不是,使用if语句来检查选择了什么选项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-19
相关资源
最近更新 更多