【问题标题】:WooCommerce - Change order status and admin e-mail recipient according to product categoryWooCommerce - 根据产品类别更改订单状态和管理员电子邮件收件人
【发布时间】:2017-04-14 10:30:01
【问题描述】:

我被要求配置一个特定的 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;
}

知道问题出在哪里吗?

谢谢, 乔斯

【问题讨论】:

    标签: wordpress woocommerce hook-woocommerce


    【解决方案1】:
    1. 您应该实施“woocommerce_thankyou”挂钩来更新订单状态,而不是“woocommerce_payment_complete_order_status”。因为,当任何具有已完成状态的订单下达时,您当前的更新订单状态代码将触发。但您的要求是将状态更改为“已完成”。

    2. 要动态添加新的电子邮件收件人,你应该试试这个钩子:“woocommerce_email_recipient_new_order” 参考这个:WooCommerce send new order email to customer

    【讨论】:

    • 谢谢,我会尝试使用“woocommerce_thankyou”钩子。然而,对于新的电子邮件收件人,这是我开始使用的代码,所以我猜它在类别识别部分的某个地方失败了。
    • 是的。检查您的类别逻辑。如果您觉得我的回答有帮助,您可以接受并点赞。
    猜你喜欢
    • 1970-01-01
    • 2021-04-21
    • 2015-10-22
    • 2019-01-12
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    相关资源
    最近更新 更多