当订单仅包含来自特定类别的产品时,我试图在订单完成时触发两个操作。这些产品的重量很容易识别为0,这使我可以通过运输方式识别这些订单。我使用此方法两次,以更改管理员电子邮件的收件人并自动完成订单。
当我测试时,它们都完美无缺。但是IRL虽然电子邮件的变化始终是由运输方式触发的,但很多订单并没有自动完成"。这给我的印象是,虽然woocommerce_email_recipient_new_order一直被触发,但woocommerce_thankyou并不是。我不明白为什么,这对我的客户来说是一个真正的问题。
知道可能导致这种情况的原因吗?
(完全披露:我已经提到了这个话题,但是因为函数内部的顺序选择机制更复杂,所以回答的人确定代码的失败来自函数而不是来自woocommerce_thankyou没有正确触发我觉得有了这些非常简单的函数,毫无疑问问题发生在custom_woocommerce_auto_complete_order函数本身之外。)
这两个功能,总是正确触发的功能,不是那个功能:
/**
* 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;
}
$shipping_method = $order->get_shipping_method();
if ( $shipping_method == 'Abonnement' ) {
$recipient = 'newrecip@mail.fr';
}
else {
$recipient = 'oldrecip@mail.fr';
}
return $recipient;
}
/**
* Autocomplete orders with only an 'abo' product
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$shipping_method = $order->get_shipping_method();
if ( $shipping_method == 'Abonnement' ) {
$order->update_status( 'completed' );
}
}