Skip to content

Instantly share code, notes, and snippets.

@lunule
Last active July 31, 2020 06:14
Show Gist options
  • Save lunule/09254f56756db182878ef04fcdba5309 to your computer and use it in GitHub Desktop.
Save lunule/09254f56756db182878ef04fcdba5309 to your computer and use it in GitHub Desktop.
[ACF - update_field() for special (and not-so-special) field types] #acf #woocommerce-memberships #update_field #fields
function qsa_update_user_meta($customer_id) {
$cid = $customer_id;
$ms = wc_memberships_get_user_active_memberships( $cid );
if ( ! empty( $ms ) ) :
// We handle the first active membership as The Membership!!
$m = $ms[0];
$mid = $m->plan_id;
// URL resources:
// https://www.advancedcustomfields.com/resources/text/
// https://www.advancedcustomfields.com/resources/url/
// https://www.advancedcustomfields.com/resources/user/
// https://www.advancedcustomfields.com/resources/date-picker/
// https://www.advancedcustomfields.com/resources/date-time-picker/
/* QSA Group Info fields
===================================================================================
/**
* User fields
* -----------
*/
$guide_Arr = []; // updating a field of type user requires an array!!
// @see related resource above.
$guide = get_field( 'guide', $mid, false );
array_push($guide_Arr, $guide);
update_field( 'guide', $guide_Arr, 'user_' . $cid );
/**
* Text and url fields
* -------------------
*/
$ztime = get_field( 'zoom_time', $mid );
$zmlink = get_field( 'zoom_meeting_link', $mid );
$zid = get_field( 'zoom_id', $mid );
$zpw = get_field( 'zoom_password', $mid );
$wlink = get_field( 'whatsapp_link', $mid );
if ( $ztime && ( '' !== $ztime ) )
update_field( 'zoom_time', $ztime, 'user_' . $cid );
if ( $zmlink && ( '' !== $zmlink ) )
update_field( 'zoom_meeting_link', $zmlink, 'user_' . $cid );
if ( $zid && ( '' !== $zid ) )
update_field( 'zoom_id', $zid, 'user_' . $cid );
if ( $zpw && ( '' !== $zpw ) )
update_field( 'zoom_password', $zpw, 'user_' . $cid );
if ( $wlink && ( '' !== $wlink ) )
update_field( 'whatsapp_link', $wlink, 'user_' . $cid );
/**
* Time picker fields
* ------------------
*/
$gstart = get_field( 'group_start_date', $mid );
$gfinish = get_field( 'group_finish_date', $mid );
if ( $gstart && ( '' !== $gstart ) ) :
$gstart_date = DateTime::createFromFormat('d/m/Y', $gstart);
if ( !is_bool( $gstart_date ) ) :
$gstart_date = $gstart_date->format('Ymd');
update_field( 'group_start_date', $gstart_date, 'user_' . $cid );
endif;
endif;
if ( $gfinish && ( '' !== $gfinish ) ) :
$gfinish_date = DateTime::createFromFormat('d/m/Y', $gfinish);
if ( !is_bool( $gfinish_date ) ) :
$gfinish_date = $gfinish_date->format('Ymd');
update_field( 'group_finish_date', $gfinish_date, 'user_' . $cid );
endif;
endif;
/* QSA Content Release Schedule fields
===================================================================================
/**
* Date time picker fields
* -----------------------
*/
for ($i=1; $i < 9; $i++) :
$show = get_field( "session_{$i}_content_show", $mid );
$hide = get_field( "session_{$i}_content_hide", $mid );
if ( $show && ( '' !== $show ) ) :
$show_date = DateTime::createFromFormat('d/m/Y g:i a', $show);
if ( !is_bool( $show_date ) ) :
$show_date = $show_date->format('Y-m-d H:i:s');
update_field( "session_{$i}_content_show", $show_date, 'user_' . $cid );
endif;
endif;
if ( $hide && ( '' !== $hide ) ) :
$hide_date = DateTime::createFromFormat('d/m/Y g:i a', $hide);
if ( !is_bool( $hide_date ) ) :
$hide_date = $hide_date->format('Y-m-d H:i:s');
update_field( "session_{$i}_content_hide", $hide_date, 'user_' . $cid );
endif;
endif;
endfor;
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment