Change dates via wp_update_post

Viewed 13

So I am trying to manually update the Subscription Dates and show a notice on the for the subsrriptions here. The goal is to make all the subscriptions trigger on the 1st of every month let's say. So in the wp_posts meta we have post_date and post_date_gmt


class ChiChi_SubscriptionMetaBoxes
{

    /**
     * To load Schedule meta box
     * */
    public static function redateSchedule()
    {
        add_meta_box('chichi-redate-schedule', _x('Redate Schedule', 'meta box title'), 'ChiChi_SubscriptionMetaBoxes::output', 'shop_subscription', 'side', 'default');
    }

    /**
     * To display Redate Schedule
     *
     * @param object $post
     * */
    public static function output($post)
    {
        global $theorder;

        if (wcs_is_subscription($theorder) && !$theorder->has_status(wcs_get_subscription_ended_statuses())) {
            include(get_theme_file_path('/lib/woocommerce/templates/html-redate-schedule.php'));
        }
    }

    public static function save($order_id)
    {
        global $wpdb;

        if (!isset($_POST['redate_schedule_date_nonce']) || !isset($_POST['redate_schedule'])) {
            return $order_id;
        }
        $nonce = $_REQUEST['redate_schedule_date_nonce'];

        if (!is_numeric($_POST['redate_schedule'])) {
            return $order_id;
        }
        $newDay = (int)$_POST['redate_schedule'];

        //Verify that the nonce is valid.
        if (!wp_verify_nonce($nonce) || wp_is_post_revision($order_id)) {
            return $order_id;
        }

        // If this is an autosave, our form has not been submitted, so we don't want to do anything.
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return $order_id;
        }

        // Check the user's permissions.
        if ('page' == $_POST['post_type']) {

            if (!current_user_can('edit_page', $order_id)) {
                return $order_id;
            }
        } else {

            if (!current_user_can('edit_post', $order_id)) {
                return $order_id;
            }
        }

        // Update the meta field in the database.
        $subscription = wcs_get_subscription($order_id);

        $pending_status = 'pending';
        if (class_exists('ActionScheduler_Store')) {
            $pending_status = ActionScheduler_Store::STATUS_PENDING;
        }

        $amSchedule = '_action_manager_schedule';

        try {
            $redatedShedule = [];
            $schedules = as_get_scheduled_actions([
                'hook' => 'woocommerce_subscription_upfront_renewal', 
                'args' => [
                    'subscription_id' => $subscription->get_id()
                ], 
                'status' => $pending_status,
                'per_page' => 100
            ]);

            if (!empty($schedules)) {
                foreach ($schedules as $key => $schedule) {
                    $scheduledPost = get_post($key);

                    if (!empty($scheduledPost)) {
                        $originalDateTs = strtotime($scheduledPost->post_date);
                        $originalGmtDateTs = strtotime($scheduledPost->post_date_gmt);

                        $newDate = date("Y-m-" . $newDay . " H:i:s", $originalDateTs);
                        $gmtDate = get_gmt_from_date($newDate);

                        // Update Post
                        wp_update_post([
                            'ID' => $key,
                            'post_date' => $newDate,
                            'post_date_gmt' => $gmtDate
                        ]);

                        // Update meta time stamps
                        $sql = "UPDATE " . $wpdb->postmeta . " SET meta_value = REPLACE(meta_value, '" . $_schedule_start . "', '" . strtotime($gmtDate) . "') WHERE `post_id` = '" . esc_sql($key) . "' AND `meta_key` = '" . $wpdb->escape($amSchedule) . "'";
                        $wpdb->get_results($sql);

                        $redatedShedule[] = $gmtDate;
                    }
                }

                if (count($redatedShedule) > 0) {
                    // Update End Date
                    $dates['end'] = date('Y-m-d H:i:s', strtotime(end($redatedShedule) . '+1 month'));
                    $subscription->update_dates($dates);

                    $order = new WC_Order($order_id);
                    $order->add_order_note("Subscription has been manually re-dated to: <br/>" . implode('<br>', $redatedShedule));
                }
            }

            return $order_id;

            // wp_cache_delete($post_id, 'posts');
        } catch (Exception $e) {
            wcs_add_admin_notice($e->getMessage(), 'error');
        }
    }
}
add_action('add_meta_boxes', 'ChiChi_SubscriptionMetaBoxes::redateSchedule', 26);
add_action('woocommerce_before_save_order_items', 'ChiChi_SubscriptionMetaBoxes::save', 10, 1);

let me know please if someone spots any issue. The code itself is commented so you know what the specific sections of code are doing.

thanks

0 Answers
Related