将对象添加到购物车

时间:2013-11-18 11:01:46

标签: ios objective-c magento

在我的应用程序中,我正在使用Magento创建一个电子商务商店。要使用Magento我正在使用这个库LogNMagento。有了这个库,我正确地连接到Magento服务器,我正在创建一个购物车,现在我需要将产品添加到我刚刚创建的购物车中。要将产品添加到此购物车,我正在使用此代码:

- (void)createCarriageWithProductID:(NSString *)productID {
    Magento.service.storeID = @1;
    [Magento.service startSession];
    self.qty = @1;
    [Magento call:@[@"cart.create"]
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              Magento.service.cartID = responseObject;
              NSLog(@"cartID: %@", Magento.service.cartID);
              self.cartId = Magento.service.cartID;
              [Magento call:@[@"cart_product.add", @{
                                  @"quoteId": Magento.service.cartID,
                                  @"products": @[productID, self.qty]
                                  }]
                    success:^(AFHTTPRequestOperation *operation, id responseObject) {
                        NSLog(@"Prodotto aggiunto");
                    }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                        NSLog(@"error cart_product.add: %@", error.localizedDescription);
                    }];
          }
          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"error cart.create: %@", error.localizedDescription);
          }];
}

但是当我尝试运行该应用并将产品添加到购物车时,我收到此消息:

2013-11-18 11:54:33.549 BitmamaShop[1410:70b] got session b483cf4f44cab768806642ba9a92b405
2013-11-18 11:54:34.701 BitmamaShop[1410:70b] cartID: 41
2013-11-18 11:54:35.660 BitmamaShop[1410:70b] error cart_product.add: SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)

前两条消息向我显示该应用程序与Magento商店正常工作,但当我尝试进行下一步操作时,它会向我显示最后一条消息。我想这个问题是因为我没有正确创建添加产品的请求。 要了解我正在使用哪种API,您可以here了解cart_create.add API的工作原理。你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我发现使用php查看了我发送的XML,并将其与我发送到Web服务器的XML进行了比较,所以我用这种方式解决了:

#import "CreateCarriage.h"
#import "AppDelegate.h"
#import "Magento.h"

@implementation CreateCarriage

- (void)createCarriageWithProductID:(NSString *)productID {
    Magento.service.storeID = @1;
    [Magento.service startSession];
    if (!appDelegate.cartID) {
        [Magento call:@[@"cart.create"]
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  Magento.service.cartID = responseObject;
                  NSLog(@"cartID: %@", Magento.service.cartID);
                  self.cartId = Magento.service.cartID;
                  [Magento call:@[@"cart_product.add", Magento.service.cartID,@[@{@"product_id": productID, @"qty": self.qty}]]
                        success:^(AFHTTPRequestOperation *operation, id responseObject) {
                            NSLog(@"Prodotto aggiunto");
                            [Magento call:@[@"cart.info", @{@"quoteId": Magento.service.cartID}]
                                  success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                      [self getListOfProductsInCart:responseObject];
                                  }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                      NSLog(@"Errore: %@", error.localizedDescription);
                                  }];

                        }
                        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                            NSLog(@"error cart_product.add: %@", error.localizedDescription);
                        }];
              }
              failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  NSLog(@"error cart.create: %@", error.localizedDescription);
              }];
    } else {
        [Magento call:@[@"cart_product.add", appDelegate.cartID,@[@{@"product_id": productID, @"qty": self.qty}]]
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  NSLog(@"Prodotto aggiunto");
                  [Magento call:@[@"cart.info", @{@"quoteId": Magento.service.cartID}]
                        success:^(AFHTTPRequestOperation *operation, id responseObject) {
                            [self getListOfProductsInCart:responseObject];
                        }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                            NSLog(@"Errore: %@", error.localizedDescription);
                        }];

              }
              failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  NSLog(@"error cart_product.add: %@", error.localizedDescription);
              }];
    }
}

- (void)getListOfProductsInCart:(id)productList {
    NSDictionary *dictCart = productList;
    NSArray *items = [dictCart objectForKey:@"items"];
    NSLog(@"%d", items.count);
//    NSDictionary *dictItems = [items firstObject];
//    NSLog(@"name: %@\nqty: %@", [dictItems objectForKey:@"name"], [dictItems objectForKey:@"qty"]);

}

通过这种方式,我生成了正确的XML,并将产品添加到购物车中。在代码中我也映射了一个方法来获取购物车的信息。我希望它对某人有用。