我被要求配置特定的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 dada@sotiaf.fr
if ( ($abo_in_order == 'true')&&($abo_alone_in_order == 'true') ) $recipient = 'dada@sotiaf.fr';
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"钩子,而不是"woocommerce_payment_complete_order_status"。因为,当下达任何具有已完成状态的订单时,您当前的更新订单状态代码将变得三重。但您的要求是将状态更改为"已完成"。
-
要动态添加新的电子邮件接收,您应该尝试以下钩子:"woocommerce_email_recipient_new_order"参考这个 : WooCommerce向客户发送新订单电子邮件