Good Day,
This is my first time using stackoverflow, nice to meet you all.
Anywho, I'm writing a plugin for WooCommerce and I am automatically refunding an order when it is cancelled. My code works fine when I manually execute it in a separate file without the hook, however, with my hook it seems to not execute. I'm doing the following:
add_action('woocommerce_cancelled_order','change_status_to_refund', 10, 1);
function change_status_to_refund( $order_id ) {
wp_redirect( home_url() );
$order = new WC_Order( $order_id );
if( 'refunded' == $order->get_status() ) {
return false;
}
if(!($order->is_paid())) {
return false;
}
$noRefundLimit = 24 * 60; //in minutes until booking
$customer_orders = get_posts( array(
'numberposts' => 1,
'post_parent' => $order_id,
'post_type' => 'wc_booking', // WC orders post type
'post_status' => 'paid, complete' // Only paid, completed bookings
) );
$bookingId = current($customer_orders)->ID;
$bookingStart = current(get_post_meta($bookingId, "_booking_start"));
$time = (new DateTime($bookingStart, new
DateTimeZone("America/Los_Angeles")))->getTimestamp();
$nowTime = (new DateTime())->getTimestamp();
$difference = round(($time - $nowTime)/60);//in minutes
if($difference >= $noRefundLimit) {
$refundPercentage = 1; //how much will we give back? fraction of 1.
// Get Items
$order_items = $order->get_items();
// Refund Amount
$refund_amount = 0;
// Prepare line items which we are refunding
$line_items = array();
if ( $order_items ) {
foreach( $order_items as $item_id => $item ) {
$refund_amount += $item->get_total();
}
}
$refund_amount = ($refund_amount * $refundPercentage);
$refund_reason = "Order Cancelled";
$refund = wc_create_refund( array(
'amount' => $refund_amount,
'reason' => $refund_reason,
'order_id' => $order_id,
'line_items' => $line_items,
'refund_payment' => true
));
var_dump($refund);
$order->update_status('wc-refunded', 'Order Cancelled And Completely
Refunded');
$order->save();
}
I have the first redirect line added for the sole purpose of testing if it redirects, which it doesn't! Any idea why this hook isn't firing?