summaryrefslogtreecommitdiff
path: root/snippets
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2020-10-16 17:34:15 +0200
committerChristian Grothoff <christian@grothoff.org>2020-10-16 17:34:15 +0200
commit2da2471cefdf7f2c1ea2ef5bd96136339eb7394c (patch)
treee573d582f85e06e099c839fa9dcb2a945368d3ba /snippets
parent0c8b3644a0d6083c4249baa078c9a54140124362 (diff)
downloadwoocommerce-taler-2da2471cefdf7f2c1ea2ef5bd96136339eb7394c.tar.gz
woocommerce-taler-2da2471cefdf7f2c1ea2ef5bd96136339eb7394c.tar.bz2
woocommerce-taler-2da2471cefdf7f2c1ea2ef5bd96136339eb7394c.zip
move oidc-woocommerce integration logic into snippet (TEST for now, MAY NOT BE WORKING YET)
Diffstat (limited to 'snippets')
-rw-r--r--snippets/oidc-woocommerce.php126
1 files changed, 126 insertions, 0 deletions
diff --git a/snippets/oidc-woocommerce.php b/snippets/oidc-woocommerce.php
new file mode 100644
index 0000000..5f31e35
--- /dev/null
+++ b/snippets/oidc-woocommerce.php
@@ -0,0 +1,126 @@
+/**
+ * This snippet integrates the OIDC connect button with WooCommerce
+ * to allow importing the customer's shopping details via OIDC.
+ */
+
+/**
+ * Add OIDC login form to checkout page.
+ */
+add_action ('openid-connect-generic-register-login-form',
+ function ( $login_form ) {
+
+ // if WooCommerce is inactive, do nothing
+ if ( ! in_array( 'woocommerce/woocommerce.php',
+ apply_filters( 'active_plugins',
+ get_option( 'active_plugins' ) ) ) ) {
+ return;
+ }
+
+ // show login form at the shopping cart (if not logged in)
+ add_action( 'woocommerce_before_checkout_billing_form',
+ array( $login_form, 'action_login_page' ) );
+ // Add action to set cookie to redirect back to current
+ // (checkout) page after OIDC provided the data
+ // (see similar add_action()
+ // above for the 'login_form_login' - we must do the
+ // same on the 'Billing details' page!)
+ add_action( 'woocommerce_before_checkout_billing_form',
+ array( $login_form, 'handle_redirect_cookie' ) );
+
+ }
+});
+
+
+/**
+ * Action to output the login button inside the "Billing details" form
+ * just before the user would otherwise manually enter their details.
+ */
+function action_login_page ( $login_form ) {
+ $user = wp_get_current_user ();
+ if (0 == $user->ID) {
+ // ID 0 is used to indicate user is not logged in.
+ // Re-use filter logic to generate login page
+ print ( $login_form->handle_login_page ('') );
+ }
+}
+
+
+// hook to convert OIDC fields to WC checkout fields if logged in
+add_filter( 'woocommerce_checkout_fields',
+ 'filter_checkout_get_value' );
+
+
+/**
+ * Filter to transform (defaults) for the Billing details according to
+ * the information obtained from OpenID Connect (OIDC). WooCommerce
+ * already grabs many of the values properly, but there are some fields
+ * which we have to convert manually.
+ */
+function filter_checkout_get_value ($in=array()){
+ $user = wp_get_current_user ();
+ if (0 != $user->ID) {
+ $token_response = $user->get ('openid-connect-generic-last-user-claim');
+ if ( (isset( $token_response)) &&
+ (array_key_exists ('given_name', $token_response)) ){
+ $given_name = $token_response [ 'given_name' ];
+ $in['billing']['billing_first_name']['default'] = $given_name;
+ $in['shipping']['shipping_first_name']['default'] = $given_name;
+ }
+ if ( (isset( $token_response)) &&
+ (array_key_exists ('family_name', $token_response)) ){
+ $family_name = $token_response [ 'family_name' ];
+ $in['billing']['billing_last_name']['default'] = $family_name;
+ $in['shipping']['shipping_last_name']['default'] = $family_name;
+ }
+ if ( (isset( $token_response)) &&
+ (array_key_exists ('phone_number', $token_response)) ){
+ $phone_number = $token_response [ 'phone_number' ];
+ $in['billing']['billing_phone']['default'] = $phone_number;
+ $in['shipping']['shipping_phone']['default'] = $phone_number;
+ }
+ if ( (isset( $token_response)) &&
+ (array_key_exists ('address', $token_response)) ){
+ $address = $token_response [ 'address' ];
+ if ( array_key_exists( 'postal_code', $address ) ){
+ $postal_code = $address [ 'postal_code' ];
+ $in['billing']['billing_postcode']['default'] = $postal_code;
+ $in['shipping']['shipping_postcode']['default'] = $postal_code;
+ }
+ if ( array_key_exists( 'country', $address ) ){
+ $country = $address [ 'country' ];
+ $in['billing']['billing_country']['default'] = $country;
+ $in['shipping']['shipping_country']['default'] = $country;
+ }
+ if ( array_key_exists( 'locality', $address ) ){
+ $locality = $address [ 'locality' ];
+ $in['billing']['billing_city']['default'] = $locality;
+ $in['shipping']['shipping_city']['default'] = $locality;
+ }
+ if ( array_key_exists ( 'region', $address ) ){
+ $region = $address [ 'region' ];
+ $in['billing']['billing_state']['default'] = $region;
+ $in['shipping']['shipping_state']['default'] = $region;
+ }
+ if ( array_key_exists ( 'formatted', $address ) ){
+ $formatted = $address [ 'formatted' ];
+ $lines = explode ( '\r\n', $formatted );
+ if ( count($lines) > 0 ){
+ $in['billing']['billing_address_1']['default'] = $lines[0];
+ $in['shipping']['shipping_address_1']['default'] = $lines[0];
+ }
+ if ( count($lines) > 1 ){
+ $in['billing']['billing_address_2']['default'] = $lines[1];
+ $in['shipping']['shipping_address_2']['default'] = $lines[1];
+ }
+ }
+ if ( array_key_exists ( 'street_address', $address ) ){
+ $street_address = $address [ 'street_address' ];
+ if ( ! array_key_exists ( 'formatted', $address ) ){
+ $in['billing']['billing_address_1']['default'] = $street_address;
+ $in['shipping']['shipping_address_1']['default'] = $street_address;
+ }
+ }
+ }
+ }
+ return $in;
+}