Subbu1304 commited on
Commit
de10225
·
verified ·
1 Parent(s): e6ee870

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -48
app.py CHANGED
@@ -934,62 +934,88 @@ def get_addons():
934
  except Exception as e:
935
  print(f"Error fetching add-ons: {e}")
936
  return jsonify({"success": False, "error": "Unable to fetch add-ons. Please try again later."})
937
-
938
- @app.route("/cart/update_quantity", methods=["POST"])
939
  def update_quantity():
940
- data = request.json # Extract JSON data from the request
941
- email = data.get('email')
942
- item_name = data.get('item_name')
943
- try:
944
- # Convert quantity to an integer
945
- quantity = int(data.get('quantity'))
946
- except (ValueError, TypeError):
947
- return jsonify({"success": False, "error": "Invalid quantity provided."}), 400
948
-
949
- # Validate inputs
950
- if not email or not item_name or quantity is None:
951
- return jsonify({"success": False, "error": "Email, item name, and quantity are required."}), 400
952
-
953
  try:
954
- # Query the cart item in Salesforce
955
- cart_items = sf.query(
956
- f"SELECT Id, Quantity__c, Price__c, Base_Price__c, Add_Ons_Price__c FROM Cart_Item__c "
957
- f"WHERE Customer_Email__c = '{email}' AND Name = '{item_name}'"
958
- )['records']
959
-
960
- if not cart_items:
961
- return jsonify({"success": False, "error": "Cart item not found."}), 404
962
 
963
- # Retrieve the first matching record
964
- cart_item_id = cart_items[0]['Id']
965
- base_price = cart_items[0]['Base_Price__c']
966
- addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
967
-
968
- # Calculate the new item price
969
- new_item_price = (base_price * quantity) + addons_price
970
 
971
- # Update the record in Salesforce
972
- sf.Cart_Item__c.update(cart_item_id, {
973
- "Quantity__c": quantity,
974
- "Price__c": new_item_price, # Update base price
975
- })
976
 
977
- # Recalculate the subtotal for all items in the cart
978
- cart_items = sf.query(f"""
979
- SELECT Price__c, Add_Ons_Price__c
980
- FROM Cart_Item__c
981
- WHERE Customer_Email__c = '{email}'
982
- """)['records']
983
- new_subtotal = sum(item['Price__c'] for item in cart_items)
984
-
985
- # Return updated item price and subtotal
986
- return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal})
987
- print(f"New item price: {new_item_price}, New subtotal: {new_subtotal}")
988
- return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal})
989
 
990
  except Exception as e:
991
- print(f"Error updating quantity: {str(e)}")
992
  return jsonify({"success": False, "error": str(e)}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
993
  @app.route("/checkout", methods=["POST"])
994
  def checkout():
995
  email = session.get('user_email')
 
934
  except Exception as e:
935
  print(f"Error fetching add-ons: {e}")
936
  return jsonify({"success": False, "error": "Unable to fetch add-ons. Please try again later."})
937
+ @app.route('/cart/update_quantity', methods=['POST'])
 
938
  def update_quantity():
 
 
 
 
 
 
 
 
 
 
 
 
 
939
  try:
940
+ data = request.get_json() # Get JSON data from the frontend
941
+ item_name = data['item_name']
942
+ customer_email = data['email']
943
+ quantity = int(data['quantity']) # Ensure quantity is cast to an integer
 
 
 
 
944
 
945
+ # Update the quantity in Salesforce or your database
946
+ cart_item = find_cart_item(item_name, customer_email) # Custom function to find the cart item
947
+
948
+ if cart_item:
949
+ # Update the quantity in Salesforce or your database
950
+ cart_item['Quantity__c'] = quantity
951
+ cart_item['Price__c'] = cart_item['Base_Price__c'] * quantity # Calculate new price based on updated quantity
952
 
953
+ # Save changes to Salesforce
954
+ update_cart_item_in_salesforce(cart_item) # Custom function to update Salesforce
 
 
 
955
 
956
+ # Return a success response with the updated data
957
+ return jsonify({"success": True, "new_item_price": cart_item['Price__c']})
958
+ else:
959
+ return jsonify({"success": False, "error": "Cart item not found."}), 400
 
 
 
 
 
 
 
 
960
 
961
  except Exception as e:
 
962
  return jsonify({"success": False, "error": str(e)}), 500
963
+
964
+ # @app.route("/cart/update_quantity", methods=["POST"])
965
+ # def update_quantity():
966
+ # data = request.json # Extract JSON data from the request
967
+ # email = data.get('email')
968
+ # item_name = data.get('item_name')
969
+ # try:
970
+ # # Convert quantity to an integer
971
+ # quantity = int(data.get('quantity'))
972
+ # except (ValueError, TypeError):
973
+ # return jsonify({"success": False, "error": "Invalid quantity provided."}), 400
974
+
975
+ # # Validate inputs
976
+ # if not email or not item_name or quantity is None:
977
+ # return jsonify({"success": False, "error": "Email, item name, and quantity are required."}), 400
978
+
979
+ # try:
980
+ # # Query the cart item in Salesforce
981
+ # cart_items = sf.query(
982
+ # f"SELECT Id, Quantity__c, Price__c, Base_Price__c, Add_Ons_Price__c FROM Cart_Item__c "
983
+ # f"WHERE Customer_Email__c = '{email}' AND Name = '{item_name}'"
984
+ # )['records']
985
+
986
+ # if not cart_items:
987
+ # return jsonify({"success": False, "error": "Cart item not found."}), 404
988
+
989
+ # # Retrieve the first matching record
990
+ # cart_item_id = cart_items[0]['Id']
991
+ # base_price = cart_items[0]['Base_Price__c']
992
+ # addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
993
+
994
+ # # Calculate the new item price
995
+ # new_item_price = (base_price * quantity) + addons_price
996
+
997
+ # # Update the record in Salesforce
998
+ # sf.Cart_Item__c.update(cart_item_id, {
999
+ # "Quantity__c": quantity,
1000
+ # "Price__c": new_item_price, # Update base price
1001
+ # })
1002
+
1003
+ # # Recalculate the subtotal for all items in the cart
1004
+ # cart_items = sf.query(f"""
1005
+ # SELECT Price__c, Add_Ons_Price__c
1006
+ # FROM Cart_Item__c
1007
+ # WHERE Customer_Email__c = '{email}'
1008
+ # """)['records']
1009
+ # new_subtotal = sum(item['Price__c'] for item in cart_items)
1010
+
1011
+ # # Return updated item price and subtotal
1012
+ # return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal})
1013
+ # print(f"New item price: {new_item_price}, New subtotal: {new_subtotal}")
1014
+ # return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal})
1015
+
1016
+ # except Exception as e:
1017
+ # print(f"Error updating quantity: {str(e)}")
1018
+ # return jsonify({"success": False, "error": str(e)}), 500
1019
  @app.route("/checkout", methods=["POST"])
1020
  def checkout():
1021
  email = session.get('user_email')