Shopify API returns empty cart









up vote
-1
down vote

favorite












Using this API: https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#add-to-cart



From my messenger bot I call cart/add.js multiple times with the variant id and quantity and it returns a success message, but I call cart.js to retrieve the items, the cart is always empty.



I'm adding items to the cart like this:



$cartAPI = 'https://'.$shopKey.':'.$shopSecret.'@'.$shopUrl.'/cart/add.js';
$request = $client->request('POST', $cartAPI, [
'form_params' => [
'id' => (int) $productID,
'quantity' => 1
]
]);


And retrieving cart like this:



$cartAPI = 'https://'.$shopKey.':'.$shopSecret.'@'.$shopUrl.'/cart.js';
$request = $client->get($cartAPI);


I tried to include cookies in the Guzzle call like so
$this->client = new Client(['cookies' => true]); and both calls use the same client instance, but it still returns an empty cart.



I don't use CloudFlare or any Caching mechanism for this.



What am I doing wrong?










share|improve this question



























    up vote
    -1
    down vote

    favorite












    Using this API: https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#add-to-cart



    From my messenger bot I call cart/add.js multiple times with the variant id and quantity and it returns a success message, but I call cart.js to retrieve the items, the cart is always empty.



    I'm adding items to the cart like this:



    $cartAPI = 'https://'.$shopKey.':'.$shopSecret.'@'.$shopUrl.'/cart/add.js';
    $request = $client->request('POST', $cartAPI, [
    'form_params' => [
    'id' => (int) $productID,
    'quantity' => 1
    ]
    ]);


    And retrieving cart like this:



    $cartAPI = 'https://'.$shopKey.':'.$shopSecret.'@'.$shopUrl.'/cart.js';
    $request = $client->get($cartAPI);


    I tried to include cookies in the Guzzle call like so
    $this->client = new Client(['cookies' => true]); and both calls use the same client instance, but it still returns an empty cart.



    I don't use CloudFlare or any Caching mechanism for this.



    What am I doing wrong?










    share|improve this question

























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      Using this API: https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#add-to-cart



      From my messenger bot I call cart/add.js multiple times with the variant id and quantity and it returns a success message, but I call cart.js to retrieve the items, the cart is always empty.



      I'm adding items to the cart like this:



      $cartAPI = 'https://'.$shopKey.':'.$shopSecret.'@'.$shopUrl.'/cart/add.js';
      $request = $client->request('POST', $cartAPI, [
      'form_params' => [
      'id' => (int) $productID,
      'quantity' => 1
      ]
      ]);


      And retrieving cart like this:



      $cartAPI = 'https://'.$shopKey.':'.$shopSecret.'@'.$shopUrl.'/cart.js';
      $request = $client->get($cartAPI);


      I tried to include cookies in the Guzzle call like so
      $this->client = new Client(['cookies' => true]); and both calls use the same client instance, but it still returns an empty cart.



      I don't use CloudFlare or any Caching mechanism for this.



      What am I doing wrong?










      share|improve this question















      Using this API: https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#add-to-cart



      From my messenger bot I call cart/add.js multiple times with the variant id and quantity and it returns a success message, but I call cart.js to retrieve the items, the cart is always empty.



      I'm adding items to the cart like this:



      $cartAPI = 'https://'.$shopKey.':'.$shopSecret.'@'.$shopUrl.'/cart/add.js';
      $request = $client->request('POST', $cartAPI, [
      'form_params' => [
      'id' => (int) $productID,
      'quantity' => 1
      ]
      ]);


      And retrieving cart like this:



      $cartAPI = 'https://'.$shopKey.':'.$shopSecret.'@'.$shopUrl.'/cart.js';
      $request = $client->get($cartAPI);


      I tried to include cookies in the Guzzle call like so
      $this->client = new Client(['cookies' => true]); and both calls use the same client instance, but it still returns an empty cart.



      I don't use CloudFlare or any Caching mechanism for this.



      What am I doing wrong?







      ajax api shopify






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 at 20:24









      marc_s

      568k12810991249




      568k12810991249










      asked Nov 11 at 18:41









      Alucard

      6781924




      6781924






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          You are doing something wrong. Why would you make a call to Shopify with a key and a secret? Are you not selling yourself a little short here? I mean if I examined your source code, and saw a key and secret, I would be able to use that to do anything I want. Is that the goal here? Allowing anyone on the Internet to make you look silly? Shopify has a button you can place anywhere to add products to a cart. You can use that. It is secure.






          share|improve this answer




















          • Thank you for your comment. Appreciated. You will not find the secret key if you examined my code. Credentials are put in a unversioned .env file and I couldn't put any button since this is for a chatbot. Now, can we focus on the matter here?
            – Alucard
            Nov 12 at 0:23


















          up vote
          0
          down vote



          accepted










          Solution i figured out here for anyone having the same issue. I needed to use the cart cookie. Ended up doing something like:



          $cartCookie = Cache::tags(['user:' .$userId, 'cookies'])->get('cart');

          if (!$cartCookie)
          $client = new Client(['cookies' => true]);
          else
          $cookieJar = CookieJar::fromArray([
          'cart' => $cartCookie,
          ], conf('shop.url'));
          $client = new Client(['cookies' => $cookieJar]);



          Cookie is stored in the cache the first time the user adds an item to the cart and used for recurrent adds. I had to put it in Cache since I'm building a chatbot but you can just use cookies from your browser.






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251957%2fshopify-api-returns-empty-cart%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            You are doing something wrong. Why would you make a call to Shopify with a key and a secret? Are you not selling yourself a little short here? I mean if I examined your source code, and saw a key and secret, I would be able to use that to do anything I want. Is that the goal here? Allowing anyone on the Internet to make you look silly? Shopify has a button you can place anywhere to add products to a cart. You can use that. It is secure.






            share|improve this answer




















            • Thank you for your comment. Appreciated. You will not find the secret key if you examined my code. Credentials are put in a unversioned .env file and I couldn't put any button since this is for a chatbot. Now, can we focus on the matter here?
              – Alucard
              Nov 12 at 0:23















            up vote
            1
            down vote













            You are doing something wrong. Why would you make a call to Shopify with a key and a secret? Are you not selling yourself a little short here? I mean if I examined your source code, and saw a key and secret, I would be able to use that to do anything I want. Is that the goal here? Allowing anyone on the Internet to make you look silly? Shopify has a button you can place anywhere to add products to a cart. You can use that. It is secure.






            share|improve this answer




















            • Thank you for your comment. Appreciated. You will not find the secret key if you examined my code. Credentials are put in a unversioned .env file and I couldn't put any button since this is for a chatbot. Now, can we focus on the matter here?
              – Alucard
              Nov 12 at 0:23













            up vote
            1
            down vote










            up vote
            1
            down vote









            You are doing something wrong. Why would you make a call to Shopify with a key and a secret? Are you not selling yourself a little short here? I mean if I examined your source code, and saw a key and secret, I would be able to use that to do anything I want. Is that the goal here? Allowing anyone on the Internet to make you look silly? Shopify has a button you can place anywhere to add products to a cart. You can use that. It is secure.






            share|improve this answer












            You are doing something wrong. Why would you make a call to Shopify with a key and a secret? Are you not selling yourself a little short here? I mean if I examined your source code, and saw a key and secret, I would be able to use that to do anything I want. Is that the goal here? Allowing anyone on the Internet to make you look silly? Shopify has a button you can place anywhere to add products to a cart. You can use that. It is secure.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 23:11









            David Lazar

            5,39831426




            5,39831426











            • Thank you for your comment. Appreciated. You will not find the secret key if you examined my code. Credentials are put in a unversioned .env file and I couldn't put any button since this is for a chatbot. Now, can we focus on the matter here?
              – Alucard
              Nov 12 at 0:23

















            • Thank you for your comment. Appreciated. You will not find the secret key if you examined my code. Credentials are put in a unversioned .env file and I couldn't put any button since this is for a chatbot. Now, can we focus on the matter here?
              – Alucard
              Nov 12 at 0:23
















            Thank you for your comment. Appreciated. You will not find the secret key if you examined my code. Credentials are put in a unversioned .env file and I couldn't put any button since this is for a chatbot. Now, can we focus on the matter here?
            – Alucard
            Nov 12 at 0:23





            Thank you for your comment. Appreciated. You will not find the secret key if you examined my code. Credentials are put in a unversioned .env file and I couldn't put any button since this is for a chatbot. Now, can we focus on the matter here?
            – Alucard
            Nov 12 at 0:23













            up vote
            0
            down vote



            accepted










            Solution i figured out here for anyone having the same issue. I needed to use the cart cookie. Ended up doing something like:



            $cartCookie = Cache::tags(['user:' .$userId, 'cookies'])->get('cart');

            if (!$cartCookie)
            $client = new Client(['cookies' => true]);
            else
            $cookieJar = CookieJar::fromArray([
            'cart' => $cartCookie,
            ], conf('shop.url'));
            $client = new Client(['cookies' => $cookieJar]);



            Cookie is stored in the cache the first time the user adds an item to the cart and used for recurrent adds. I had to put it in Cache since I'm building a chatbot but you can just use cookies from your browser.






            share|improve this answer
























              up vote
              0
              down vote



              accepted










              Solution i figured out here for anyone having the same issue. I needed to use the cart cookie. Ended up doing something like:



              $cartCookie = Cache::tags(['user:' .$userId, 'cookies'])->get('cart');

              if (!$cartCookie)
              $client = new Client(['cookies' => true]);
              else
              $cookieJar = CookieJar::fromArray([
              'cart' => $cartCookie,
              ], conf('shop.url'));
              $client = new Client(['cookies' => $cookieJar]);



              Cookie is stored in the cache the first time the user adds an item to the cart and used for recurrent adds. I had to put it in Cache since I'm building a chatbot but you can just use cookies from your browser.






              share|improve this answer






















                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                Solution i figured out here for anyone having the same issue. I needed to use the cart cookie. Ended up doing something like:



                $cartCookie = Cache::tags(['user:' .$userId, 'cookies'])->get('cart');

                if (!$cartCookie)
                $client = new Client(['cookies' => true]);
                else
                $cookieJar = CookieJar::fromArray([
                'cart' => $cartCookie,
                ], conf('shop.url'));
                $client = new Client(['cookies' => $cookieJar]);



                Cookie is stored in the cache the first time the user adds an item to the cart and used for recurrent adds. I had to put it in Cache since I'm building a chatbot but you can just use cookies from your browser.






                share|improve this answer












                Solution i figured out here for anyone having the same issue. I needed to use the cart cookie. Ended up doing something like:



                $cartCookie = Cache::tags(['user:' .$userId, 'cookies'])->get('cart');

                if (!$cartCookie)
                $client = new Client(['cookies' => true]);
                else
                $cookieJar = CookieJar::fromArray([
                'cart' => $cartCookie,
                ], conf('shop.url'));
                $client = new Client(['cookies' => $cookieJar]);



                Cookie is stored in the cache the first time the user adds an item to the cart and used for recurrent adds. I had to put it in Cache since I'm building a chatbot but you can just use cookies from your browser.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 0:27









                Alucard

                6781924




                6781924



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251957%2fshopify-api-returns-empty-cart%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto