1

I have a pay later gateway that I have set to drop orders into on hold status. I would like to automate sending these orders an email with 'pay now' link when I change their status to pending payment. This would save allot of page load time since I currently have to view order then change status then trigger 'send customer invoice/order details' then drop back to admin order list for each order using this gateway(I do have 'WooCommerce order status manager' which allows me to change order status to pending in bulk editor, but this has a way of confusing me which orders have been sent an invoice with 'pay link' when I get distracted by something that happens in RL, thus if I had it set up to auto send 'pay link' email upon changing to pending status it would cut 95% of the time it takes currently).

I found Send an Email notification to the admin for pending order status in WooCommerce answer code and I'm trying to bend it to my will.

I changed line 11 to contain 'WC_Email_Customer-Invoice" In an attempt to get this code to send that email with a 'pay now' link, Original code pointed to 'WC_Email_New_Order' I realize that the last couple lines appear to only send this to admin email but I haven't been receiving anything in admin email. I'm pretty new to php but I also think this is only targeted at 'NEW Customer Pending Order' and might not pertain to status change to pending. I need this pay later gateway to drop to on-hold so woo will immediately reduce stock of items associated to order, and with this code:

function reduce_stock_pending($order_id) {
 wc_reduce_stock_levels($order_id);
}
add_action('woocommerce_order_status_pending', 'reduce_stock_pending');

I'm able to move the order to pending status while nulling the auto restock woo does on orders with pending payment. It's not perfect because u can see in order notes that it rapidly increases stock & then immediately takes it back due to code snippet, but it's functional for my application.

While this whole process is bending vanilla woocommerce functionality that everyone seems to agree holds logic to the average woo store, that same logic does not help with how I need to run my store. The orders pushed through this restricted to local payment gateway will be for products and services that will be delivered and installed before customer account settles the bill.

The struggle continues, I am determined to find a way to automate this despite all the claims that this is only a manual function. Any help in the pursuit of this goal will be greatly appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Cranmoot
  • 51
  • 1
  • 6
  • Sorry but all your described needed process should be done by (or in) your custom gateway itself. Also in your code, `['WC_Email_Customer-Invoice'];` will not work as it needs to be `['WC_Email_Customer_Invoice'];` instead. – LoicTheAztec Nov 28 '18 at 21:44

1 Answers1

3

I have great news... it's working this code will allow a pass through invoice later gateway to drop an order into on-hold status where woo will automatically pull stock quantities and keep them away from ineligible customers,

then when you are prepared to invoice the customer, that's when those very few lines at the top combat how when you move an order to the processing status woo likes to put intangible stock numbers back into the store while it waits to verify payment successful, however it does the customer they can't add them to cart. Even though it is reporting them we have stock on product/catalog pages. That may serve for some merchants but not in my situation, now just push them bulk to pending payment, it will automatically send an Email with a 'pay link' so no need for manual sending... It also does not reduce stock a 2nd time all the way through order completion.

Based on Send an Email notification to the admin for pending order status in WooCommerce answer code, where I just replaced WC_Email_New_Order by WC_Email_Customer_Invoice:

// Customer Invoice notification only for "Pending" Order status
add_action( 'woocommerce_checkout_order_processed', 'pending_customer_invoice_notification', 20, 1 );
function pending_customer_invoice_notification( $order_id ) {
    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Only for "pending" order status
    if( ! $order->has_status( 'pending' ) ) return;

    // Get an instance of the WC_Email_New_Order object
    $wc_email = WC()->mailer()->get_emails()['WC_Email_Customer_Invoice'];

    ## -- Customizing Heading, subject (and optionally add recipients)  -- ##
    // Change Subject
    $wc_email->settings['subject'] = __('{site_title} - New customer Pending order  ({order_number}) - {order_date}');

    // Change Heading
    $wc_email->settings['heading'] = __('New customer Pending Order'); 
    // $wc_email->settings['recipient'] .= ',name@email.com'; // Add email recipients (coma separated)

    // Send "New Email" notification (to admin)
    $wc_email->trigger( $order_id );
}

It's working(at least for my application).

I have to give a lot of credit to so many people especially thanks to @LoicTheAztec for the original code and advice.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Cranmoot
  • 51
  • 1
  • 6