Created
November 27, 2020 14:15
-
-
Save rvdsteege/559d743326178d8315995dd73327b3a1 to your computer and use it in GitHub Desktop.
Cancel WooCommerce order on cancelled Pronamic Pay payment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'pronamic_payment_status_update', 'pronamic_pay_wc_order_cancel', 15 ); | |
/** | |
* Cancel WooCommerce order on cancelled Pronamic Pay payment. | |
* | |
* @param Payment $payment Payment. | |
* @return void | |
*/ | |
function pronamic_pay_wc_order_cancel( $payment ) { | |
// Check for WooCommerce payment source. | |
if ( 'woocommerce' !== $payment->get_source() ) { | |
return; | |
} | |
// Check for WooCommerce order. | |
$order = \wc_get_order( $payment->get_source_id() ); | |
if ( empty( $order ) ) { | |
return; | |
} | |
// Check for cancelled payment status. | |
if ( 'Cancelled' !== $payment->get_status() ) { | |
return; | |
} | |
// Check if last Pronamic Pay payment which has been created | |
// for the order is the same as the payment being processed. | |
$order_payment_id = (int) $order->get_meta( '_pronamic_payment_id' ); | |
if ( ! empty( $order_payment_id ) && $payment->get_id() !== $order_payment_id ) { | |
return; | |
} | |
// Go ahead, cancel the order with a note. | |
$note = \sprintf( | |
/* translators: 1: payment URL, 2: payment ID, 3: WooCommerce payment method title, 4: Pronamic payment status */ | |
__( '<a href="%1$s">Payment #%2$s</a> via "%3$s" updated to "%4$s".', 'pronamic_ideal' ), | |
\esc_urL( $payment->get_edit_payment_url() ), | |
\esc_html( $payment->get_id() ), | |
\esc_html( $payment->get_meta( 'woocommerce_payment_method_title' ) ), | |
\esc_html( $payment->get_status_label() ) | |
); | |
$order->update_status( 'cancelled', $note ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment