小程序商品加入購物車
用于(yu)小程序(xu)的商品(pin)加入購物車操作
使用步驟
第一步:配置接口地址
在 config.js 文件(jian)中找(zhao)到 config 設(she)置
第二步:檢查接口定義
搜索 shop_add_cart 接口是否已(yi)經(jing)存在(zai)定(ding)義(有(you)的(de)小程序模(mo)板已(yi)經(jing)定(ding)義)
如果沒有定義,在 config 底(di)部新(xin)增以下代碼:
shopAddCartUrl: getApiUrl('shop_add_cart'),并復制紅色部分(fen)待用。如果已經(jing)存在(zai),則直接復制使用即(ji)可。
第三步:接口調用請求
如果定義名稱(cheng)不同,使用第二步復制的內容替(ti)換紅框部(bu)分
/**
* 加(jia)入購物(wu)車(che)(addCart) && 立即購買(buyNow) && 確認(ren)規格選擇(specType)
* 接收參數
* 無需接收參數(shu)(shu),從頁面數(shu)(shu)據中取值
* 接口傳參
* product_id:購(gou)買的商品ID
* product_num:購買的商品數量
* spec_value_id:購買的商品(pin)規格ID
*/
onConfirmSubmit() {
let _this = this;
let detail = _this.data.detail;
// 提交數據
let postData = {
product_id: detail.aid,
product_num: _this.data.product_num,
spec_value_id: detail.spec_value_id ? detail.spec_value_id : 0
};
// 提交類型(xing)
let submitType = _this.data.buttonType;
// 加入購(gou)物車
if ('addCart' == submitType) {
App._requestPost(_this, App.globalData.config.shopAddCartUrl, postData,
function(res) {
_this.setData({
showBottomPopup: !_this.data.showBottomPopup,
cart_total_num: res.data.cart_total_num
});
App.showSuccess('已(yi)添加至購物車');
},
function(res) {
App.showError(res.msg);
}
);
}
// 立即(ji)購(gou)買
else if ('buyNow' == submitType) {
App._requestPost(_this, App.globalData.config.shopBuyNowUrl, postData,
function(res) {
wx.redirectTo({ url: res.url });
},
function(res) {
App.showError(res.msg);
}
);
}
// 確(que)認規格選擇
else if ('specType' == submitType) {
_this.setData({
showBottomPopup: !_this.data.showBottomPopup
})
}
},
接口傳值
product_id:購買的商品ID,必傳
product_num:購買的商品數量,必傳
spec_value_id:購買的商品(pin)規格ID,選填,無規格傳(chuan)0
文檔最后更新時間:2026-01-13 11:59:49
未解決你的問題?請到「問答社區」反饋你遇到的問題
