php 如果WooCommerce会员没有过滤器挂钩到功能,我如何编辑WooCommerce会员“设置结束日期”功能?

i5desfxk  于 2022-11-21  发布在  PHP
关注(0)|答案(1)|浏览(109)

在WooCommerce Memberships插件文件class-wc-memberships-user-membership.php中,我试图在我的主题的functions.php文件中编辑此函数,这样我就不必直接编辑此文件:

/**
     * 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;
    }

但是,没有过滤器可以用来连接到它。我只需要将$end_date = date( 'Y-m-d H:i:s', (int) $end_timestamp );更改为$end_date = date( 'Y-m-d 05:00:00', (int) $end_timestamp );,以强制所有的会员资格在到期日的午夜到期。
有人能帮助我完成这一点,使我不必直接编辑此文件吗?

am46iovg

am46iovg1#

此处更新结束日期 meta而不编辑文件的唯一选项是下钩。

/**
     * Fires immediately after updating a post's metadata.
     *
     * @since 2.9.0
     *
     * @param int    $meta_id    ID of updated metadata entry.
     * @param int    $object_id  Post ID.
     * @param string $meta_key   Metadata key.
     * @param mixed  $meta_value Metadata value. This will be a PHP-serialized string representation of the value
     *                           if the value is an array, an object, or itself a PHP-serialized string.
     */
    do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );

在这里,您可以在变量$object_id中获取成员ID-您可以在add_action中检查它是否是成员对象,并相应地更新 meta

相关问题