javascript - Shopify Access a product with its id on thank you page without using 'admin' in url - Stack Overflo

I am trying to access a specific product using its id from the below url,.jsonIts giving me 404 error.

I am trying to access a specific product using its id from the below url,

.json

Its giving me 404 error.
Although, I am able to access all products as below:

.json

I have to access the product which was just processed in checkout process.
I have its id as below:

var products = Shopify.checkout.line_items;

products will contain an array of product id's only which are processed in checkout.
Now I need to access all other properties of these products.
I can surely do this:

.json

But it requires Authentication.
Any thoughts?

I am trying to access a specific product using its id from the below url,

https://tempstore.myshopify./products/1234.json

Its giving me 404 error.
Although, I am able to access all products as below:

https://tempstore.myshopify./products.json

I have to access the product which was just processed in checkout process.
I have its id as below:

var products = Shopify.checkout.line_items;

products will contain an array of product id's only which are processed in checkout.
Now I need to access all other properties of these products.
I can surely do this:

https://tempstore.myshopify./admin/products/1234.json

But it requires Authentication.
Any thoughts?

Share Improve this question asked Apr 13, 2017 at 12:03 awebartisanawebartisan 1,5141 gold badge16 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

From the frontend, you need to have the product handle to get the JSON object:

https://tempstore.myshopify./products/[handle].js

or

https://tempstore.myshopify./products/[handle].json

(Note that the returned values from the .js and .json endpoints are quite different from each other!)

Like you point out, the Shopify.checkout.line_items array of objects only has the product IDs, not the product handles. We're not pletely out-of-luck, though, because we can get the entire list of products in the store including the product handles by hitting the /products.json endpoint.

Of course, this means grabbing a potentially huge JSON object just to get information that we should've had included in the checkout line items... but unless there's some alternate source of the line item information available on the checkout page, looping through the entire list may be what you need to do.

So your end code would look something like this:

Checkout.jQuery.getJSON(  // Or whatever your preferred way of getting info is
  'https://tempstore.myshopify./products.json',
  function(prodlist){
    for(var p = 0; p < prodlist.length; p++){
      var prod = prodlist[p];
      // Find if Shopify.checkout.line_items contains prod.id, left as exercise for the reader
      if(found){ 
        Checkout.jQuery.getJSON(
          'https://tempstore.myshopify./products/' + prod.handle + '.js', 
          function(product){ 
            /* Whatever needs to be done */
          })
      }
    }

  }

)

Hope this helps!

var shop = Shopify.shop;
    var lineItems = Shopify.checkout.line_items;
        var url = 'https://' + shop + '/products.json?callback=?';
        var requiredData = [];
        $.getJSON(url).done(function(data){
            lineItems.forEach(function(lineItemProduct){
                data.products.find(function(product){
                   if(lineItemProduct.product_id == product.id){
                    requiredData.push(product);
                   }
                });
            });
        });

        console.log(requiredData);

This is how I solved it, If it helps anybody :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744662574a4586550.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>