Skip to main content

my.addToCart

addToCart

my.addToCart là API để thêm 1 hoặc nhiều sản phẩm vào giỏ hàng (cart) của Tiki.

Quét mã để trải nghiệm

Xem code mẫu trên Tini Studio

Demo

Trải nghiệm thử với trình giả lập bên dưới

API Params

Thuộc tínhKiểu dữ liệuBắt buộcMô tả
productsArray of ObjectsDanh sác các sản phẩm cần thêm vào cart.
successFunctionCallback function khi add vào cart được thực hiện thành công
failFunctionCallback function khi add vào cart thất bại, argument sẽ là error message
completeFunctionCallback function khi add vào cart kết thúc cho dù thành công hay thất bại.

Thuộc tính của product

Thuộc tínhKiểu dữ liệuMô tả
productIdstringID của product
quantitynumberSố lượng

Callback function payload

Fail callback payload

Thuộc tínhKiểu dữ liệuMô tả
errorstringTên lỗi
errorMessagestringNội dung chi tiết của lỗi

Sample Code

Ví dụ thêm sản phẩm vào giỏ hàng, cập nhật badge và mở màn hình giỏ hàng của Tiki.

index.txml
<view>
<block-header title="Usage" description="Add to cart" />
<view class="block-content">
<input class="form-value" placeholder="Product Id" name="productId" onInput="productIdChange"></input>
</view>
<view class="block-content">
<view class="component-item">
<button style="width: 100%;" onTap="onAddToCart">
Add to Cart
</button>
</view>
</view>
</view>
index.js
Page({
data: {
productId: undefined,
quantity: 0
},
onLoad() {
my.addIconsToNavigationBar({
icons: [
{
image: '/images/cart.png',
width: 25,
height: 25
}
],
padding: 10,
success: (res) => {
console.log(res);
},
fail: (res) => {
console.log(res);
}
});
},

productIdChange(e) {
this.setData({
productId: e.detail.value
});
},
onAddToCart() {
my.addToCart({
products: [
{
productId: this.data.productId,
quantity: 1
}
],
success: (res) => {
this.setData({
quantity: this.data.quantity + 1
});
my.addIconsToNavigationBar({
icons: [
{
image: '/images/cart.png',
width: 25,
height: 25,
badge: `${this.data.quantity}`
}
],
padding: 10,
success: (res) => {
console.log(res);
},
fail: (res) => {
console.log(res);
}
});
},
fail: (res) => {
console.log(res);
}
});
},

onCustomIconEvent(e) {
my.openScreen({ screenCode: 'TK_CART' });
}
});