【发布时间】: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()区域是我有点迷路的地方。我要做的是在购买产品时向我选择的电子邮件地址发送一封额外的新订单电子邮件,其中用户从selectdropdown 中选择name="wapf[field_5f87355d578fd]"和option,其值为@ 987654329@ -
您在执行此操作时没有启用调试,因为
select不是函数。这应该会引发某种错误。您需要找到每个订单项目的存储数据的值。这条语句(select(name="wapf[field_5f87355d578fd]") value == "business_1")永远不会返回true。 -
整个问题的问题在于产品页面上
<select>的 HTML 输出与产品页面无关,而是 - 数据存储在哪里?它是作为订单项元数据存储的,还是什么?您是使用插件添加此下拉列表还是 ACF?将商品添加到购物车后,它曾在某个时间点$_POST[ 'wapf[field_5f87355d578fd]']的事实不再相关......附加信息存储在哪里?这是您检索该字段所需的内容。所以我会先调试购物车数据,然后是订单数据。
标签: php html wordpress woocommerce