In the WooCommerce Memberships plugin file class-wc-memberships-user-membership.php I am trying to edit this function in my theme's functions.php file so I don't have to edit this file directly:
/**
* Sets the membership end datetime.
*
* @since 1.0.0
*
* @param string|int $date end date either as a unix timestamp or mysql datetime string - defaults to empty string (unlimited membership, no end date)
* @return bool success
*/
public function set_end_date( $date = '' ) {
$end_timestamp = '';
$end_date = '';
if ( is_numeric( $date ) ) {
$end_timestamp = (int) $date;
} elseif ( is_string( $date ) ) {
$end_timestamp = strtotime( $date );
}
if ( ! empty( $end_timestamp ) ) {
$plan = $this->get_plan();
// if the plan is with fixed dates, ensure the end date is set at midnight of the end day
if ( $plan && $plan->is_access_length_type( 'fixed' ) ) {
try {
$utc_timezone = new \DateTimeZone( 'UTC' );
$local_timezone = new \DateTimeZone( wc_timezone_string() );
$end_datetime = new \DateTime( date( 'c', $end_timestamp ), $utc_timezone );
// since the input date is in UTC, convert the date to local timezone, set to end of the day, then convert back to UTC for storage purposes
$end_datetime->setTimezone( $local_timezone );
$end_datetime->setTime( 0, 0 );
$end_datetime->setTimezone( $utc_timezone );
$end_timestamp = $end_datetime->getTimestamp();
} catch ( \Exception $e ) {
// in case of DateTime errors, just use the end date timestamp as is but issue a warning
trigger_error( sprintf( 'Unable to end start date for User Membership #%d: %s', $this->get_id(), $e->getMessage() ), E_USER_WARNING );
}
}
$end_date = date( 'Y-m-d H:i:s', (int) $end_timestamp );
}
// update end date in post meta
$success = (bool) update_post_meta( $this->id, $this->end_date_meta, $end_date ?: '' );
// set expiration scheduled events
$this->schedule_expiration_events( $end_timestamp );
return $success;
}
However there is no filter I can use to hook into it. I just need to change the $end_date = date( 'Y-m-d H:i:s', (int) $end_timestamp );
to $end_date = date( 'Y-m-d 05:00:00', (int) $end_timestamp );
to force all the memberships to expire at midnight on the day they are due to expire.
Can someone assist me in accomplishing this so I don't have to edit this file directly?
The only option here to update the end date meta without editing the file is the below hook.
Here you can get the membership ID in the variable
$object_id
- you can check if it's a membership object in the add_action and updated the meta accordingly