Help with custom function to change shipping address & order status
-
I have a custom WooCommerce function that works but is a little buggy. I could really use some help figuring out a better way to do this.
The goal: after an order is placed through our POS system it defaults to ‘Processing’. I need it to add a default shipping address and then change to ‘Completed’. I need the address to change first, and then change the status. Here’s what I’ve come up with so far:
add_action( 'woocommerce_payment_complete_order_status_processing', 'alter_shipping_fields_after_order', 999, 1); function alter_shipping_fields_after_order( $order_id ) { $order = wc_get_order( $order_id ); $ordertype = $order->get_meta('wc_pos_order_type'); $orderaddress = $order->get_shipping_address_1(); $ordercity = $order->get_shipping_city(); $orderstate = $order->get_shipping_state(); $orderpostcode = $order->get_shipping_postcode(); $ordercountry = $order->get_shipping_country(); if($ordertype == "POS" ) { if ( empty( $orderaddress ) ) { update_post_meta( $order_id, '_shipping_address_1', 'street' ); } if ( empty( $ordercity ) ) { update_post_meta( $order_id, '_shipping_city', 'city'); } if ( empty( $orderstate ) ) { update_post_meta( $order_id, '_shipping_state', 'state'); } if ( empty( $orderpostcode ) ) { update_post_meta( $order_id, '_shipping_postcode', 'zip'); } update_post_meta( $order_id, '_shipping_country', 'country'); $order->update_status( 'completed' ); } }
The problem is that when the order is placed it says Order Failed through the POS. However, when we go to our orders list it has successfully completed and the address has been added.
Any ideas why it’s saying order failed when actually it was successful?
(We can have POS orders default to ‘completed’ after orders are placed, but because I need to add an address to the order first we’re having it default to processing.)
- The topic ‘Help with custom function to change shipping address & order status’ is closed to new replies.