Skip to content

Commit

Permalink
Merge pull request #190 from ConvertKit/add-custom-field-data-on-purc…
Browse files Browse the repository at this point in the history
…hase-data-event

Send Custom Field Data with Purchase Data
  • Loading branch information
n7studios authored Sep 12, 2024
2 parents b48a15f + 559c738 commit 3e4d795
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ jobs:
- name: Start chromedriver
run: |
export DISPLAY=:99
chromedriver --url-base=/wd/hub &
chromedriver --port=9515 --url-base=/wd/hub &
sudo Xvfb -ac :99 -screen 0 1920x1080x24 > /dev/null 2>&1 & # optional
# Write any secrets, such as API keys, to the .env.dist.testing file now.
Expand Down
65 changes: 65 additions & 0 deletions includes/class-ckwc-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,71 @@ public function send_purchase_data( $order_id, $status_old = 'new', $status_new
// it is never displayed again.
WP_CKWC()->get_class( 'review_request' )->request_review();

// Check if any custom field data needs to be added to the subscriber.
$fields = $this->custom_field_data( $order );
if ( ! count( $fields ) ) {
return $response;
}

// Get subscriber ID by email address.
$subscriber_id = $this->api->get_subscriber_id( $purchase['email_address'] );

// If an error occured fetching the subscriber, add a WooCommerce Order note and bail.
if ( is_wp_error( $subscriber_id ) ) {
$order->add_order_note(
sprintf(
/* translators: %1$s: Error Code, %2$s: Error Message */
__( '[ConvertKit] Purchase Data: Custom Fields: Get Subscriber Error: %1$s %2$s', 'woocommerce-convertkit' ),
$subscriber_id->get_error_code(),
$subscriber_id->get_error_message()
)
);

return $subscriber_id;
}

// If no subscriber could be found, add a WooCommerce Order note and bail.
if ( ! $subscriber_id ) {
$order->add_order_note(
sprintf(
/* translators: %1$s: Error Code, %2$s: Error Message */
__( '[ConvertKit] Purchase Data: Custom Fields: No subscriber found for email address %s', 'woocommerce-convertkit' ),
$purchase['email_address']
)
);

return $subscriber_id;
}

// Update subscriber with custom field data.
$response = $this->api->update_subscriber(
$subscriber_id,
$purchase['first_name'],
$purchase['email_address'],
$fields
);

// If an error occured updating the subscriber, add a WooCommerce Order note.
if ( is_wp_error( $response ) ) {
$order->add_order_note(
sprintf(
/* translators: %1$s: Error Code, %2$s: Error Message */
__( '[ConvertKit] Purchase Data: Custom Fields: Update Subscriber Error: %1$s %2$s', 'woocommerce-convertkit' ),
$response->get_error_code(),
$response->get_error_message()
)
);
}

// Add a note to the WooCommerce Order that the custom fields data sent successfully.
$order->add_order_note(
sprintf(
/* translators: ConvertKit Subscriber ID */
__( '[ConvertKit] Purchase Data: Custom Fields sent successfully: Subscriber ID [%s]', 'woocommerce-convertkit' ),
$subscriber_id
)
);

// Return.
return $response;

Expand Down
55 changes: 55 additions & 0 deletions tests/acceptance/purchase-data/PurchaseDataCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function testSendPurchaseDataWithSimpleProductCheckout(AcceptanceTester $
$I,
[
'send_purchase_data' => true,
'custom_fields' => false,
]
);

Expand All @@ -65,6 +66,60 @@ public function testSendPurchaseDataWithSimpleProductCheckout(AcceptanceTester $
// Confirm that the Transaction ID is stored in the Order's metadata.
$I->wooCommerceOrderMetaKeyAndValueExist($I, $result['order_id'], 'ckwc_purchase_data_sent', 'yes', true);
$I->wooCommerceOrderMetaKeyAndValueExist($I, $result['order_id'], 'ckwc_purchase_data_id', $purchaseDataID, true);

// Confirm that the email address was added to ConvertKit.
$subscriber = $I->apiCheckSubscriberExists($I, $result['email_address'], 'First');

// Confirm the subscriber's custom field data is empty, as no Order to Custom Field mapping was specified
// in the integration's settings.
$I->apiCustomFieldDataIsEmpty($I, $subscriber);
}

/**
* Test that the Customer's purchase is sent to ConvertKit when:
* - The 'Send purchase data to ConvertKit' is enabled in the integration Settings, and
* - The opt in settings are disabled, and
* - The Customer purchases a 'Simple' WooCommerce Product, and
* - The Order is created via the frontend checkout.
*
* @since 1.8.4
*
* @param AcceptanceTester $I Tester.
*/
public function testSendPurchaseDataWithCustomFieldsOnSimpleProductCheckout(AcceptanceTester $I)
{
// Create Product and Checkout for this test.
$result = $I->wooCommerceCreateProductAndCheckoutWithConfig(
$I,
[
'display_opt_in' => false,
'check_opt_in' => false,
'send_purchase_data' => true,
'custom_fields' => true,
]
);

// Confirm that the purchase was added to ConvertKit.
$purchaseDataID = $I->apiCheckPurchaseExists($I, $result['order_id'], $result['email_address'], $result['product_id']);

// Check that the Order's Notes include a note from the Plugin confirming the purchase was added to ConvertKit.
$I->wooCommerceOrderNoteExists($I, $result['order_id'], '[ConvertKit] Purchase Data sent successfully: ID [' . $purchaseDataID . ']');

// Confirm that the Transaction ID is stored in the Order's metadata.
$I->wooCommerceOrderMetaKeyAndValueExist($I, $result['order_id'], 'ckwc_purchase_data_sent', 'yes', true);
$I->wooCommerceOrderMetaKeyAndValueExist($I, $result['order_id'], 'ckwc_purchase_data_id', $purchaseDataID, true);

// Confirm that the email address was now added to ConvertKit.
$subscriber = $I->apiCheckSubscriberExists($I, $result['email_address'], 'First');

// Confirm the subscriber's custom field data exists and is correct.
$I->apiCustomFieldDataIsValid($I, $subscriber);

// Check that the Order's Notes include a note from the Plugin confirming the custom field data was added to ConvertKit.
$I->wooCommerceOrderNoteExists($I, $result['order_id'], '[ConvertKit] Purchase Data: Custom Fields sent successfully: Subscriber ID [' . $subscriber['id'] . ']');

// Unsubscribe the email address, so we restore the account back to its previous state.
$I->apiUnsubscribe($subscriber['id']);
}

/**
Expand Down

0 comments on commit 3e4d795

Please sign in to comment.