简体   繁体   English

WooCommerce 不应该在订单是订阅父时发送订单完成邮件

[英]WooCommerce should NOT send order complete mail when order is subscription parent

I try to unsent the WooCommerce default order complete mail when a customer buys a subscription.当客户购买订阅时,我尝试取消发送 WooCommerce 默认订单完成邮件。 WooCommerce creates a parent order for that subscription. WooCommerce 为该订阅创建父订单。 When we close that parent order, the customer will get the order completed mail.当我们关闭该父订单时,客户将收到订单完成邮件。 Then we try no block but only on parent order for subscriptions.然后我们尝试不阻止,但仅针对订阅的父订单。

I found this answer but it's for renewal completed mails.我找到了这个答案,但它用于更新已完成的邮件。

There is an info order_type which should store the info if it is a subscription parent order or not, how can that value be accessed?有一个 info order_type应该存储信息,如果它是订阅父订单或不是,如何访问该值?

There is maybe also a way to check via wcs_order_contains_subscription() if it is a parent subscription order.如果它是父订阅订单,也许还有一种方法可以通过wcs_order_contains_subscription()检查。 ( https://docs.woocommerce.com/document/subscriptions/develop/functions/order-cart-functions/ ) https://docs.woocommerce.com/document/subscriptions/develop/functions/order-cart-functions/

Something like this: wcs_order_contains_subscription( $order, $order_type )像这样的东西: wcs_order_contains_subscription( $order, $order_type )

Update: With the new information this is the current status:更新:使用新信息,这是当前状态:

// Conditionally remove action to send order completed email for subscription parent order

function unhook_order_complete_email_subscription_parent($email_class) {

    // Check whether to send the parent order completed email or not
    $send_parent_completed_email = order_type(parent);

    if (!$send_parent_completed_email) {

        // Remove action to prevent email from being sent
        remove_action(
            "woocommerce_order_status_completed_notification",
            array(
                $email_class->emails["WC_Email_Customer_Completed_Order"],
                "trigger"
            )
        );
    }
}

add_action("woocommerce_order_status_changed", "unhook_order_complete_email_subscription_parent");

There is no function like order_type() .没有像order_type()这样的 function 。 This should fix your problem:这应该可以解决您的问题:

// Conditionally remove action to send order completed email for subscription parent order

function unhook_order_complete_email_subscription_parent($order_id) {
    $email_class = wc()->mailer()->get_emails();
    
    // Check whether to send the parent order completed email or not
    $order = wc_get_order($order_id);
    if ($order && !$order->get_parent_id()) {
        // Remove action to prevent email from being sent
        remove_action(
            "woocommerce_order_status_completed_notification",
            array(
                $email_class->emails["WC_Email_Customer_Completed_Order"],
                "trigger"
            )
        );
    }
}

add_action("woocommerce_order_status_changed", "unhook_order_complete_email_subscription_parent");

EDIT:编辑:

This is a better approach that works.这是一种更好的方法。 get_parent_id() will return 0 if an order is a parent, else it will return the parent_id if it is a child.如果订单是父订单, get_parent_id()将返回 0,否则如果订单是子订单,它将返回parent_id

function enable_email($order) {
    // We have to restore the configuration of what we disabled after we are done with our hack
    if ($order->get_parent_id() != 0 && ($status = get_transient( "tmp_completed_order_email" )) !== false) {
        wc()->mailer()->emails['WC_Email_Customer_Completed_Order']->enabled = $status;        
    }
}

function disable_email($order)
{
    if ($order->get_parent_id() != 0) { // It is a child
        // Store the current configuration in a temporary place.
        $current_status = wc()->mailer()->emails['WC_Email_Customer_Completed_Order']->enabled;

        // Everything should be done in 10 seconds. Lets keep the data till then
        set_transient( 'tmp_completed_order_email', $current_status, 10 );

        // Disabled completed order email
        wc()->mailer()->emails['WC_Email_Customer_Completed_Order']->enabled = 'no';
    }
}
add_action("woocommerce_before_order_object_save", "disable_email");
add_action("woocommerce_after_order_object_save", "enable_email");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM