memberful / memberful-wp

Better membership software for WordPress.
https://memberful.com
42 stars 15 forks source link

Sync shipping address #125

Open dstrojny opened 9 years ago

dstrojny commented 9 years ago

We have an option for collecting shipping address, but it doesn't sync with the WordPress user profile. We should consider adding it in.

dstrojny commented 9 years ago

I'm going to close this, because I think we could better solve this problem by providing a nice export of members in the Memberful dashboard.

skl commented 6 years ago

@dstrojny I don't suppose you'd still consider this? We'd like this to synchronise with WordPress as we have a WooCommerce shop which separately stores Billing/Shipping address and at the moment Memberful also stores a shipping address and obviously they're getting out of sync. Right now if we run the Memberful export, we don't see any of the WordPress shipping addresses.

dstrojny commented 6 years ago

Sure! We can definitely reconsider this.

skl commented 5 years ago

I have this working with a custom plugin and child theme for a WooCommerce site I work with. My approach was:

Memberful --> WordPress/WooCommerce

WordPress/WooCommerce --> Memberful

// Intercept database save by forcing a redirect to prevent further processing by WooCommerce,
// emulate normal behaviour so user is none the wiser.
wc_add_notice( __( 'Address changed successfully.', 'woocommerce' ) );
do_action( 'woocommerce_customer_save_address', $user_id, $load_address );
wp_safe_redirect( wc_get_endpoint_url( 'edit-address', '', wc_get_page_permalink( 'myaccount' ) ) );
exit;

I suppose you could take out the sleep and just return instead of the DB save intercept, which would hand over control back to the WooCommerce form processor. But there is a slight discrepancy as there are more WooCommerce meta fields than there are Memberful address fields (e.g. shipping_company, shipping_address_2).

The behaviour is transparent to the user (except it takes a few seconds longer to save the address) and doesn't require custom WooCommerce templates for the address editing pages.

The custom WooCommerce templates may be required in order to change the account-editing links to Memberful account links (which will bring up the Memberful Overlay automatically) e.g. sign in/out and password reset.

It should be noted that I initially tried the approach of adding a WordPress filter to the "update_user_metadata" hook but this quickly led to circular updates between the WordPress DB and the Webhook essentially calling each other.

One case the above hasn't been tested for is if the user edits their address on the checkout page during checkout. It's a start though.

dstrojny commented 5 years ago

Cool! Thanks for sharing the solution you have in place 👍

skl commented 5 years ago

After thinking this through I've swapped out sleep() for return so that WooCommerce can complete the DB update as normal. This way there's no delay for the user. However to keep the data consistent I had to add delete_user_meta() on the webhook listener to delete "shipping_address_2" and "shipping_company" WP meta fields as I just concatenate these into Memberful's address.street field in the mutation query.

Doing it this way makes the bi-direction sync asynchronous without any apparent slowdown (by also setting blocking => false on the wp_remote_post() calls to the Memberful API).