最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Retrieve product information as JSON with Stencil Utils - Stack Overflow

programmeradmin4浏览0评论

Using stencil-utils, I can retrieve product information for a given product ID. However, I can't find any documentation on how to retrieve the information as a JSON response as opposed to an HTML template.

Right now, I'm using the following code:

utils.api.product.getById(
    847,
    {},
    (err, resp) => {
        console.log(resp);
    }
)

I'm hoping there's a parameter I can pass in the params object that will return the response as JSON so I can extract just the information I need about the product.

Using stencil-utils, I can retrieve product information for a given product ID. However, I can't find any documentation on how to retrieve the information as a JSON response as opposed to an HTML template.

Right now, I'm using the following code:

utils.api.product.getById(
    847,
    {},
    (err, resp) => {
        console.log(resp);
    }
)

I'm hoping there's a parameter I can pass in the params object that will return the response as JSON so I can extract just the information I need about the product.

Share Improve this question asked Jan 29, 2016 at 17:24 dstaleydstaley 1,0521 gold badge14 silver badges36 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Using { params: { debug: "context" } } will work great on the local environment created using stencil start, however once you bundle & upload your theme to a live site, it stops working. The debugging tools debug: "context" and debug: "bar" are disabled on production.

After reaching out to bigmerce support, which originally linked me to this SO question, it seems this is their proposed workflow:

You will have to use a dummy handlebars template, include the variables you need, and use the custom handlebars helper provided by bigmerce, {{json}} - which seems to just run JSON.stringify() - the helper is defined here.

utils.api.product.getById(
    847, { template: 'path/to/template' }, (err, resp) => {
        // Will print the name of the product.
        console.log(resp.product.title);
    });

I have had success with path/to/template being custom/template-name and placing the handlebars template in the templates/ponents/custom folder. I have not tested passing it a template from another source.

So it looks like you can pass additional parameters by adding a param object to the options. With debug: "context", you can retrieve the entire context of the page, and you can then get the product information by accessing response.product.

utils.api.product.getById(
    847,
    { params: { debug: "context" } },
    (err, resp) => {
        // Will print the name of the product.
        console.log(resp.product.title);
    }
)
发布评论

评论列表(0)

  1. 暂无评论