I have a very simple custom gateway plugin intended to store some passive data on Woocommerce orders, no actual processing is taking place. It uses the woocommerce_before_pay_action hook to capture the meta data from the custom woocommerce_form_field payment fields via $_POST and store them in the order meta like this:
function save_cash_custom_fields( $order ){
//If this order had been paid for with cash
if( $order->get_payment_method() === 'ms_cash' ){
//If the cash_paid_by field is set
if( isset( $_POST['cash_paid_by'] ) ){
$order->update_meta_data( 'cash_paid_by', sanitize_text_field( $_POST['cash_paid_by'] ) );
}
//If the cash_received_by field is set
if( isset( $_POST['cash_received_by'] ) ){
$order->update_meta_data( 'cash_received_by', sanitize_text_field( $_POST['cash_received_by'] ) );
}
}
add_action( 'woocommerce_before_pay_action', 'save_cash_custom_fields', 10, 1 );
And it works. Sometimes. It’s being really inconsistent, sometimes it captures the data and other times it doesn’t, there are no errors showing and the fields aren’t even being created in the database the times it doesn’t work.
I’ve stepped through the function and output every step of the way, and it’s always there. No errors are being logged anywhere, and the transaction is completing correctly, just this meta data is not being stored.
Can anyone suggest why this is being intermittent? Is the hook not the best option for this?