I've been working on this the last day with no luck. Is there any way to update a stored args array key for an event scheduled? Reason I ask is I have array args that look like this when scheduling a payment:

    [customer] => Array
            [name] => ''
            [email] => ''
            [id] => ''
            [user_id] => ''
            [street] => ''
            [city] => ''
            [state] => ''
            [zip] => ''
            [phone] => ''

    [payments] => Array

    [recurring] => Array
            [user_id] => 70
            [amount] => 1600


Payments will get pushed to that array as they are processed, thus changing the array and changing how the key will be serialized. Is it possible to update the key of the array with the new serialized args?


So what I came up with, which seems to work is the following:

This function will search by the user id and find the corresponding cron job

function search_user_cron( $hook, $user_id ) {

    $crons = _get_cron_array();

    foreach( $crons as $timestamp => $cron ) 

        if( isset( $cron[$hook] ) ) 

            foreach( $cron as $jobs ) 

                foreach( $jobs as $key => $job ) 

                    if( $job['args'][0]['customer']['user_id'] == $user_id )

                        return array( 'id' => $user_id, 'timestamp' => $timestamp, 'key' => $key );


Then I use this function to go through and create a new job with the new args and unset the old one

function replace_cron_args( $hook, $user_id ) {

    // lets find the cron job corresponding to the user id
    $user = search_user_cron( $hook, $user_id );

    // get all the current cron jobs
    $crons = _get_cron_array();

    // get the most recent updated arguments
    $stored_args = get_user_meta( $user['id'], 'oe_customer_details', true );

    // get the current user key
    $key = $user['key'];

    // get the current user timestamp
    $timestamp = $user['timestamp'];

    // serialize the new arguments
    $newkey = md5( serialize( $stored_args ) );

    // update the stored arguments
    $crons[$timestamp][$hook][$key]['args'][0] = $stored_args;

    // replace the key with the new serialized key
    $crons[$timestamp][$hook][$newkey] = $crons[$timestamp][$hook][$key];

    // unset the old array
    unset( $crons[$timestamp][$hook][$key] );

    // set the new cron jobs
    _set_cron_array( $crons );


If anyone can think of a more efficient way, please let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.