Created
February 23, 2024 04:17
-
-
Save mattallan/28f2fc0ae49e9d4d67bbf88268a1e4d4 to your computer and use it in GitHub Desktop.
Woo Subscriptions Gifting - Recipient Address Update Fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Woo Subscriptions Gifting - Recipient Address Update Fix | |
* Description: When changing a subscription's address that you have gifted, only update the shipping address. Don't update the gifter's address. | |
* Author: Matt Allan | |
* Author URI: https://woocommerce.com/ | |
*/ | |
function wcsg_update_gifted_subscription_shipping_address() { | |
global $wp; | |
$nonce_value = wc_get_var( $_REQUEST['woocommerce-edit-address-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine. | |
if ( ! wp_verify_nonce( $nonce_value, 'woocommerce-edit_address' ) ) { | |
return; | |
} | |
if ( empty( $_POST['action'] ) || 'edit_address' !== $_POST['action'] || ! isset( $_GET['subscription'] ) ) { | |
return; | |
} | |
if ( ! function_exists( 'wcs_get_subscription' ) || ! method_exists( 'WCS_Gifting', 'get_recipient_user' ) ) { | |
return; | |
} | |
wc_nocache_headers(); | |
$subscription = wcs_get_subscription( absint( $_GET['subscription'] ) ); | |
if ( ! $subscription ) { | |
return; | |
} | |
$recipient_user_id = absint( WCS_Gifting::get_recipient_user( $subscription ) ); | |
$user_id = get_current_user_id(); | |
// Bail if the subscription doesn't have a recipient or the user is not allowed to view the subscription or if the user is the recipient. | |
// When the current logged in user is the recipient, they should use the default WooCommerce Core save_address() function. | |
if ( $user_id <= 0 || $recipient_user_id <= 0 || ! user_can( $user_id, 'view_order', $subscription->get_id() ) || $recipient_user_id === $user_id ) { | |
return; | |
} | |
$address_type = isset( $wp->query_vars['edit-address'] ) ? wc_edit_address_i18n( sanitize_title( $wp->query_vars['edit-address'] ), true ) : 'billing'; | |
// Bail if the address type is not shipping or the country field is not set. | |
if ( $address_type !== 'shipping' || ! isset( $_POST[ $address_type . '_country' ] ) ) { | |
return; | |
} | |
$address = WC()->countries->get_address_fields( wc_clean( wp_unslash( $_POST[ $address_type . '_country' ] ) ), $address_type . '_' ); | |
foreach ( $address as $key => $field ) { | |
if ( ! isset( $field['type'] ) ) { | |
$field['type'] = 'text'; | |
} | |
// Get Value. | |
if ( 'checkbox' === $field['type'] ) { | |
$value = (int) isset( $_POST[ $key ] ); | |
} else { | |
$value = isset( $_POST[ $key ] ) ? wc_clean( wp_unslash( $_POST[ $key ] ) ) : ''; | |
} | |
// Hook to allow modification of value. | |
$value = apply_filters( 'woocommerce_process_myaccount_field_' . $key, $value ); | |
// Validation: Required fields. | |
if ( ! empty( $field['required'] ) && empty( $value ) ) { | |
/* translators: %s: Field name. */ | |
wc_add_notice( sprintf( __( '%s is a required field.', 'woocommerce' ), $field['label'] ), 'error', array( 'id' => $key ) ); | |
} | |
if ( ! empty( $value ) ) { | |
// Validation and formatting rules. | |
if ( ! empty( $field['validate'] ) && is_array( $field['validate'] ) ) { | |
foreach ( $field['validate'] as $rule ) { | |
switch ( $rule ) { | |
case 'postcode': | |
$country = wc_clean( wp_unslash( $_POST[ $address_type . '_country' ] ) ); | |
$value = wc_format_postcode( $value, $country ); | |
if ( '' !== $value && ! WC_Validation::is_postcode( $value, $country ) ) { | |
switch ( $country ) { | |
case 'IE': | |
$postcode_validation_notice = __( 'Please enter a valid Eircode.', 'woocommerce' ); | |
break; | |
default: | |
$postcode_validation_notice = __( 'Please enter a valid postcode / ZIP.', 'woocommerce' ); | |
} | |
wc_add_notice( $postcode_validation_notice, 'error' ); | |
} | |
break; | |
case 'phone': | |
if ( '' !== $value && ! WC_Validation::is_phone( $value ) ) { | |
/* translators: %s: Phone number. */ | |
wc_add_notice( sprintf( __( '%s is not a valid phone number.', 'woocommerce' ), '<strong>' . $field['label'] . '</strong>' ), 'error' ); | |
} | |
break; | |
case 'email': | |
$value = strtolower( $value ); | |
if ( ! is_email( $value ) ) { | |
/* translators: %s: Email address. */ | |
wc_add_notice( sprintf( __( '%s is not a valid email address.', 'woocommerce' ), '<strong>' . $field['label'] . '</strong>' ), 'error' ); | |
} | |
break; | |
} | |
} | |
} | |
} | |
} | |
wc_add_notice( __( 'Gifted subscription\'s shipping address changed successfully.', 'woocommerce-subscriptions-gifting' ) ); | |
/** | |
* Hook: woocommerce_customer_save_address. | |
* | |
* Fires after a customer address has been saved. | |
* | |
* @since 3.6.0 | |
* @param int $user_id User ID being saved. | |
* @param string $address_type Type of address; 'billing' or 'shipping'. | |
*/ | |
do_action( 'woocommerce_customer_save_address', $user_id, $address_type ); | |
wp_safe_redirect( wc_get_endpoint_url( 'edit-address', '', wc_get_page_permalink( 'myaccount' ) ) ); | |
exit; | |
} | |
add_action( 'template_redirect', 'wcsg_update_gifted_subscription_shipping_address', 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment