Programmatically update additional field's value

I would like to know the function that I can use to programmatically change the value of Appointments+ additional fields.

  • Ash
    • WordPress Hacker

    Hello Willi

    You can try the following function:

    function update_app_add_field_val( $app_id, $add_field, $add_val )
    {
    	global $appointments, $wpdb;
    	$appmeta = appointments_get_table( 'appmeta' );
    	$meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $appmeta WHERE app_appointment_id = %d and meta_key = %s ", $app_id, 'additional_fields' ));
    
    	$meta = maybe_unserialize( $meta[0]->meta_value );
    	$meta[$add_field] = $add_val;
    	$meta = maybe_serialize( $meta );
    
    	$wpdb->query( $wpdb->prepare( "UPDATE $appmeta set meta_value = %s WHERE app_appointment_id = %d and meta_key = %s ", $meta, $app_id, 'additional_fields' ));
    }

    Then use the function update_app_add_field_val to update addtional field value.

    Here:
    $app_id = appointment ID,
    $add_field = additional field,
    $add_val = additional field value

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,
    Ash

  • Willi
    • Flash Drive

    Hi Ash,
    I'm not sure the question here was specific enough. I want to change the "status" field to paid when someone pays an invoice. Status isn't an additional field it is part of the appointments default setup, so not sure if the above is still relavent?
    Would you be able to give me a function for changing status to paid?

  • Ash
    • WordPress Hacker

    Hello Willi

    Please try the following function:

    function app_update_status( $app_id )
    {
    	$app = appointments_get_appointment( $app_id );
    	$data = array();
    	$data['status'] = 'paid';
    	appointments_update_appointment( $app_id, $data );
    }

    Call the function like:

    app_update_status(21);

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,
    Ash

  • Willi
    • Flash Drive

    Hi Ash,
    Thanks for this function. I know that the other pieces of this problem will be working out how to call the function at the same time as an invoice is paid, which contains the appointment id. I will try to find the hook is for invoices being marked as paid. Do you have any ideas of how to pull the app id from the invoice?

  • Ash
    • WordPress Hacker

    Hello Willi

    Appointments+ plugin doesn't create any invoice in the system. I think you are using any external invoice system. Is there any developer documentation for that plugin? If so, and you can link to me, then I can try to help. Otherwise, you can contact the plugin support if they can give any idea.

    Let us know how it goes. Have a nice day!

    Cheers,
    Ash

  • Willi
    • Flash Drive

    Yes you are right, it is a plugin called wp-invoice.

    The WP-Invoice Documentation links
    https://www.usabilitydynamics.com/product/wp-invoice/docs/home
    https://www.usabilitydynamics.com/product/wp-invoice/docs/wp-invoice-api
    https://wordpress.org/plugins/wp-invoice
    https://wordpress.org/plugins/wp-invoice/#developers

    I have contacted plugin developers but haven't got a response yet. If you can help it would be much appreciated!

    Cheers
    Willi

  • Willi
    • Flash Drive

    Hi Ash, Thanks for looking into it, the request seems to have been escalated as of yesterday with the plugin developers, so hopefully they will provide the action hook. I was planning to add the appointment ID to either the content section of the invoice or the the description field of the invoiced service, do you think that it matters between these two?

  • Ash
    • WordPress Hacker

    Hello Willi

    I was planning to add the appointment ID to either the content section of the invoice or the the description field of the invoiced service, do you think that it matters between these two?

    Though I am not familiar at all with the plugin, but still, I don't think it does any matter as long you can add the appointment ID with the action hook that the plugin developer will provide :slight_smile:

    Have a nice day!

    Cheers,
    Ash

  • Willi
    • Flash Drive

    Hi,

    Ash when I asked the plugin developers for the hook they gave me this:

    do_action( 'wpi_successful_payment', $invoice );
    We do this one for each payment. ^

    You can use this one for PayPal:
    do_action('wpi_paypal_complete_ipn', $_POST);

    Not sure if this is what we needed because it doesn't reference the invoice being set to paid. I had a dig into the code and found the below. Which seems to do the work but doesn't have an action included. Would it be possible to make something from either or both of these?

    /**
    * Determine if Amount to pay (subtotal) is not 0 and Balance (net) <= 0,
    * We set status as 'Paid'.
    */
    if (isset($this->data['net']) &&
    isset($this->data['subtotal']) &&
    $this->data['subtotal'] > 0 &&
    $this->data['net'] <= 0) {
    $data['post_status'] = 'paid';
    $this->add_entry("type=update&amount=paid&note=".__("Status of invoice was changed to 'Paid'.", ud_get_wp_invoice()->domain));
    } else {
    $data['post_status'] = (!empty($this->data['post_status']) ? $this->data['post_status'] : 'active');
    }

    if( !empty( $this->data['post_date'] ) ) {
    $data['post_date'] = $this->data['post_date'];
    }

    if( !empty( $this->data['post_date_gmt'] ) ) {
    $data['post_date_gmt'] = $this->data['post_date_gmt'];
    }

    if(empty($data['post_title'])) {
    return false;
    }

  • Ash
    • WordPress Hacker

    Hi there

    Nope, it won't help much either, unfortunately. About the hook below:

    do_action( 'wpi_successful_payment', $invoice );

    Please ask them, within this hook how can you find the appointment ID from the $invoice variable. If that is possible, then we can use that hook.

    Have a nice day!

    Cheers,
    Ash

  • Willi
    • Flash Drive

    Hi Ash,

    I've been speaking to the plugin developer and they gave me a lead on the hook that actually happens when the invoice is changed to 'paid' They told me to use the transition_post_status hook (https://codex.wordpress.org/Post_Status_Transitions) and that the post type is 'wpi_object' not 'invoice' as we first tried.

    If the post of type wpi_object is included in a transition_post_status action and it contains the appointment id in one of the fields, could you get the $app_id by doing get_post_meta() ? (https://codex.wordpress.org/Custom_Fields#Getting_Custom_Fields)

    Please tell me I'm close!

  • Ash
    • WordPress Hacker

    Hey Willi

    That might work :slight_smile:

    But now, I need some more information. I need the old status and new status of the post. I also need the meta field that contains the invoice ID.

    If I get those, perhaps I could help you :slight_smile:

    Have a nice day!

    Cheers,
    Ash

  • Willi
    • Flash Drive

    Hi Ash,

    I believe the post statuses are: old- "active" new- "paid" (At least that's what they are displayed as in the front end)

    Inspecting the html in the post editor gives the below that maybe could indicate "description" or "item_description" as the name of the meta field we are after.

    <textarea style="" name="wpi_invoice[itemized_list][1][description]" class="item_description"></textarea>

    I assume there is a proper way to get all of this information for a post type? I have looked online and there seems to be ways to ascertain this using php queries, but not sure how to go about implementing them (where I would put the code and where I would see the result)...

    How would you go about confirming the statuses and meta field information? Happy to do all the searching/confirming, but your guidance in how to do it would be very helpful.

    Thanks
    Willi

  • Ash
    • WordPress Hacker

    Hello Willi

    Without seeing code this is almost impossible to find out the meta name and status etc. The textfield name could be different, the meta field name is something written in php and may not be same as field name.

    What did the other support guys say? Did they give any hint about that? Have a nice day!

    Cheers,
    Ash

  • Willi
    • Flash Drive

    Not sure if my search is missing something but neither of those appear to be in the content of any of the plugin files. I found the below which seems to confirm 'paid' is indeed the value for the status. I also found other bits of code that were talking about the field I am looking for and seemed to indicate it would be called 'description'. Are you able to create the function based on those assumptions and then I can just test? If you need any more information, I could supply the plugin code?

    /**
    * Determine if Amount to pay (subtotal) is not 0 and Balance (net) <= 0,
    * We set status as 'Paid'.
    */
    if (isset($this->data['net']) &&
    isset($this->data['subtotal']) &&
    $this->data['subtotal'] > 0 &&
    $this->data['net'] <= 0) {
    $data['post_status'] = 'paid';
    $this->add_entry("type=update&amount=paid&note=".__("Status of invoice was changed to 'Paid'.", ud_get_wp_invoice()->domain));
    } else {
    $data['post_status'] = (!empty($this->data['post_status']) ? $this->data['post_status'] : 'active');
    }

    if( !empty( $this->data['post_date'] ) ) {
    $data['post_date'] = $this->data['post_date'];
    }

    if( !empty( $this->data['post_date_gmt'] ) ) {
    $data['post_date_gmt'] = $this->data['post_date_gmt'];
    }

    if(empty($data['post_title'])) {
    return false;
    }

  • Ash
    • WordPress Hacker

    Hello Willi

    This is same code you sent here: https://premium.wpmudev.org/forums/topic/programmatically-update-additional-fields-value#post-1305436

    Okay, let me draw a rough scenario here. The hook you got from them:

    do_action( 'wpi_successful_payment', $invoice );

    I assume, $invoice is the invoice object where $invoice->ID is the ID of the invoice. I also assume, the meta name is wpi_app_id where appointment ID is stored. Then you can use the following code:

    add_action( 'wpi_successful_payment', function( $invoice ) {
    	$app_id = get_post_meta( $invoice->ID, 'wpi_app_id', true );
    	$app = appointments_get_appointment( $app_id );
    	$data = array();
    	$data['status'] = 'paid';
    	appointments_update_appointment( $app_id, $data );
    }, 99, 1 );

    The above code should do the job for you. You just need to make sure the following from the invoice plugin support team:
    Invoice ID: $invoice->ID
    App ID meta name: wpi_app_id

    Hope it helps :slight_smile: Have a nice day!

    Cheers,
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.