0
我被要求配置特定的WooCommerce行爲,我無法逃避必須使用過濾器。我沒有真正的能力。WooCommerce - 根據產品類別更改訂單狀態和管理員電子郵件收件人
什麼應該發生,確切地說,當一個訂單隻包含來自「abo」類別的產品時,它會自動標記爲「完成」並且管理員郵件被髮送到不同的服務。
我收集了一些代碼示例,更改電子郵件收件人,訂單狀態或根據產品類別進行通用更改。這是我的科學怪人的代碼。電子郵件更改和訂單狀態更改都失敗。
/**
* Change email recipient for admin New Order emails when the order only has products from the 'abo' category
*
* @param string $recipient a comma-separated string of email recipients (will turn into an array after this filter!)
* @param \WC_Order $order the order object for which the email is sent
* @return string $recipient the updated list of email recipients
*/
add_filter('woocommerce_email_recipient_new_order', 'dada_conditional_email_recipient', 10, 2);
function dada_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();
foreach ($items as $item) {
$product = $order->get_product_from_item($item);
// check if there's an "abo" in the order, then if there's anything else.
if (is_product() && has_term('abo', 'product_cat')) {
$abo_in_order = 'true';
}
if (is_product() && has_term('livre', 'product_cat') || has_term('revue', 'product_cat') || has_term('livre', 'product_cat')) {
$abo_alone_in_order = 'false';
}
else {
$abo_alone_in_order = 'true';
}
}
// if there's an 'abo' and nothing else, change the e-mail recipient to [email protected]
if (($abo_in_order == 'true')&&($abo_alone_in_order == 'true')) $recipient = '[email protected]';
return $recipient;
}
/**
* Autocomplete orders with only an 'abo' product
*/
add_filter('woocommerce_payment_complete_order_status', 'dada_abo_order_autocomplete', 10, 2);
function dada_abo_order_autocomplete($order_status, $order_id) {
$order = wc_get_order($order_id);
if ('processing' == $order_status && ('on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status)) {
// just in case
if (! $order instanceof WC_Order) {
return $order_status;
}
$items = $order->get_items();
foreach ($items as $item) {
$product = $order->get_product_from_item($item);
// check if there's an "abo" in the order, then if there's anything else.
if (is_product() && has_term('abo', 'product_cat')) {
$abo_in_order = 'true';
}
if (is_product() && has_term('livre', 'product_cat') || has_term('revue', 'product_cat') || has_term('livre', 'product_cat')) {
$abo_alone_in_order = 'false';
}
else {
$abo_alone_in_order = 'true';
}
}
// if there's an 'abo' and nothing else, change the order status to 'completed'
if (($abo_in_order == 'true')&&($abo_alone_in_order == 'true')) $order_status = 'completed';
}
return $order_status;
}
任何想法問題來自哪裏?
謝謝 喬斯
謝謝,我會嘗試使用「woocommerce_thankyou」掛鉤。然而,對於新的電子郵件收件人而言,這是我開始使用的代碼,所以我猜測它在類別識別部分的某個地方失敗了。 – YearZeroJoss
是的。檢查你的分類邏輯。如果你發現我的答案有幫助,你可以接受它,並upvote它。 –