Skip Navigation

[Resolved] Function to create custom post/relationship blocking Woo "processing" status

This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by davidS-53 6 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1124302

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? Toolset related or otherwise? Perhaps a better place to tie it into?

Here's the full code:

// Create custom post when placing order
// =============================================================================
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();

// Now, make all the posts

$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 );

$student_post_title = $student_code;

$student_post = array(
'post_title' => $student_post_title,
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'student',
'meta_input' => array(
'wpcf-purchase-school' => $school_code,
'wpcf-purchase-class' => $class_code,
'wpcf-purchase-student' => $student_code,
'wpcf-purchase-count' => '0',
),
);

post_exists( $student_post_title ) or wp_insert_post( $student_post );

$class_post_title = $class_code;

$class_post = array(
'post_title' => $class_post_title,
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'class',
'meta_input' => array(
'wpcf-purchase-school' => $school_code,
'wpcf-purchase-class' => $class_code,
'wpcf-purchase-count' => '0',
),
);

post_exists( $class_post_title ) or wp_insert_post( $class_post );

$school_post_title = $school_code;

$school_post = array(
'post_title' => $school_post_title,
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'school',
'meta_input' => array(
'wpcf-purchase-school' => $school_code,
'wpcf-purchase-count' => '0',
),
);

post_exists( $school_post_title ) or wp_insert_post( $school_post );

// Now connect it up

$school_post_id = post_exists( $school_post_title );
if ( ! $school_post_id ) {
$school_post_id = wp_insert_post( $school_post );
}

$class_post_id = post_exists( $class_post_title );
if ( ! $class_post_id ) {
$class_post_id = wp_insert_post( $class_post );
}

$student_post_id = post_exists( $student_post_title );
if ( ! $student_post_id ) {
$student_post_id = wp_insert_post( $student_post );
}

$fundraiser_post_id = post_exists( $fundraiser_post_title );
if ( ! $fundraiser_post_id ) {
$fundraiser_post_id = wp_insert_post( $fundraiser_post );
}

toolset_connect_posts( 'school-class', $school_post_id, $class_post_id );
toolset_connect_posts( 'class-student', $class_post_id, $student_post_id );
toolset_connect_posts( 'student-fundraiser-purchase', $student_post_id, $fundraiser_post_id );
toolset_connect_posts( 'school-fundraiser-purchase', $school_post_id, $fundraiser_post_id );

// Create running totals

$student_total_value = get_post_meta( $student_post_id, 'wpcf-purchase-total', true );
$student_total_count = get_post_meta( $student_post_id, 'wpcf-purchase-count', true );
$running_total = $student_total_value + $purchase_value;
$running_count = $student_total_count + 1;
update_post_meta( $student_post_id, 'wpcf-purchase-total', $running_total );
update_post_meta( $student_post_id, 'wpcf-purchase-count', $running_count );

$running_total = 0;
$running_count = 0;

$class_total_value = get_post_meta( $class_post_id, 'wpcf-purchase-total', true );
$class_total_count = get_post_meta( $class_post_id, 'wpcf-purchase-count', true );
$running_total = $class_total_value + $purchase_value;
$running_count = $class_total_count + 1;
update_post_meta( $class_post_id, 'wpcf-purchase-total', $running_total );
update_post_meta( $class_post_id, 'wpcf-purchase-count', $running_count );

$running_total = 0;
$running_count = 0;

$school_total_value = get_post_meta( $school_post_id, 'wpcf-purchase-total', true );
$school_total_count = get_post_meta( $school_post_id, 'wpcf-purchase-count', true );
$running_total = $school_total_value + $purchase_value;
$running_count = $school_total_count + 1;
update_post_meta( $school_post_id, 'wpcf-purchase-total', $running_total );
update_post_meta( $school_post_id, 'wpcf-purchase-count', $running_count );

$running_total = 0;
$running_count = 0;

}

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

#1124503

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi David

You are using the woocommerce_payment_complete_order_status hook, but unless I'm mistaken this is a filter (which needs a return value) and you are treating it as an action (and not returning anything).

hidden link

#1128155

Yeah, that was it. Cheers!