1

I need to submit a form on the woo-commerce product page via restful API custom endpoint

the problem is the wp_remote_post not working correctly with me or me mostly I did something wrong.

The form is add to cart form and it has a lot of custom data, not the usual add to cart. for plugin called yith composite to specific

My code so far

add_action('rest_api_init', function () {
    register_rest_route( 'yith-composite', 'add-item',array(
        'methods' => 'POST',
        'callback' => 'yith_custom_add_item'
    ));
});

function yith_custom_add_item( WP_REST_Request $request ) {
    $currentuserid_fromjwt = get_current_user_id();
    $product_id = $_GET['id'];
    $url = get_permalink( $product_id );
    $user_cookie = wp_generate_auth_cookie($currentuserid_fromjwt, time() + (10 * 365 * 24 * 60 * 60), 'logged_in');

    $response = wp_remote_post( $url, array(
        'body'    => json_encode($request->get_json_params()['body']),
        'headers' => array(
                           'Content-Type' => 'multipart/form-data;',
                           'cookie' => 'PHPSESSID=2f3f5db725c728c627ada8196ca9d438; tk_ai=woo%3A4404G2GnKIS%2BNq44234vMsYl; wordpress_test_cookie=WP+Cookie+check; wp-settings-time-138=1574607034; wordpress_logged_in_856ec7e7dd32c9b2fc11b366505cf40d=' . $user_cookie
                       ),
    ) );
    return $response;
    exit;
}

I send the following body including bearer token with postman:

{
    "body": {
        "ywcp_selection[153CE4EE-C1A0-6D39-9AD5-58B51C5AB306]": -1,
        "ywcp_variation_id[153CE4EE-C1A0-6D39-9AD5-58B51C5AB306]": 124136,
        "ywcp_quantity[153CE4EE-C1A0-6D39-9AD5-58B51C5AB306]": 1,
        "ywcp_selected_product_value[153CE4EE-C1A0-6D39-9AD5-58B51C5AB306]": 10,
        "ywcp_selection[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": -1,
        "attribute_pa_sphere[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": "5-75",
        "ywcp_variation_id[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": 110359,
        "ywcp_quantity[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": 1,
        "ywcp_selected_product_value[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": 102552,
        "quantity": 1,
        "add-to-cart": 102491
    }
}
4
  • and what is the result of your code ? and what is the result you want ? edit your question to add all these details.
    – Kaperto
    Nov 27, 2019 at 21:08
  • I have a plugin that adds a cart API to woocommerce, if I just submit the form the product will be added to customer cart by cookie or toke. I tried this on postman but the request body was form data and succeed. the product added to that customer bt its token Nov 27, 2019 at 21:51
  • You shouldn't exit in a REST API endpoint, and you shouldn't use PHP session IDs either
    – Tom J Nowell
    Nov 27, 2019 at 23:08
  • Remove both still the same issue, if you could share code for answering this will be appreciated. Nov 28, 2019 at 1:55

0

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.