Skip to content

Commit

Permalink
Fix: not removing documents meta on Woo subcription renewal (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamadNateqi authored Apr 12, 2024
1 parent 94020f0 commit bb14362
Showing 1 changed file with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public function __construct() {
if ( class_exists( 'WC_Subscriptions' ) ) {
if ( version_compare( \WC_Subscriptions::$version, '2.0', '<' ) ) {
add_action( 'woocommerce_subscriptions_renewal_order_created', array( $this, 'woocommerce_subscriptions_renewal_order_created' ), 10, 4 );
} elseif ( version_compare( \WC_Subscriptions::$version, '2.5', '<' ) ) {
} elseif ( version_compare( \WC_Subscriptions::$version, '4.7.0', '<' ) ) {
add_filter( 'wcs_renewal_order_meta', array( $this, 'wcs_renewal_order_meta' ), 10, 3 );
add_filter( 'wcs_resubscribe_order_meta', array( $this, 'wcs_renewal_order_meta' ), 10, 3 );
} else {
add_filter( 'wc_subscription_renewal_order_data', array( $this, 'wcs_renewal_order_meta' ), 10, 3 );
add_filter( 'wc_subscriptions_renewal_order_data', array( $this, 'wcs_renewal_order_meta' ), 10, 3 );
add_filter( 'wc_subscriptions_resubscribe_order_data', array( $this, 'wcs_renewal_order_meta' ), 10, 3 );
}
}
Expand Down Expand Up @@ -91,35 +91,41 @@ public function reset_invoice_data ( $order ) {
/**
* Removes documents meta from WooCommerce Subscriptions renewal order
*/
public function wcs_renewal_order_meta ( $meta, $to_order, $from_order ) {
if ( ! empty( $meta ) ) {
$documents = WPO_WCPDF()->documents->get_documents();
$documents_meta = array();

foreach ( $documents as $document ) {
$document_data_keys = apply_filters( 'wpo_wcpdf_delete_document_data_keys', array(
'settings',
'date',
'date_formatted',
'number',
'number_data',
'notes',
'exists',
), $document );

$document_meta = array_map( function ( $data_key ) use ( $document ) {
return "_wcpdf_{$document->slug}_{$data_key}";
}, $document_data_keys );
$document_meta[] = "_wcpdf_formatted_{$document->slug}_number"; // legacy meta key
$documents_meta = array_merge( $documents_meta, $document_meta );
}
public function wcs_renewal_order_meta( $meta, $to_order, $from_order ) {
if ( empty( $meta ) ) {
return $meta;
}

foreach ( $meta as $key => $value ) {
if ( in_array( $value['meta_key'], $documents_meta ) ) {
unset( $meta[$key] );
}
$documents = WPO_WCPDF()->documents->get_documents();
$documents_meta = array();

foreach ( $documents as $document ) {
$document_data_keys = apply_filters( 'wpo_wcpdf_delete_document_data_keys', array(
'settings',
'date',
'date_formatted',
'number',
'number_data',
'notes',
'exists',
), $document );

$document_meta = array_map( function ( $data_key ) use ( $document ) {
return "_wcpdf_{$document->slug}_{$data_key}";
}, $document_data_keys );
$document_meta[] = "_wcpdf_formatted_{$document->slug}_number"; // legacy meta key
$documents_meta = array_merge( $documents_meta, $document_meta );
}

foreach ( $meta as $key => $value ) {
// The old deprecated hook (`wcs_renewal_order_meta`) sends $meta with this data structure: array(... , array( "meta_key":"_wcpdf_invoice_number","meta_value":"158" ), ...)
// The new hook (`wc_subscriptions_renewal_order_data`) sends $meta with this data structure: array(... ,"_wcpdf_invoice_number":"158", ...)
$meta_key = is_array( $value ) ? ( $value['meta_key'] ?? null ) : $key;
if ( in_array( $meta_key, $documents_meta, true ) ) {
unset( $meta[ $key ] );
}
}

return $meta;
}

Expand Down

0 comments on commit bb14362

Please sign in to comment.