• Resolved pmsn

    (@pmsn)


    Hi. I tried to get cart with authenticated user but I get this error.

    {
        "code": "cocart_must_authenticate_user",
        "message": "Must authenticate customer as the cart key provided is a registered customer."
    }

    This is my code.

    public function get_cart($_var)
        {
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://mystore.com/wp-json/cocart/v2/cart?cart_key=2",
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTPHEADER => array(
                    'Accept: application/json',
                    'User-Agent: CoCart API/v2',
                    'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2RhbGxlc3RvcmUubGl2ZSIsImlhdCI6MTY2ODkyMzkxNSwibmJmIjoxNjY4OTIzOTE1LCJleHAiOjE2Njk1Mjg3MTUsImRhdGEiOnsidXNlciI6eyJpZCI6IjIifX19.yT7zZiCI9JE4r-zywzZFV0aj_mCmHQGzFkCE2XQzbkQ'
                )
            ));
    
            $response = curl_exec($curl);
            curl_close($curl);
            $response = json_decode($response, true);
            echo json_encode($response);
        }

    For JWT I use this plugin https://www.ads-software.com/plugins/jwt-authentication-for-wp-rest-api/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter pmsn

    (@pmsn)

    Also I tried not send cart_key. Only send jwtoken. When I add item with cart/add-item endpoint it return cart data like this.

    {
        "cart_hash": "dadf5550c07e784cc4d7bcbd95570906",
        "cart_key": "2",
        "currency": { ... },
        "customer": { ... },
        "items": [
            {
                "item_key": "d82c8d1619ad8176d665453cfb2e55f0",
                "id": 53,
                "name": "Black, size: XL, prompt: eeee",
                "title": "Black, size: XL, prompt: eeee",
                "price": "15000",
                "quantity": {
                    "value": 1,
                    "min_purchase": 1,
                    "max_purchase": -1
                },
                "totals": {
                    "subtotal": "15000",
                    "subtotal_tax": 0,
                    "total": 150,
                    "tax": 0
                },
                "slug": "black-size-xl-prompt-eeee",
                "meta": {
                    "product_type": "simple",
                    "sku": "",
                    "dimensions": {
                        "length": "",
                        "width": "",
                        "height": "",
                        "unit": "cm"
                    },
                    "weight": 0,
                    "variation": [],
                    "virtual": false,
                    "downloadable": false
                },
                "backorders": "",
                "cart_item_data": [],
                "featured_image": "...",
                "categories": [ ... ],
                "tags": false,
                "stock_status": {
                    "status": "In Stock",
                    "stock_quantity": null,
                    "hex_color": "#7ad03a"
                },
                "gallery": { ... },
                "permalink": "...",
                "is_discounted": false,
                "price_regular": "15000",
                "price_sale": "0",
                "price_discounted": "0"
            }
        ],
        "item_count": 1,
        "items_weight": 0,
        "coupons": [],
        "needs_payment": true,
        "needs_shipping": true,
        "shipping": { ... },
        "fees": [],
        "taxes": [],
        "totals": { ... },
        "removed_items": [],
        "cross_sells": [],
        "notices": { ... }
    }

    When I check response header for CoCart-API-Cart-Key I see “a4ab59e0b4b2af619fac4e54e929aeb0” so I check my database and I saw cart row with this cart_key in cart table.

    And then I get cart again using /cart endpoint I got this result.

    {
        "cart_hash": "",
        "cart_key": "2",
        "currency": { ... },
        "customer": { ... }
        "items": [],
        "item_count": 0,
        "items_weight": 0,
        "coupons": [],
        "needs_payment": false,
        "needs_shipping": false,
        "shipping": {
            "total_packages": 0,
            "show_package_details": false,
            "has_calculated_shipping": false,
            "packages": []
        },
        "fees": [],
        "taxes": [],
        "totals": { .... },
        "removed_items": [],
        "cross_sells": [],
        "notices": []
    }

    I got same cart_key but no items. and CoCart-API-Cart-Key from response header is “905dd0ba1e5e55b137230d34c7391771” but there’s no cart row with this cart_key in my database.

    Plugin Author Sébastien Dumont

    (@sebd86)

    Hey @pmsn

    It looks like you confused the session. The cart key is used for guest customers only.

    You only provide a cart key while authenticating to transfer the cart to that customer logged in and only 1 time. Making any further requests authenticated with the cart key staggers the system as that previous session associated with that cart key no longer exists.

    I would use the logout endpoint to help clear things and try again first by using the login endpoint for the customer you want to login as.

    Then keep the same authentication for all cart endpoints until you are finished with that customer by logout endpoint again.

    Let me know if you still have any issues.

    regards,

    Sébastien Dumont

    Thread Starter pmsn

    (@pmsn)

    Hi Sébastien

    Thank you for your answer.

    After many attempts I found out it is because of cookies.

    So my project have 3 parts
    1. Store app using React
    2. Rest api for React app using php
    3. WP site for woocommerce api and cocart api

    When React app need to make a request to cocart api it not call to cocart api directly it call to php rest api (number 2) and php will send request to cocart api server and then return response back to React app. So I think with this workflow somehow it can’t not set cookies from cocart response to browser so it can’t track my cart.

    My workaround it get set-cookies from cocart api and using React-Cookie to store it and when React app make a request to php server I will add this cookie with
    request headers and send it to cocart api and I can get my cart.

    Plugin Author Sébastien Dumont

    (@sebd86)

    Glad you figured out your issue. Hope it all goes well.

    regards,

    Sébastien Dumont

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Can’t get cart. cocart_must_authenticate_user’ is closed to new replies.