Skip to content

Commit

Permalink
Merge pull request #193 from ConvertKit/exclude-name-from-address
Browse files Browse the repository at this point in the history
Exclude Name from Billing + Shipping Addresses
  • Loading branch information
n7studios authored Sep 23, 2024
2 parents dd87b60 + 412a466 commit e29c318
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 54 deletions.
47 changes: 28 additions & 19 deletions includes/class-ckwc-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public function init_form_fields() {

$this->form_fields = array(
// Account name.
'account_name' => array(
'account_name' => array(
'title' => __( 'Account Name', 'woocommerce-convertkit' ),
'type' => 'oauth_disconnect',
'label' => __( 'Disconnect', 'woocommerce-convertkit' ),
Expand All @@ -462,15 +462,15 @@ public function init_form_fields() {
),

// Enable/Disable entire integration.
'enabled' => array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce-convertkit' ),
'type' => 'checkbox',
'label' => __( 'Enable ConvertKit integration', 'woocommerce-convertkit' ),
'default' => 'no',
),

// Subscribe.
'event' => array(
'event' => array(
'title' => __( 'Subscribe Event', 'woocommerce-convertkit' ),
'type' => 'select',
'default' => 'pending',
Expand Down Expand Up @@ -508,7 +508,7 @@ public function init_form_fields() {
// The setting name that needs to be checked/enabled for this setting to display. Used by JS to toggle visibility.
'class' => 'enabled subscribe',
),
'subscription' => array(
'subscription' => array(
'title' => __( 'Subscription', 'woocommerce-convertkit' ),
'type' => 'subscription',
'default' => '',
Expand All @@ -517,7 +517,7 @@ public function init_form_fields() {
// The setting name that needs to be checked/enabled for this setting to display. Used by JS to toggle visibility.
'class' => 'enabled subscribe',
),
'name_format' => array(
'name_format' => array(
'title' => __( 'Name Format', 'woocommerce-convertkit' ),
'type' => 'select',
'default' => 'first',
Expand All @@ -534,7 +534,7 @@ public function init_form_fields() {
),

// Custom Field Mappings.
'custom_field_last_name' => array(
'custom_field_last_name' => array(
'title' => __( 'Send Last Name', 'woocommerce-convertkit' ),
'type' => 'custom_field',
'default' => '',
Expand All @@ -543,7 +543,7 @@ public function init_form_fields() {
// The setting name that needs to be checked/enabled for this setting to display. Used by JS to toggle visibility.
'class' => 'enabled subscribe',
),
'custom_field_phone' => array(
'custom_field_phone' => array(
'title' => __( 'Send Phone Number', 'woocommerce-convertkit' ),
'type' => 'custom_field',
'default' => '',
Expand All @@ -552,7 +552,7 @@ public function init_form_fields() {
// The setting name that needs to be checked/enabled for this setting to display. Used by JS to toggle visibility.
'class' => 'enabled subscribe',
),
'custom_field_billing_address' => array(
'custom_field_billing_address' => array(
'title' => __( 'Send Billing Address', 'woocommerce-convertkit' ),
'type' => 'custom_field',
'default' => '',
Expand All @@ -561,7 +561,7 @@ public function init_form_fields() {
// The setting name that needs to be checked/enabled for this setting to display. Used by JS to toggle visibility.
'class' => 'enabled subscribe',
),
'custom_field_shipping_address' => array(
'custom_field_shipping_address' => array(
'title' => __( 'Send Shipping Address', 'woocommerce-convertkit' ),
'type' => 'custom_field',
'default' => '',
Expand All @@ -570,7 +570,16 @@ public function init_form_fields() {
// The setting name that needs to be checked/enabled for this setting to display. Used by JS to toggle visibility.
'class' => 'enabled subscribe',
),
'custom_field_payment_method' => array(
'custom_field_address_exclude_name' => array(
'title' => __( 'Exclude Name from Billing & Shipping Addresses', 'woocommerce-convertkit' ),
'type' => 'checkbox',
'default' => 'no',
'description' => __( 'If enabled, removes the order\'s first name, last name and company name when storing the billing and shipping address above.', 'woocommerce-convertkit' ),

// The setting name that needs to be checked/enabled for this setting to display. Used by JS to toggle visibility.
'class' => 'enabled subscribe',
),
'custom_field_payment_method' => array(
'title' => __( 'Send Payment Method', 'woocommerce-convertkit' ),
'type' => 'custom_field',
'default' => '',
Expand All @@ -579,7 +588,7 @@ public function init_form_fields() {
// The setting name that needs to be checked/enabled for this setting to display. Used by JS to toggle visibility.
'class' => 'enabled subscribe',
),
'custom_field_customer_note' => array(
'custom_field_customer_note' => array(
'title' => __( 'Send Customer Note', 'woocommerce-convertkit' ),
'type' => 'custom_field',
'default' => '',
Expand All @@ -590,7 +599,7 @@ public function init_form_fields() {
),

// Subscribe: Display Opt In Checkbox Settings.
'display_opt_in' => array(
'display_opt_in' => array(
'title' => __( 'Opt-In Checkbox', 'woocommerce-convertkit' ),
'label' => __( 'Display an opt-in checkbox on checkout', 'woocommerce-convertkit' ),
'type' => 'checkbox',
Expand All @@ -605,7 +614,7 @@ public function init_form_fields() {
// The setting name that needs to be checked/enabled for this setting to display. Used by JS to toggle visibility.
'class' => 'enabled subscribe',
),
'opt_in_label' => array(
'opt_in_label' => array(
'title' => __( 'Opt-In Checkbox: Label', 'woocommerce-convertkit' ),
'type' => 'text',
'default' => __( 'I want to subscribe to the newsletter', 'woocommerce-convertkit' ),
Expand All @@ -615,7 +624,7 @@ public function init_form_fields() {
// The setting name that needs to be checked/enabled for this setting to display. Used by JS to toggle visibility.
'class' => 'enabled subscribe display_opt_in',
),
'opt_in_status' => array(
'opt_in_status' => array(
'title' => __( 'Opt-In Checkbox: Default Status', 'woocommerce-convertkit' ),
'type' => 'select',
'default' => 'checked',
Expand All @@ -629,7 +638,7 @@ public function init_form_fields() {
// The setting name that needs to be checked/enabled for this setting to display. Used by JS to toggle visibility.
'class' => 'enabled subscribe display_opt_in',
),
'opt_in_location' => array(
'opt_in_location' => array(
'title' => __( 'Opt-In Checkbox: Display Location', 'woocommerce-convertkit' ),
'type' => 'select',
'default' => 'billing',
Expand All @@ -645,7 +654,7 @@ public function init_form_fields() {
),

// Purchase Data.
'send_purchases' => array(
'send_purchases' => array(
'title' => __( 'Purchase Data', 'woocommerce-convertkit' ),
'label' => __( 'Send purchase data to ConvertKit.', 'woocommerce-convertkit' ),
'type' => 'checkbox',
Expand All @@ -660,7 +669,7 @@ public function init_form_fields() {
// The setting name that needs to be checked/enabled for this setting to display. Used by JS to toggle visibility.
'class' => 'enabled subscribe',
),
'send_purchases_event' => array(
'send_purchases_event' => array(
'title' => __( 'Purchase Data Event', 'woocommerce-convertkit' ),
'type' => 'select',
'default' => 'processing',
Expand Down Expand Up @@ -691,7 +700,7 @@ public function init_form_fields() {
// The setting name that needs to be checked/enabled for this setting to display. Used by JS to toggle visibility.
'class' => 'enabled subscribe send_purchases',
),
'sync_past_orders' => array(
'sync_past_orders' => array(
'title' => __( 'Sync Past Orders', 'woocommerce-convertkit' ),
'label' => __( 'Send old purchase data to ConvertKit i.e. Orders that were created in WooCommerce prior to this Plugin being installed.', 'woocommerce-convertkit' ),
'type' => 'sync_past_orders_button',
Expand All @@ -708,7 +717,7 @@ public function init_form_fields() {
),

// Debugging.
'debug' => array(
'debug' => array(
'title' => __( 'Debug', 'woocommerce-convertkit' ),
'type' => 'checkbox',
'label' => __( 'Write data to a log file', 'woocommerce-convertkit' ),
Expand Down
32 changes: 32 additions & 0 deletions includes/class-ckwc-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,14 @@ private function custom_field_data( $order ) {

$fields = array();

// If the name and company name should be excluded from the billing and shipping address
// fetched using get_formatted_billing_address() / get_formatted_shipping_address(),
// add filters now.
if ( $this->integration->get_option_bool( 'custom_field_address_exclude_name' ) ) {
add_filter( 'woocommerce_order_formatted_billing_address', array( $this, 'remove_name_from_address' ) );
add_filter( 'woocommerce_order_formatted_shipping_address', array( $this, 'remove_name_from_address' ) );
}

if ( $this->integration->get_option( 'custom_field_last_name' ) ) {
$fields[ $this->integration->get_option( 'custom_field_last_name' ) ] = $order->get_billing_last_name();
}
Expand All @@ -985,6 +993,14 @@ private function custom_field_data( $order ) {
$fields[ $this->integration->get_option( 'custom_field_customer_note' ) ] = $order->get_customer_note();
}

// If the name and company name should be excluded from the billing and shipping address
// fetched using get_formatted_billing_address() / get_formatted_shipping_address(),
// remove filters now so these WooCommerce functions work correctly for other Plugins.
if ( $this->integration->get_option_bool( 'custom_field_address_exclude_name' ) ) {
remove_filter( 'woocommerce_order_formatted_billing_address', array( $this, 'remove_name_from_address' ) );
remove_filter( 'woocommerce_order_formatted_shipping_address', array( $this, 'remove_name_from_address' ) );
}

/**
* Returns an array of ConvertKit Custom Field Key/Value pairs, with values
* comprising of Order data based, to be sent to ConvertKit when an Order's
Expand All @@ -1003,4 +1019,20 @@ private function custom_field_data( $order ) {

}

/**
* Removes the first name, last name and company name from the WooCommerce Order address,
* when calling WC_Order->get_formatted_billing_address() and WC_Order->get_formatted_shipping_address().
*
* @since 1.8.5
*
* @param array $address Billing or Shipping Address.
* @return array
*/
public function remove_name_from_address( $address ) {

unset( $address['first_name'], $address['last_name'], $address['company_name'] );
return $address;

}

}
9 changes: 5 additions & 4 deletions tests/_support/Helper/Acceptance/ConvertKitAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,15 @@ public function apiUnsubscribe($id)
/**
* Check the subscriber array's custom field data is valid.
*
* @param AcceptanceTester $I AcceptanceTester.
* @param array $subscriber Subscriber from API.
* @param AcceptanceTester $I AcceptanceTester.
* @param array $subscriber Subscriber from API.
* @param bool $excludeNameFromAddress Exclude name from billing address.
*/
public function apiCustomFieldDataIsValid($I, $subscriber)
public function apiCustomFieldDataIsValid($I, $subscriber, $excludeNameFromAddress = false)
{
$I->assertEquals($subscriber['fields']['last_name'], 'Last');
$I->assertEquals($subscriber['fields']['phone_number'], '123-123-1234');
$I->assertEquals($subscriber['fields']['billing_address'], 'First Last, Address Line 1, City, CA 12345');
$I->assertEquals($subscriber['fields']['billing_address'], ( $excludeNameFromAddress ? 'Address Line 1, City, CA 12345' : 'First Last, Address Line 1, City, CA 12345' ));
$I->assertEquals($subscriber['fields']['payment_method'], 'cod');
$I->assertEquals($subscriber['fields']['notes'], 'Notes');
}
Expand Down
61 changes: 32 additions & 29 deletions tests/_support/Helper/Acceptance/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,16 @@ public function deactivateWooCommerceAndConvertKitPlugins($I)
*
* @since 1.6.0
*
* @param AcceptanceTester $I Acceptance Tester.
* @param bool|string $accessToken Access Token (if specified, used instead of CONVERTKIT_OAUTH_ACCESS_TOKEN).
* @param bool|string $refreshToken Refresh Token (if specified, used instead of CONVERTKIT_OAUTH_REFRESH_TOKEN).
* @param string $subscriptionEvent Subscribe Event.
* @param bool|string $subscription Form, Tag or Sequence to subscribe customer to.
* @param string $nameFormat Name Format.
* @param bool $mapCustomFields Map Order data to Custom Fields.
* @param bool $displayOptIn Display Opt-In Checkbox.
* @param bool $sendPurchaseDataEvent Send Purchase Data to ConvertKit on Order Event.
* @param AcceptanceTester $I Acceptance Tester.
* @param bool|string $accessToken Access Token (if specified, used instead of CONVERTKIT_OAUTH_ACCESS_TOKEN).
* @param bool|string $refreshToken Refresh Token (if specified, used instead of CONVERTKIT_OAUTH_REFRESH_TOKEN).
* @param string $subscriptionEvent Subscribe Event.
* @param bool|string $subscription Form, Tag or Sequence to subscribe customer to.
* @param string $nameFormat Name Format.
* @param bool $mapCustomFields Map Order data to Custom Fields.
* @param bool $displayOptIn Display Opt-In Checkbox.
* @param bool $sendPurchaseDataEvent Send Purchase Data to ConvertKit on Order Event.
* @param bool $excludeNameFromAddress Exclude name from billing and shipping addresses.
*/
public function setupConvertKitPlugin(
$I,
Expand All @@ -104,39 +105,41 @@ public function setupConvertKitPlugin(
$nameFormat = 'first',
$mapCustomFields = false,
$displayOptIn = false,
$sendPurchaseDataEvent = false
$sendPurchaseDataEvent = false,
$excludeNameFromAddress = false
) {
// Define Plugin's settings.
$I->haveOptionInDatabase(
'woocommerce_ckwc_settings',
[
'enabled' => 'yes',
'access_token' => ( $accessToken !== false ? $accessToken : $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'] ),
'refresh_token' => ( $refreshToken !== false ? $refreshToken : $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] ),
'event' => $subscriptionEvent,
'subscription' => ( $subscription ? $subscription : '' ),
'name_format' => $nameFormat,
'enabled' => 'yes',
'access_token' => ( $accessToken !== false ? $accessToken : $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'] ),
'refresh_token' => ( $refreshToken !== false ? $refreshToken : $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] ),
'event' => $subscriptionEvent,
'subscription' => ( $subscription ? $subscription : '' ),
'name_format' => $nameFormat,

// Custom Field mappings.
'custom_field_last_name' => ( $mapCustomFields ? 'last_name' : '' ),
'custom_field_phone' => ( $mapCustomFields ? 'phone_number' : '' ),
'custom_field_billing_address' => ( $mapCustomFields ? 'billing_address' : '' ),
'custom_field_shipping_address' => ( $mapCustomFields ? 'shipping_address' : '' ),
'custom_field_payment_method' => ( $mapCustomFields ? 'payment_method' : '' ),
'custom_field_customer_note' => ( $mapCustomFields ? 'notes' : '' ),
'custom_field_last_name' => ( $mapCustomFields ? 'last_name' : '' ),
'custom_field_phone' => ( $mapCustomFields ? 'phone_number' : '' ),
'custom_field_billing_address' => ( $mapCustomFields ? 'billing_address' : '' ),
'custom_field_shipping_address' => ( $mapCustomFields ? 'shipping_address' : '' ),
'custom_field_payment_method' => ( $mapCustomFields ? 'payment_method' : '' ),
'custom_field_customer_note' => ( $mapCustomFields ? 'notes' : '' ),
'custom_field_address_exclude_name' => ( $excludeNameFromAddress ? 'yes' : 'no' ),

// Opt-In Checkbox.
'display_opt_in' => ( $displayOptIn ? 'yes' : 'no' ),
'opt_in_label' => 'I want to subscribe to the newsletter',
'opt_in_status' => 'checked',
'opt_in_location' => 'billing',
'display_opt_in' => ( $displayOptIn ? 'yes' : 'no' ),
'opt_in_label' => 'I want to subscribe to the newsletter',
'opt_in_status' => 'checked',
'opt_in_location' => 'billing',

// Purchase Data.
'send_purchases' => ( $sendPurchaseDataEvent ? 'yes' : 'no' ),
'send_purchases_event' => ( $sendPurchaseDataEvent ? $sendPurchaseDataEvent : '' ),
'send_purchases' => ( $sendPurchaseDataEvent ? 'yes' : 'no' ),
'send_purchases_event' => ( $sendPurchaseDataEvent ? $sendPurchaseDataEvent : '' ),

// Debug.
'debug' => 'yes',
'debug' => 'yes',
]
);
}
Expand Down
4 changes: 3 additions & 1 deletion tests/_support/Helper/Acceptance/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public function wooCommerceCreateProductAndCheckoutWithConfig($I, $options = fal
'send_purchase_data' => false,
'product_form_tag_sequence' => false,
'custom_fields' => false,
'exclude_name_from_address' => false,
'name_format' => 'first',
'coupon_form_tag_sequence' => false,
'use_legacy_checkout' => true,
Expand All @@ -232,7 +233,8 @@ public function wooCommerceCreateProductAndCheckoutWithConfig($I, $options = fal
$options['name_format'],
$options['custom_fields'],
$options['display_opt_in'],
( ( $options['send_purchase_data'] === true ) ? 'processing' : $options['send_purchase_data'] )
( ( $options['send_purchase_data'] === true ) ? 'processing' : $options['send_purchase_data'] ),
$options['exclude_name_from_address']
);

// Create Product.
Expand Down
Loading

0 comments on commit e29c318

Please sign in to comment.