I’m tying a function in to woocommerce_payment_complete_order_status that creates a custom post based on whether or not a coupon is used to place an order. The code all works fine- creates the post, payments complete, and the site goes back to the thankyou page, but the order gets stuck on awaiting payment. So somehow running this function is blocking the move from awaiting payment to processing order status, so eventually expires and cancels the order.

Any idea why that might be? Perhaps a better place to tie it into?

Here’s the full code:

function create_fundraiser_purchase_post( $order_status, $order_id ) {

$order = new WC_Order( $order_id );

// Order Data
$order_data = $order->get_data();

// Coupon Data
if( $order->get_used_coupons() ) {
    foreach( $order->get_used_coupons() as $coupon) {
        $coupons_list .= $coupon;
    }
}

// Only make the post if the coupon is for a school 
if (strpos($coupons_list, 'school') !== false) {

    $order_id = $order_data['id'];
    $order_date_created = $order_data['date_created']->date('F j, Y');

    // Break up code into blocks for sorting
    $cleaned_coupon = str_replace("school","",$coupons_list);
    $pieces = explode("-", $cleaned_coupon);
    $school_id = $pieces[0];
    $class_id = $pieces[1];
    $student_id = $pieces[2];

    $school_code = $school_id;
    $class_code = $school_id . '-' . $class_id;
    $student_code = $cleaned_coupon;

    // Make nice title
    $title="Fundraiser Purchase " . $order_id . ' ' . $cleaned_coupon;

    $order_shipping_total = $order_data['shipping_total'];
    $order_total_tax = $order_data['total_tax'];
    $total = $order_data['total'];

    $purchase_value = ( $total - $order_total_tax - $order_shipping_total ) * 0.35;

    $user_id = $order->user_id;

    // Add Woocommerce meta
    $order = wc_get_order( $order_id );
    $order->update_meta_data( 'wpcf-purchase-student', $student_code );
    $order->update_meta_data( 'wpcf-purchase-value', $purchase_value );
    $order->save();

    $fundraiser_post = array(
        'post_title' => $title,
        'post_content' => '',
        'post_status' => 'publish',
        'post_type' => 'fundraiser-purchase',
        'meta_input' => array(
            'wpcf-purchase-date' => $order_date_created,
            'wpcf-purchase-school' => $school_code,
            'wpcf-purchase-class' => $class_code,
            'wpcf-purchase-student' => $student_code,
            'wpcf-purchase-value' => $purchase_value,
            'wpcf-purchase-user-id' => $user_id,
            'wpcf-purchase-order-id' => $order_id,
        ),
    );

    post_exists( $fundraiser_post ) or wp_insert_post( $fundraiser_post );

}
add_action( 'woocommerce_payment_complete_order_status', 'create_fundraiser_purchase_post', 10, 2 );

1 Answer
1

Most likely the reason this is hanging is because hook you are using is not complete. woocommerce_payment_complete_order_status is a hook for a filter but you are using an add_action. The hook for the add_action should be woocommerce_payment_complete_order_status_{$get_status}

resource for filter
http://hookr.io/plugins/woocommerce/3.0.6/filters/woocommerce_payment_complete_order_status/

apply_filters( 'woocommerce_payment_complete_order_status', 'create_fundraiser_purchase_post', 10, 2 );

resource for action
http://hookr.io/plugins/woocommerce/3.0.6/actions/woocommerce_payment_complete_order_status_get_status/

add_action( 'woocommerce_payment_complete_order_status_{$get_status}', 'create_fundraiser_purchase_post', 10, 2 );

Leave a Reply

Your email address will not be published. Required fields are marked *