Commit b69a3d9a by lee

u cart soldout logic

parent cdb5fa4f
...@@ -16,6 +16,8 @@ typedef void(^ShoppingCartCallBack) (NSError *error,KWMCartResult *cart); ...@@ -16,6 +16,8 @@ typedef void(^ShoppingCartCallBack) (NSError *error,KWMCartResult *cart);
@property (nonatomic, strong) NSMutableArray<KWMShopCartModel *> * items; @property (nonatomic, strong) NSMutableArray<KWMShopCartModel *> * items;
@property (nonatomic, assign) BOOL sync;
@property (nonatomic, strong) NSNumber *count; @property (nonatomic, strong) NSNumber *count;
+ (instancetype)sharedInstance; + (instancetype)sharedInstance;
......
...@@ -99,7 +99,7 @@ ...@@ -99,7 +99,7 @@
[this missionCompleted]; [this missionCompleted];
// if (version == this.version) { // if (version == this.version) {
[[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) { [[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) {
// callback(error,cart); if(this.sync)callback(error,cart);
}]; }];
// } // }
} failure:^(NSURLSessionDataTask *task, NSError *error) { } failure:^(NSURLSessionDataTask *task, NSError *error) {
...@@ -107,7 +107,7 @@ ...@@ -107,7 +107,7 @@
// callback(error,nil); // callback(error,nil);
}]; }];
[self addTasksObject:task]; [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 { - (void)changeProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback {
...@@ -118,14 +118,14 @@ ...@@ -118,14 +118,14 @@
[this missionCompleted]; [this missionCompleted];
// if (version == this.version) { // if (version == this.version) {
[[KWMShoppingCart sharedInstance] setResult:result]; [[KWMShoppingCart sharedInstance] setResult:result];
// callback(nil,result); if(this.sync)callback(nil,result);
// } // }
} failure:^(NSURLSessionDataTask *task, NSError *error) { } failure:^(NSURLSessionDataTask *task, NSError *error) {
[this missionCompleted]; [this missionCompleted];
// callback(error,nil); // callback(error,nil);
}]; }];
[self addTasksObject:task]; [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 { -(void)updateProductWithVariantIds:(NSArray<NSNumber *> *)variantIds quantitties:(NSArray<NSNumber *> *)quantites callback:(ShoppingCartCallBack)callback {
...@@ -136,17 +136,19 @@ ...@@ -136,17 +136,19 @@
[this missionCompleted]; [this missionCompleted];
// if (version == this.version) { // if (version == this.version) {
[[KWMShoppingCart sharedInstance] setResult:result]; [[KWMShoppingCart sharedInstance] setResult:result];
// callback(nil,result); if(this.sync)callback(nil,result);
// } // }
} failure:^(NSURLSessionDataTask *task, NSError *error) { } failure:^(NSURLSessionDataTask *task, NSError *error) {
[this missionCompleted]; [this missionCompleted];
// callback(error,nil); // callback(error,nil);
}]; }];
[self addTasksObject:task]; [self addTasksObject:task];
if(!this.sync) {
for (int i=0; i<quantites.count && self.items.count; i++) { 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]; [self changeLocalCartWithVariantId:variantIds[i] quantity:quantites[i].integerValue flag:quantites[i].integerValue ? 2 : 0];
} }
callback(nil,self.result); callback(nil,self.result);
}
} }
- (void)deleteProductWithVariantId:(NSNumber *)variantId callback:(ShoppingCartCallBack)callback { - (void)deleteProductWithVariantId:(NSNumber *)variantId callback:(ShoppingCartCallBack)callback {
......
...@@ -33,6 +33,7 @@ static KWMUserModel *_shareUser; ...@@ -33,6 +33,7 @@ static KWMUserModel *_shareUser;
if ([dict objectForKey:@"email"] != nil) { if ([dict objectForKey:@"email"] != nil) {
_shareUser.email = [dict objectForKey:@"email"]; _shareUser.email = [dict objectForKey:@"email"];
} }
_shareUser.customerJSON = dict;
} }
}); });
......
...@@ -84,6 +84,7 @@ ...@@ -84,6 +84,7 @@
-(void)setHeaderImage:(NSString *)headerImage{ -(void)setHeaderImage:(NSString *)headerImage{
_headerImage = headerImage; _headerImage = headerImage;
NSURL *imageURL = [NSURL URLWithString:headerImage]; NSURL *imageURL = [NSURL URLWithString:headerImage];
// self.ivHeader.contentMode = UIViewContentModeScaleToFill;
[self.ivHeader sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"ic_loading"]]; [self.ivHeader sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"ic_loading"]];
} }
......
...@@ -87,7 +87,7 @@ typedef enum{ ...@@ -87,7 +87,7 @@ typedef enum{
self.titleView.count = count; self.titleView.count = count;
[self.titleView resumeView]; [self.titleView resumeView];
} }
[self.cvHome reloadData];
} }
-(void)viewDidDisappear:(BOOL)animated{ -(void)viewDidDisappear:(BOOL)animated{
......
...@@ -632,7 +632,7 @@ ...@@ -632,7 +632,7 @@
NSDictionary *parament = @{ NSDictionary *parament = @{
@"customer_id":customer.identifier.stringValue, @"customer_id":customer.identifier.stringValue,
@"customer_email":customer.email, @"customer_email":customer.email,
@"customer_name":customer.lastName, @"customer_name":customer.lastName ?: @"",
@"product_title":product.title, @"product_title":product.title,
@"product_id":product.identifier.stringValue, @"product_id":product.identifier.stringValue,
@"variant_id":self.variant.identifier.stringValue, @"variant_id":self.variant.identifier.stringValue,
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
@interface KWMBeforePayVC () @interface KWMBeforePayVC ()
@property (nonatomic) BUYCustomer *customer; @property (nonatomic) BUYCustomer *customer;
@property (weak, nonatomic) IBOutlet UIButton *submit;
@end @end
...@@ -56,6 +57,7 @@ ...@@ -56,6 +57,7 @@
NSNotificationCenter *notifi = [NSNotificationCenter defaultCenter]; NSNotificationCenter *notifi = [NSNotificationCenter defaultCenter];
[notifi addObserver:self selector:@selector(paySuccess) name:KWMPaySuccess object:nil]; [notifi addObserver:self selector:@selector(paySuccess) name:KWMPaySuccess object:nil];
[notifi addObserver:self selector:@selector(requestOrderPayResult) name:UIApplicationDidBecomeActiveNotification object:nil]; [notifi addObserver:self selector:@selector(requestOrderPayResult) name:UIApplicationDidBecomeActiveNotification object:nil];
} }
-(void)dealloc{ -(void)dealloc{
......
...@@ -18,8 +18,9 @@ ...@@ -18,8 +18,9 @@
#import "KWMStringUtil.h" #import "KWMStringUtil.h"
#import "KWMShoppingCart.h" #import "KWMShoppingCart.h"
@interface KWMShopCartVC () @interface KWMShopCartVC ()<UIAlertViewDelegate>
@property (nonatomic) NSMutableArray *shopCartList; @property (nonatomic) NSMutableArray *shopCartList;
@property (nonatomic, strong) NSMutableArray *soldout;
@end @end
@implementation KWMShopCartVC{ @implementation KWMShopCartVC{
...@@ -50,17 +51,93 @@ ...@@ -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.btnRandom.layer.borderColor = [UIColor colorWithRed:78.0/255 green:78.0/255 blue:78.0/255 alpha:1.0].CGColor;
[self setTotalPrice]; [self setTotalPrice];
__weak typeof(self) this = self;
[self checkInStockAndRefresh];
}
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)); - (void) checkInStockAndRefresh {
dispatch_after(delay, dispatch_get_main_queue(), ^{ __weak typeof(self) this = self;
[self showLoading];
[[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) { [[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) {
[this refresh]; [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{ -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
...@@ -251,6 +328,11 @@ ...@@ -251,6 +328,11 @@
self.shopCartList = [[KWMShoppingCart sharedInstance] items]; self.shopCartList = [[KWMShoppingCart sharedInstance] items];
[self.tbvCart reloadData]; [self.tbvCart reloadData];
[self setTotalPrice]; [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 @@ ...@@ -1040,6 +1040,7 @@
<outlet property="lbPhone" destination="XAZ-es-XbK" id="3xN-ic-WmI"/> <outlet property="lbPhone" destination="XAZ-es-XbK" id="3xN-ic-WmI"/>
<outlet property="lbShippingRate" destination="pVN-aG-lfu" id="YuB-rr-DOP"/> <outlet property="lbShippingRate" destination="pVN-aG-lfu" id="YuB-rr-DOP"/>
<outlet property="lbTotalPrice" destination="fdr-W0-QCc" id="Cfi-P2-B3O"/> <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="vAddressHeight" destination="zO1-RN-x44" id="VIC-wQ-AuJ"/>
<outlet property="vHasAddress" destination="gba-z6-T82" id="syf-hx-7rS"/> <outlet property="vHasAddress" destination="gba-z6-T82" id="syf-hx-7rS"/>
<outlet property="vPayType" destination="Yux-Ro-p2d" id="qih-Ka-xDX"/> <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