17.5 优惠券使用
实现步骤:
第 1 步:优惠券模型
json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| [ { "id": 55, "code": "568935ab", "amount": "5.00", "status": "publish", "date_created": "2022-04-03T21:00:38", "date_created_gmt": "2022-04-03T13:00:38", "date_modified": "2022-04-03T21:00:38", "date_modified_gmt": "2022-04-03T13:00:38", "discount_type": "fixed_cart", "description": "", "date_expires": null, "date_expires_gmt": null, "usage_count": 3, "individual_use": false, "product_ids": [], "excluded_product_ids": [], "usage_limit": null, "usage_limit_per_user": null, "limit_usage_to_x_items": null, "free_shipping": true, "product_categories": [], "excluded_product_categories": [], "exclude_sale_items": false, "minimum_amount": "0.00", "maximum_amount": "0.00", "email_restrictions": [], "used_by": [ "2", "6", "6" ], "meta_data": [], "_links": { "self": [ { "href": "https://wp.ducafecat.tech/wp-json/wc/v3/coupons/55" } ], "collection": [ { "href": "https://wp.ducafecat.tech/wp-json/wc/v3/coupons" } ] } } ]
|
amount
优惠金额
CouponsModel 定义, 去掉已使用用户的解析
lib/common/models/woo/coupons_model
1 2 3
| factory CouponsModel.fromJson(Map<String, dynamic> json) => CouponsModel( ...
|
第 2 步:api 接口
lib/common/api/coupon.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import '../index.dart';
class CouponApi { static Future<CouponsModel?> couponDetail(String code) async { var res = await WPHttpService.to.get( '/coupons', params: { "code": code, }, );
List<CouponsModel> coupons = []; for (var item in res.data) { coupons.add(CouponsModel.fromJson(item)); } return coupons.isNotEmpty ? coupons.first : null; } }
|
通过商家 下发的 优惠券
号进行打折活动
第 3 步:CartService 加入优惠券操作
lib/common/services/cart.dart
1 2
| final List<CouponsModel> lineCoupons = [];
|
1 2 3 4 5 6 7 8 9 10 11
| bool applyCoupon(CouponsModel item) { int index = lineCoupons.indexWhere((element) => element.id == item.id); if (index >= 0) { return false; } lineCoupons.add(item); return true; }
|
1 2 3 4 5
| double get discount => lineCoupons.fold<double>(0, (double previousValue, CouponsModel element) { return previousValue + (double.parse(element.amount ?? "0")); });
|
第 4 步:控制器
lib/pages/cart/cart_index/controller.dart
1 2
| String couponCode = '';
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Future<void> onApplyCoupon() async { if (couponCode.isEmpty) { Loading.error("Voucher code empty."); return; } CouponsModel? coupon = await CouponApi.couponDetail(couponCode); if (coupon != null) { couponCode = ""; bool isSuccess = CartService.to.applyCoupon(coupon); if (isSuccess) { Loading.success("Coupon applied."); } else { Loading.error("Coupon is already applied."); } update(["cart_index"]); } else { Loading.error("Coupon code is not valid."); } }
|
下发优惠券号 568935ab
第 5 步:视图
lib/pages/cart/cart_index/view.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| Widget _buildCoupons() { return <Widget>[ InputWidget.textBorder( hintText: "Voucher Code", fillColor: AppColors.surface, onChanged: (value) => controller.couponCode = value, ).expanded(),
SizedBox( width: AppSpace.listItem, ),
ButtonWidget.text( LocaleKeys.gCartBtnApplyCode.tr, onTap: controller.onApplyCoupon, textColor: AppColors.highlight, textSize: 12.sp, textWeight: FontWeight.w500, ), ].toRow(); }
|
提交代码到 git