1

Trying to add to cart functionality in my custom php code using shopify ajax api. My code is below for which getting error code 401.

Anybody can give me example of how use the Shopify ajax apis or solve problem in code below in php code.

CODE:

<?php
    session_start();
    require __DIR__.'/vendor/autoload.php';
    use phpish\shopify;

    require __DIR__.'conf.php';

    $shopify = shopify\client(SHOPIFY_SHOP, SHOPIFY_APP_API_KEY, SHOPIFY_APP_PASSWORD, true);

    try
    {
        $add = $shopify('POST /cart/add.js',array(), array
        (
            'cart' => array
            (
                "quantity" => 2,
                "id" => 3908511558
            )
        ));
        echo "<pre>";
        print_r($add);
        echo "</pre>";
    }
    catch(shopify\ApiException $e)
    {
        echo $e;
        echo "<pre>";
        print_R($e->getRequest());
        print_R($e->getResponse());
        echo "</pre>";
    }
    catch(shopify\CurlException $e)
    {
        echo $e;
        echo "<pre>";
        print_R($e->getRequest());
        print_R($e->getResponse());
        echo "</pre>";
    }
?>

ERROR:

phpish\shopify\ApiException: [401] Unauthorized
5
  • Code seems okay. Check your API credentials? Commented Dec 1, 2015 at 14:44
  • cart/add.js only work in shopify store environment, not external pages. Commented Dec 1, 2015 at 19:48
  • API credentials are correct. please give me example of usage of this api in php. Commented Dec 2, 2015 at 5:14
  • cart/add.js not belong to the shopify api Commented Dec 2, 2015 at 15:07
  • Thanks miglio for helping me and i am also try jquery ajax api of shopify for add to cart but when i run this api in my localhost then shopify gives me 401 error for authentication and ask me to replace code in my shopify store or enable to Your CROS. So please if you have example of jquery ajax api please help me. Commented Dec 3, 2015 at 10:44

1 Answer 1

1

You are sending wrong parameters to the api. Try :

shopify\client(STORE_NAME, SHOPIFY_APP_API_KEY, ACCESS_TOKEN);

in place of :

$shopify = shopify\client(SHOPIFY_SHOP, SHOPIFY_APP_API_KEY, SHOPIFY_APP_PASSWORD, true);

Rest of the code seems correct.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.