Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,17 @@ private static function get_contact_order_metadata( $order, $payment_page_url =
$metadata[ Newspack_Newsletters::get_metadata_key( 'payment_page' ) ] = $payment_page_url;

$utm = $order->get_meta( 'utm' );
if ( empty( $utm ) ) {
$utm = [];
// Try the explicit `utm_<name>` meta.
foreach ( WooCommerce_Order_UTM::$params as $param ) {
$param_name = 'utm_' . $param;
$utm_value = $order->get_meta( $param_name );
if ( ! empty( $utm_value ) ) {
$utm[ $param ] = $utm_value;
}
}
}
if ( ! empty( $utm ) ) {
foreach ( $utm as $key => $value ) {
$metadata[ Newspack_Newsletters::get_metadata_key( 'payment_page_utm' ) . $key ] = $value;
Expand Down Expand Up @@ -357,16 +368,16 @@ private static function get_contact_order_metadata( $order, $payment_page_url =

// Clear out any payment-related fields that don't relate to the current order.
$payment_fields = array_keys( Newspack_Newsletters::get_payment_metadata_fields() );
$utm_meta_field = Newspack_Newsletters::get_metadata_key( 'payment_page_utm' );
foreach ( WooCommerce_Order_UTM::$params as $param ) {
if ( ! isset( $metadata[ $utm_meta_field . $param ] ) ) {
$metadata[ $utm_meta_field . $param ] = '';
}
}
foreach ( $payment_fields as $meta_key ) {
$meta_field = Newspack_Newsletters::get_metadata_key( $meta_key );
if ( ! isset( $metadata[ $meta_field ] ) ) {
if ( 'payment_page_utm' === $meta_key ) {
foreach ( WooCommerce_Order_UTM::$params as $param ) {
$metadata[ $meta_field . $param ] = '';
}
} else {
$metadata[ $meta_field ] = '';
}
if ( ! isset( $metadata[ $meta_field ] ) && 'payment_page_utm' !== $meta_key ) {
$metadata[ $meta_field ] = '';
}
}

Expand Down
6 changes: 5 additions & 1 deletion tests/mocks/wc-mocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function get_billing_email() {

class WC_Order {
public $data = [ 'items' => [] ];
public $meta = [];
public function __construct( $data ) {
global $orders_database;
$data['id'] = count( $orders_database ) + 1;
Expand All @@ -88,6 +89,9 @@ public function __construct( $data ) {
update_user_meta( $customer->get_id(), 'wc_total_spent', $total_spent );
// Add the order to the mock DB.
}
if ( isset( $data['meta'] ) ) {
$this->meta = $data['meta'];
}
$orders_database[] = $this;
}
public function get_id() {
Expand All @@ -97,7 +101,7 @@ public function get_customer_id() {
return $this->data['customer_id'];
}
public function get_meta( $field_name ) {
return null;
return isset( $this->meta[ $field_name ] ) ? $this->meta[ $field_name ] : '';
}
public function has_status( $statuses ) {
return in_array( $this->data['status'], $statuses );
Expand Down
39 changes: 39 additions & 0 deletions tests/unit-tests/newsletters.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,43 @@ public function test_newsletters_master_list_feature() {
'Expected to get the same lists back when no master list is set.'
);
}

/**
* Contact handling.
*/
public static function test_newsletters_contact_handling() {
$utm_params = [
'campaign' => 'test_campaign',
'content' => 'test_content',
];
$contact = [
'email' => 'test@email.com',
'name' => 'John Doe',
'metadata' => [
'NP_Payment Page' => '/donate/?utm_campaign=' . $utm_params['campaign'] . '&utm_content=' . $utm_params['content'],
'NP_Payment UTM: campaign' => $utm_params['campaign'],
'NP_Membership Status' => 'Donor',
'NP_Product Name' => 'Donate: One-Time',
'NP_Last Payment Amount' => '20.00',
'NP_Last Payment Date' => '2024-08-27',
'NP_Payment UTM: source' => '',
'NP_Payment UTM: medium' => '',
'NP_Payment UTM: term' => '',
'NP_Payment UTM: content' => $utm_params['content'],
'NP_Current Subscription Start Date' => '',
'NP_Current Subscription End Date' => '',
'NP_Billing Cycle' => '',
'NP_Recurring Payment' => '',
'NP_Next Payment Date' => '',
'NP_Total Paid' => '109.00',
'NP_Account' => '492',
'NP_Registration Date' => '2024-08-26',
],
];
$normalized_contact = Newspack_Newsletters::normalize_contact_data( $contact );
self::assertEquals( $contact['email'], $normalized_contact['email'] );
self::assertEquals( $utm_params['campaign'], $normalized_contact['metadata']['NP_Payment UTM: campaign'] );
self::assertEquals( $utm_params['content'], $normalized_contact['metadata']['NP_Payment UTM: content'] );
self::assertEquals( '', $normalized_contact['metadata']['NP_Payment UTM: term'] );
}
}
38 changes: 33 additions & 5 deletions tests/unit-tests/woocommerce-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ public function set_up() { // phpcs:ignore Squiz.Commenting.FunctionComment.Miss
* Test payment metadata extraction - basic shape of the data.
*/
public function test_woocommerce_connection_payment_metadata_basic() {
$customer = new WC_Customer( self::$user_id );
$order_data = [
'customer_id' => self::$user_id,
'status' => 'completed',
'total' => 50,
'meta' => [
'utm' => [
'source' => 'test_source',
'campaign' => 'test_campaign',
'term' => 'test_term',
'content' => 'test_content',
],
],
];
$order = \wc_create_order( $order_data );
$payment_page_url = 'https://example.com/donate';
Expand All @@ -63,11 +70,11 @@ public function test_woocommerce_connection_payment_metadata_basic() {
'NP_Product Name' => '',
'NP_Last Payment Amount' => '$' . $order_data['total'],
'NP_Last Payment Date' => $today,
'NP_Payment UTM: source' => '',
'NP_Payment UTM: source' => 'test_source',
'NP_Payment UTM: medium' => '',
'NP_Payment UTM: campaign' => '',
'NP_Payment UTM: term' => '',
'NP_Payment UTM: content' => '',
'NP_Payment UTM: campaign' => 'test_campaign',
'NP_Payment UTM: term' => 'test_term',
'NP_Payment UTM: content' => 'test_content',
'NP_Current Subscription Start Date' => '',
'NP_Current Subscription End Date' => '',
'NP_Billing Cycle' => '',
Expand All @@ -82,6 +89,27 @@ public function test_woocommerce_connection_payment_metadata_basic() {
);
}

/**
* Test payment metadata extraction - with different location of UTM meta.
* Newspack will set the 'utm' order meta, but when importing data, a one-to-one relation
* might be needed. In such a case, a field in the imported data would correspond to
* a string meta field, instead of the UTM params being stored as serialized values.
*/
public function test_woocommerce_connection_payment_metadata_utm() {
$order_data = [
'customer_id' => self::$user_id,
'status' => 'completed',
'total' => 50,
'meta' => [
'utm_source' => 'test_source',
'utm_campaign' => 'test_campaign',
],
];
$order = \wc_create_order( $order_data );
$contact_data = WooCommerce_Connection::get_contact_from_order( $order );
$this->assertEquals( 'test_source', $contact_data['metadata']['NP_Payment UTM: source'] );
$this->assertEquals( 'test_campaign', $contact_data['metadata']['NP_Payment UTM: campaign'] );
}

/**
* Test payment metadata extraction using a failed order.
Expand Down