Daltron
Daltron

Reputation: 1846

Wordpress/Woocommerce: After creating order programmatically, I'd like to also send an invoice programmatically... how?

So, my website involves a booking system and the flow looks like this:

  1. Guest selects dates for booking a product (location).
  2. Upon submitting the request, a message is sent to the host for review.
  3. If accepted, I want the order to be payed by the guest. // my issues are here

I have successfully created orders. Ideally, I'd like to be able to send the guest to their "Cart" page, with the booking order they placed (ONLY AFTER, the host has approved the booking). This way, everything can be handled by WooCommerce except the initial ACCEPT or DECLINE by the host.

Question:

  1. Do I need to add the order to the user's cart when this process starts (step 1)? If so, how do I disable payment until the host has approved the order?

Code:

On the initial booking page:

$y_booking_order = wc_create_order();
$y_booking_order->add_product( get_product( $san_y_id ), $san_num_days);
$y_booking_order->calculate_totals();
$y_booking_order->update_status('on-hold');
update_post_meta($y_booking_order->id, '_arrival_date', $san_date_arr);
update_post_meta($y_booking_order->id, '_departure_date', $san_date_dep);
update_post_meta($y_booking_order->id, '_request_sender', $san_user_id);
update_post_meta($y_booking_order->id, '_customer_user', get_current_user_id());
// do I add to cart here? If so, how to stop guest from paying until after host approves?

So far, once the order is created, I'm just letting the host change the status of the order from 'on-hold' to 'pending'. Now I just want to let the guest pay...

Thoughts?

Upvotes: 4

Views: 2200

Answers (1)

Kaji
Kaji

Reputation: 2330

If you're not collecting a pre-authorization of some sort, 'pending' is where the status should be.

As far as sending out the invoice goes, digging through WooCommerce's code and reorganizing it a bit to trim the fat yields the following function, which should allow you to programmatically send out an invoice e-mail in the same manner as if you had told it to send the invoice from the menu when editing the order:

function send_invoice_email($post_id) {
    $order = wc_get_order($post_id);

    wc_switch_to_site_locale();

    do_action('woocommerce_before_resend_order_emails', $order);

    // Ensure gateways are loaded in case they need to insert data into the emails.
    WC()->payment_gateways();
    WC()->shipping();

    // Load mailer.
    $mailer = WC()->mailer();
    $email_to_send = 'customer_invoice';
    $mails = $mailer->get_emails();

    if (!empty($mails)) {
        foreach ($mails as $mail) {
            if ($mail->id == $email_to_send) {
                $mail->trigger($order->get_id(), $order);
                $order->add_order_note( sprintf( __('%s email notification manually sent.', 'woocommerce'), $mail->title), false, true);
            }
        }
    }

    do_action('woocommerce_after_resend_order_email', $order, $email_to_send);

    wc_restore_locale();
}

Upvotes: 3

Related Questions