Commit b69a3d9a by lee

u cart soldout logic

parent cdb5fa4f
......@@ -16,6 +16,8 @@ typedef void(^ShoppingCartCallBack) (NSError *error,KWMCartResult *cart);
@property (nonatomic, strong) NSMutableArray<KWMShopCartModel *> * items;
@property (nonatomic, assign) BOOL sync;
@property (nonatomic, strong) NSNumber *count;
+ (instancetype)sharedInstance;
......
......@@ -99,7 +99,7 @@
[this missionCompleted];
// if (version == this.version) {
[[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) {
// callback(error,cart);
if(this.sync)callback(error,cart);
}];
// }
} failure:^(NSURLSessionDataTask *task, NSError *error) {
......@@ -107,7 +107,7 @@
// callback(error,nil);
}];
[self addTasksObject:task];
callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:1]);
if(!this.sync)callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:1]);
}
- (void)changeProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback {
......@@ -118,14 +118,14 @@
[this missionCompleted];
// if (version == this.version) {
[[KWMShoppingCart sharedInstance] setResult:result];
// callback(nil,result);
if(this.sync)callback(nil,result);
// }
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[this missionCompleted];
// callback(error,nil);
}];
[self addTasksObject:task];
callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:quantity ? 2 : 0]);
if(!this.sync)callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:quantity ? 2 : 0]);
}
-(void)updateProductWithVariantIds:(NSArray<NSNumber *> *)variantIds quantitties:(NSArray<NSNumber *> *)quantites callback:(ShoppingCartCallBack)callback {
......@@ -136,17 +136,19 @@
[this missionCompleted];
// if (version == this.version) {
[[KWMShoppingCart sharedInstance] setResult:result];
// callback(nil,result);
if(this.sync)callback(nil,result);
// }
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[this missionCompleted];
// callback(error,nil);
}];
[self addTasksObject:task];
if(!this.sync) {
for (int i=0; i<quantites.count && self.items.count; i++) {
[self changeLocalCartWithVariantId:variantIds[i] quantity:quantites[i].integerValue flag:quantites[i].integerValue ? 2 : 0];
}
callback(nil,self.result);
}
}
- (void)deleteProductWithVariantId:(NSNumber *)variantId callback:(ShoppingCartCallBack)callback {
......
......@@ -33,6 +33,7 @@ static KWMUserModel *_shareUser;
if ([dict objectForKey:@"email"] != nil) {
_shareUser.email = [dict objectForKey:@"email"];
}
_shareUser.customerJSON = dict;
}
});
......
......@@ -84,6 +84,7 @@
-(void)setHeaderImage:(NSString *)headerImage{
_headerImage = headerImage;
NSURL *imageURL = [NSURL URLWithString:headerImage];
// self.ivHeader.contentMode = UIViewContentModeScaleToFill;
[self.ivHeader sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"ic_loading"]];
}
......
......@@ -87,7 +87,7 @@ typedef enum{
self.titleView.count = count;
[self.titleView resumeView];
}
[self.cvHome reloadData];
}
-(void)viewDidDisappear:(BOOL)animated{
......
......@@ -632,7 +632,7 @@
NSDictionary *parament = @{
@"customer_id":customer.identifier.stringValue,
@"customer_email":customer.email,
@"customer_name":customer.lastName,
@"customer_name":customer.lastName ?: @"",
@"product_title":product.title,
@"product_id":product.identifier.stringValue,
@"variant_id":self.variant.identifier.stringValue,
......
......@@ -26,6 +26,7 @@
@interface KWMBeforePayVC ()
@property (nonatomic) BUYCustomer *customer;
@property (weak, nonatomic) IBOutlet UIButton *submit;
@end
......@@ -56,6 +57,7 @@
NSNotificationCenter *notifi = [NSNotificationCenter defaultCenter];
[notifi addObserver:self selector:@selector(paySuccess) name:KWMPaySuccess object:nil];
[notifi addObserver:self selector:@selector(requestOrderPayResult) name:UIApplicationDidBecomeActiveNotification object:nil];
}
-(void)dealloc{
......
......@@ -18,8 +18,9 @@
#import "KWMStringUtil.h"
#import "KWMShoppingCart.h"
@interface KWMShopCartVC ()
@interface KWMShopCartVC ()<UIAlertViewDelegate>
@property (nonatomic) NSMutableArray *shopCartList;
@property (nonatomic, strong) NSMutableArray *soldout;
@end
@implementation KWMShopCartVC{
......@@ -50,17 +51,93 @@
self.btnRandom.layer.borderColor = [UIColor colorWithRed:78.0/255 green:78.0/255 blue:78.0/255 alpha:1.0].CGColor;
[self setTotalPrice];
__weak typeof(self) this = self;
[self checkInStockAndRefresh];
}
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC));
dispatch_after(delay, dispatch_get_main_queue(), ^{
- (void) checkInStockAndRefresh {
__weak typeof(self) this = self;
[self showLoading];
[[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) {
[this refresh];
NSArray *ids = [cart.items rx_mapWithBlock:^id(KWMShopCartModel *each) {
return each.product_id;
}];
[this.client getProductsByIds:ids completion:^(NSArray<BUYProduct *> * _Nullable products, NSError * _Nullable error) {
[this hideLoading];
// [this refresh];
[this checkInStockWithItems:cart.items products:products];
}];
}];
}
- (void) checkInStockWithItems:(NSArray<KWMShopCartModel *> *) items products:(NSArray<BUYProduct *> *) products {
self.soldout = [NSMutableArray new];
for (id item in items) {
if(![self checkInStockWithItem:item products:products]) {
[self.soldout addObject:item];
}
}
if (self.soldout.count > 0) {
[[[UIAlertView alloc] initWithTitle:nil message:@"订单中有售罄商品哦!!!" delegate:self cancelButtonTitle:@"直接删除" otherButtonTitles:@"加入愿望清单", nil] show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[[KWMShoppingCart sharedInstance] setSync:YES];
NSArray *ids = [self.soldout rx_mapWithBlock:^id(KWMShopCartModel *each) {
return each.identifier;
}];
NSArray *qus = [ids rx_mapWithBlock:^(id it){ return @(0); }];
__weak typeof(self) this = self;
[[KWMShoppingCart sharedInstance] updateProductWithVariantIds:ids quantitties:qus callback:^(NSError *error, KWMCartResult *cart) {
[[KWMShoppingCart sharedInstance] setSync:NO];
[this refresh];
if (buttonIndex == 1) {
[this addSoldOutProductToWishlist];
}
}];
}
- (void) addSoldOutProductToWishlist {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// KWMUserModel *customer = [KWMUserModel shareUser];
[self.soldout enumerateObjectsUsingBlock:^(KWMShopCartModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSDictionary * dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"customer"];
NSDictionary *parament = @{
@"customer_id":dict[@"id"],
@"customer_email":dict[@"email"],
@"customer_name":dict[@"last_name"] ?: @"",
@"product_title":obj.shopCartDict[@"product_title"],
@"product_id":obj.product_id,
@"variant_id":obj.identifier,
@"variant_sku":obj.shopCartDict[@"sku"],
@"variant_title":obj.size,
@"product_handle":obj.shopCartDict[@"handle"],
@"shop":Shopify_SHOP_DOMAIN
};
// [self.api saveWish:parament success:nil failure:nil];
[self.api saveWish:parament success:^(NSURLSessionDataTask *task, KWMAdditionalListResult *result) { } failure:^(NSURLSessionDataTask *task, NSError *error) { }];
}];
self.soldout = nil;
});
}
- (BOOL) checkInStockWithItem:(KWMShopCartModel *) item products:(NSArray<BUYProduct *> *) products {
for (BUYProduct *p in products) {
if (item.product_id.integerValue == p.identifier.integerValue) {
for (BUYProductVariant *v in p.variants) {
if ([item.size isEqualToString:v.title] && v.availableValue) {
return YES;
}
}
}
}
return NO;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
......@@ -251,6 +328,11 @@
self.shopCartList = [[KWMShoppingCart sharedInstance] items];
[self.tbvCart reloadData];
[self setTotalPrice];
// [self.client getProductsByIds:[self.shopCartList rx_mapWithBlock:^id(KWMShopCartModel *each) {
// return each.product_id;
// }] completion:^(NSArray<BUYProduct *> * _Nullable products, NSError * _Nullable error) {
//
// }];
}
//计算总价格
......
......@@ -1040,6 +1040,7 @@
<outlet property="lbPhone" destination="XAZ-es-XbK" id="3xN-ic-WmI"/>
<outlet property="lbShippingRate" destination="pVN-aG-lfu" id="YuB-rr-DOP"/>
<outlet property="lbTotalPrice" destination="fdr-W0-QCc" id="Cfi-P2-B3O"/>
<outlet property="submit" destination="vT5-Az-K57" id="fwS-UU-AZ4"/>
<outlet property="vAddressHeight" destination="zO1-RN-x44" id="VIC-wQ-AuJ"/>
<outlet property="vHasAddress" destination="gba-z6-T82" id="syf-hx-7rS"/>
<outlet property="vPayType" destination="Yux-Ro-p2d" id="qih-Ka-xDX"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment