今天有一个shopify修改的需求,在shopify中如何使用JavaScript结合开发文档实现不加入购物车直接进入订单支付环节
通过查看开发文档和尝试,发现可以实现。
获取到商品信息,然后通过/cart/create.js接口提交订单,然后将用户重定向到 Shopify 的结账页面。
// 使用 API 创建订单
fetch('/cart/create.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
line_items: [
{
variant_id: 123456789,
quantity: 1,
},
],
shipping_address: {
first_name: 'John',
last_name: 'Doe',
address1: '123 Main St',
phone: '555-555-5555',
city: 'Anytown',
province: 'CA',
country: 'Canada',
zip: 'A1B 2C3',
},
}),
})
.then((response) => response.json())
.then((data) => {
// 将用户重定向到 Shopify 的结账页面
window.location.href = '/checkout?id=' + data.token;
});