Skip to content

Instantly share code, notes, and snippets.

@rogercoathup
Last active November 22, 2021 01:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogercoathup/4692f97eb32cb121c8100d2a237d3831 to your computer and use it in GitHub Desktop.
Save rogercoathup/4692f97eb32cb121c8100d2a237d3831 to your computer and use it in GitHub Desktop.
Supports the purchaser entering the recipient address at checkout for a woocommerce-subscriptions-gifting (https://github.com/Prospress/woocommerce-subscriptions-gifting) gift.
<?php
add_action( 'woocommerce_checkout_subscription_created', 'maybe_reinstate_gifting_address', 99, 3 );
/**
* Ugly hack to reinstate the ship to address, if it's been removed by the gifting plugin
* Don't remove if it's an existing user though
* @param [type] $subscription [description]
* @param [type] $order [description]
* @param [type] $recurring_cart [description]
* @return [type] [description]
*/
function maybe_reinstate_gifting_address( $subscription, $order, $recurring_cart ) {
// only process immediate gift subscriptions
$cart_item = reset( $recurring_cart->cart_contents );
if ( ! empty( $cart_item['wcsg_gift_recipients_email'] ) ) {
$recipient_user_id = email_exists( $cart_item['wcsg_gift_recipients_email'] );
if ( 'true' === get_user_meta( $recipient_user_id, 'wcsg_update_account', true ) ) {
$address = array(
'first_name' => $order->get_shipping_first_name(),
'last_name' => $order->get_shipping_last_name(),
'country' => $order->get_shipping_country(),
'company' => $order->get_shipping_company(),
'address_1' => $order->get_shipping_address_1(),
'address_2' => $order->get_shipping_address_2(),
'city' => $order->get_shipping_city(),
'state' => $order->get_shipping_state(),
'postcode' => $order->get_shipping_postcode(),
);
$subscription->set_address( $address, 'shipping' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment