Shopify 购物车值仅在我使用硬编码值时更新

问题描述 投票:0回答:1

我编写了一些代码,允许我的客户在其价值超过固定阈值时将免费产品添加到购物车。

我通过主题设置在我的文件中获取这些值。问题是,免费产品代码仅在我使用硬编码值时才有效。否则,我会在购物车 update.js 文件中收到 404 错误。

这是有效的代码:

$.ajax({
    type: 'get',
    url: '/cart.js',
    dataType: 'json',
    success: function(data) {
        var total_cart_price = 0;
        total_cart_price = data.total_price / 100;
        var free_product_id = settings.first_free_product;
        var first_free_price_threshold = settings.first_free_price_threshold;
        console.log(Free product ID: ${free_product_id} and price threshold: ${first_free_price_threshold});

        const cartItems = data.items.map(item => item.id);
        let updates = {};
        if (total_cart_price < 60) {
            updates = {
                48270424670533: 0
            };
        }
        if (total_cart_price >= 60) {
            updates = {
                48270424670533: 1
            };
        }
        fetch(window.Shopify.routes.root + 'cart/update.js', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                updates
            })
        }).then(response => {
            myCartRefresher();
            return response.json();
        }).catch((error) => {
            console.error('Error:', error);
        });
    }
});

这是没有的代码:

$.ajax({
    type: 'get',
    url: '/cart.js',
    dataType: 'json',
    success: function(data) {
        var total_cart_price = 0;
        total_cart_price = data.total_price / 100;
        var free_product_id = {{ settings.first_free_product }};
        var first_free_price_threshold = {{ settings.first_free_price_threshold }};
        console.log(Free product ID: ${free_product_id} and price threshold: ${first_free_price_threshold});

        const cartItems = data.items.map(item => item.id);
        let updates = {};
        if (total_cart_price < first_free_price_threshold) {
            updates = {
                free_product_id: 0
            };
        }

        if (total_cart_price >= first_free_price_threshold) {
            updates = {
                free_product_id: 1
            };
        }
        fetch(window.Shopify.routes.root + 'cart/update.js', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                updates
            })
        }).then(response => {
            myCartRefresher();
            return response.json();
        }).catch((error) => {
            console.error('Error:', error);
        });
    }
});

我做错了什么?

谢谢。

javascript fetch shopify shopify-api
1个回答
0
投票

键/对象的语法看起来有点破损。

试试这个:

        if (total_cart_price < first_free_price_threshold) {
            updates = {
                [free_product_id]: 0
            };
        }

        if (total_cart_price >= first_free_price_threshold) {
            updates = {
                [free_product_id]: 1
            };
        }
© www.soinside.com 2019 - 2024. All rights reserved.