Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -370,6 +370,8 @@ def update_quantity(): | |
| 370 | 
             
            def checkout():
         | 
| 371 | 
             
                email = session.get('user_email')
         | 
| 372 | 
             
                user_id = session.get('user_id')
         | 
|  | |
|  | |
| 373 | 
             
                if not email or not user_id:
         | 
| 374 | 
             
                    return jsonify({"success": False, "message": "User not logged in"})
         | 
| 375 |  | 
| @@ -385,7 +387,7 @@ def checkout(): | |
| 385 | 
             
                        return jsonify({"success": False, "message": "Cart is empty"})
         | 
| 386 |  | 
| 387 | 
             
                    # Calculate the total price of the order
         | 
| 388 | 
            -
                    total_price = sum(item['Price__c'] for item in cart_items)
         | 
| 389 |  | 
| 390 | 
             
                    # Format order details including instructions
         | 
| 391 | 
             
                    order_details = []
         | 
| @@ -405,11 +407,31 @@ def checkout(): | |
| 405 | 
             
                    }
         | 
| 406 | 
             
                    sf.Order__c.create(order_data)
         | 
| 407 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 408 | 
             
                    # Clear the cart after placing the order
         | 
| 409 | 
             
                    for item in cart_items:
         | 
| 410 | 
             
                        sf.Cart_Item__c.delete(item["Id"])
         | 
| 411 |  | 
| 412 | 
             
                    return jsonify({"success": True, "message": "Order placed successfully!"})
         | 
|  | |
| 413 | 
             
                except Exception as e:
         | 
| 414 | 
             
                    print(f"Error during checkout: {str(e)}")
         | 
| 415 | 
             
                    return jsonify({"success": False, "error": str(e)})
         | 
|  | |
| 370 | 
             
            def checkout():
         | 
| 371 | 
             
                email = session.get('user_email')
         | 
| 372 | 
             
                user_id = session.get('user_id')
         | 
| 373 | 
            +
                
         | 
| 374 | 
            +
                # Check if the user is logged in
         | 
| 375 | 
             
                if not email or not user_id:
         | 
| 376 | 
             
                    return jsonify({"success": False, "message": "User not logged in"})
         | 
| 377 |  | 
|  | |
| 387 | 
             
                        return jsonify({"success": False, "message": "Cart is empty"})
         | 
| 388 |  | 
| 389 | 
             
                    # Calculate the total price of the order
         | 
| 390 | 
            +
                    total_price = sum(item['Price__c'] * item['Quantity__c'] for item in cart_items)  # Updated to multiply by quantity
         | 
| 391 |  | 
| 392 | 
             
                    # Format order details including instructions
         | 
| 393 | 
             
                    order_details = []
         | 
|  | |
| 407 | 
             
                    }
         | 
| 408 | 
             
                    sf.Order__c.create(order_data)
         | 
| 409 |  | 
| 410 | 
            +
                    # Fetch the current Reward_Points__c for the user
         | 
| 411 | 
            +
                    customer_record = sf.query(f"""
         | 
| 412 | 
            +
                        SELECT Reward_Points__c FROM Customer_Login__c
         | 
| 413 | 
            +
                        WHERE Email__c = '{email}'
         | 
| 414 | 
            +
                    """)
         | 
| 415 | 
            +
                    customer = customer_record.get("records", [])[0] if customer_record else None
         | 
| 416 | 
            +
             | 
| 417 | 
            +
                    if customer:
         | 
| 418 | 
            +
                        # Calculate 10% of the subtotal
         | 
| 419 | 
            +
                        reward_points_to_add = total_price * 0.10
         | 
| 420 | 
            +
             | 
| 421 | 
            +
                        # Add the new reward points to the existing value
         | 
| 422 | 
            +
                        new_reward_points = customer['Reward_Points__c'] + reward_points_to_add if customer.get('Reward_Points__c') else reward_points_to_add
         | 
| 423 | 
            +
             | 
| 424 | 
            +
                        # Update the Reward_Points__c field in Salesforce
         | 
| 425 | 
            +
                        sf.Customer_Login__c.update(customer['Id'], {
         | 
| 426 | 
            +
                            "Reward_Points__c": new_reward_points
         | 
| 427 | 
            +
                        })
         | 
| 428 | 
            +
             | 
| 429 | 
             
                    # Clear the cart after placing the order
         | 
| 430 | 
             
                    for item in cart_items:
         | 
| 431 | 
             
                        sf.Cart_Item__c.delete(item["Id"])
         | 
| 432 |  | 
| 433 | 
             
                    return jsonify({"success": True, "message": "Order placed successfully!"})
         | 
| 434 | 
            +
                
         | 
| 435 | 
             
                except Exception as e:
         | 
| 436 | 
             
                    print(f"Error during checkout: {str(e)}")
         | 
| 437 | 
             
                    return jsonify({"success": False, "error": str(e)})
         |