0

I have a problem

I'm trying to listen to a webhook, and I can do it, I check with the help wp_mail function and I get all the data

But I want to make a add data in the database, and for some reason update_post_meta or add_post_meta do not work in this condition if (trim ($ _ SERVER ['REQUEST_METHOD']) == 'POST')

See my code bellow

add_action( 'template_redirect', 'my_hook' );
function my_hook() {
    if( is_wc_endpoint_url( 'order-received' ) ) {
        global $wpdb;
        $wc_key = $_GET['key'];
        $result = $wpdb->get_results(
            $wpdb->prepare( "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_order_key' && meta_value LIKE %s", $wc_key)
        );
        $post_id = $result[0]->post_id;
        if ( trim($_SERVER['REQUEST_METHOD']) == 'POST') {
            $data = file_get_contents("php://input");
            $json_data = json_decode($data, true);
            if ( $json_data['event'] == 'document.complete' ) {
                $document_id = $json_data['document_id'];
                //$headers = 'From: My Name <myname@mydomain.com>' . "\r\n";
                //wp_mail('test@gmail.com', 'Theme', strval($document_id), $headers);
                update_post_meta((int)$post_id, '_pleasesign_string', strval($document_id));
            }
        }
    }
}

UPDATE

add_action( 'template_redirect', 'my_hook' );
function my_hook() {
    if( is_wc_endpoint_url( 'order-received' ) ) {
        global $wpdb;
        $wc_key = $_GET['key'];
        $result = $wpdb->get_results(
            $wpdb->prepare( "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_order_key' && meta_value LIKE %s", $wc_key)
        );
        $post_id = $result[0]->post_id;
        add_post_meta((int)$post_id, '_pleasesign_string', 'false', true);
        if ( trim($_SERVER['REQUEST_METHOD']) == 'POST') {
            $data = file_get_contents("php://input");
            $json_data = json_decode($data, true);
            if ( $json_data['event'] == 'document.complete' ) {
                $document_id = $json_data['document_id'];
                //$headers = 'From: My Name <myname@mydomain.com>' . "\r\n";
                //wp_mail('test@gmail.com', 'Theme', strval($document_id), $headers);
                update_post_meta((int)$post_id, '_pleasesign_string', strval($document_id));
            }
        }
    }
}
  • Can you check the output of update_post_meta? For example: $meta_update = update_post_meta([your-code-here]); print_r($meta_update); – Tom Nov 23 '19 at 13:10
  • @Tom the output shows false, because before if (trim ($ _ SERVER ['REQUEST_METHOD']) == 'POST') I added a meta field and it is added. Like this add_post_meta((int)$post_id, '_pleasesign_string', 'false', true); – Сергей Сокол Nov 23 '19 at 13:17
  • @Tom I'am update code – Сергей Сокол Nov 23 '19 at 13:17
  • Understood. Can you provide a sample JSON output? I'm wondering if your IF statement is being triggered. If you are sure the IF statement is firing, then try storing abcd instead of strval($document_id) and see if it will store. If it will, then it looks like there is an issue with document_id. – Tom Nov 24 '19 at 15:26
  • @Tom example of json { "id": "c59f04c2e94db3246b6ed8f65057a264", "event": "document.complete", "document_id": "PLSDOCd29adb10ef03c432644d95403d" } – Сергей Сокол Nov 25 '19 at 18:43

Your Answer

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

Browse other questions tagged or ask your own question.