Update menu.py
Browse files
    	
        menu.py
    CHANGED
    
    | @@ -177,4 +177,108 @@ def get_addons(): | |
| 177 | 
             
                    app.logger.error(f"Error fetching addons: {str(e)}")
         | 
| 178 | 
             
                    return jsonify({"success": False, "error": "An error occurred while fetching customization options."}), 500
         | 
| 179 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 180 |  | 
|  | |
| 177 | 
             
                    app.logger.error(f"Error fetching addons: {str(e)}")
         | 
| 178 | 
             
                    return jsonify({"success": False, "error": "An error occurred while fetching customization options."}), 500
         | 
| 179 |  | 
| 180 | 
            +
            @app.route('/cart/add', methods=['POST'])
         | 
| 181 | 
            +
            def add_to_cart():
         | 
| 182 | 
            +
                try:
         | 
| 183 | 
            +
                    # Get data from request
         | 
| 184 | 
            +
                    data = request.json
         | 
| 185 | 
            +
                    item_name = data.get('itemName', '').strip()
         | 
| 186 | 
            +
                    item_price = data.get('itemPrice')
         | 
| 187 | 
            +
                    item_image = data.get('itemImage')
         | 
| 188 | 
            +
                    addons = data.get('addons', [])
         | 
| 189 | 
            +
                    instructions = data.get('instructions', '')
         | 
| 190 | 
            +
                    category = data.get('category')
         | 
| 191 | 
            +
                    section = data.get('section')
         | 
| 192 | 
            +
                    quantity = data.get('quantity', 1)  # Get the quantity field from the request
         | 
| 193 | 
            +
                    customer_email = session.get('user_email')
         | 
| 194 | 
            +
             | 
| 195 | 
            +
                    # Basic validation for required fields
         | 
| 196 | 
            +
                    if not item_name or not item_price:
         | 
| 197 | 
            +
                        return jsonify({"success": False, "error": "Item name and price are required."}), 400
         | 
| 198 | 
            +
             | 
| 199 | 
            +
                    if not customer_email:
         | 
| 200 | 
            +
                        return jsonify({"success": False, "error": "User email is required."}), 400
         | 
| 201 | 
            +
             | 
| 202 | 
            +
                    # Query to check if the item is already in the cart
         | 
| 203 | 
            +
                    query = f"""
         | 
| 204 | 
            +
                        SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c 
         | 
| 205 | 
            +
                        FROM Cart_Item__c
         | 
| 206 | 
            +
                        WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
         | 
| 207 | 
            +
                    """
         | 
| 208 | 
            +
                    result = sf.query(query)
         | 
| 209 | 
            +
                    cart_items = result.get("records", [])
         | 
| 210 | 
            +
             | 
| 211 | 
            +
                    # Calculate the total price for the addons
         | 
| 212 | 
            +
                    addons_price = sum(addon['price'] for addon in addons)
         | 
| 213 | 
            +
                    new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
         | 
| 214 | 
            +
             | 
| 215 | 
            +
                    # If the item is already in the cart, update it
         | 
| 216 | 
            +
                    if cart_items:
         | 
| 217 | 
            +
                        cart_item_id = cart_items[0]['Id']
         | 
| 218 | 
            +
                        existing_quantity = cart_items[0]['Quantity__c']
         | 
| 219 | 
            +
                        existing_addons = cart_items[0].get('Add_Ons__c', "None")
         | 
| 220 | 
            +
                        existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
         | 
| 221 | 
            +
                        existing_instructions = cart_items[0].get('Instructions__c', "")
         | 
| 222 | 
            +
             | 
| 223 | 
            +
                        # Combine the new addons with the existing ones
         | 
| 224 | 
            +
                        combined_addons = existing_addons if existing_addons != "None" else ""
         | 
| 225 | 
            +
                        if new_addons:
         | 
| 226 | 
            +
                            combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
         | 
| 227 | 
            +
             | 
| 228 | 
            +
                        # Combine existing instructions with new instructions
         | 
| 229 | 
            +
                        combined_instructions = existing_instructions
         | 
| 230 | 
            +
                        if instructions:
         | 
| 231 | 
            +
                            combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
         | 
| 232 | 
            +
             | 
| 233 | 
            +
                        # Calculate total addons price
         | 
| 234 | 
            +
                        combined_addons_list = combined_addons.split("; ")
         | 
| 235 | 
            +
                        combined_addons_price = sum(
         | 
| 236 | 
            +
                            float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
         | 
| 237 | 
            +
                        )
         | 
| 238 | 
            +
             | 
| 239 | 
            +
                        # Update the cart item in Salesforce (updating quantity)
         | 
| 240 | 
            +
                        sf.Cart_Item__c.update(cart_item_id, {
         | 
| 241 | 
            +
                            "Quantity__c": existing_quantity + quantity,  # Add the selected quantity
         | 
| 242 | 
            +
                            "Add_Ons__c": combined_addons,
         | 
| 243 | 
            +
                            "Add_Ons_Price__c": combined_addons_price,
         | 
| 244 | 
            +
                            "Instructions__c": combined_instructions,
         | 
| 245 | 
            +
                            "Price__c": (existing_quantity + quantity) * item_price + combined_addons_price,
         | 
| 246 | 
            +
                            "Category__c": category,
         | 
| 247 | 
            +
                            "Section__c": section
         | 
| 248 | 
            +
                        })
         | 
| 249 | 
            +
                    else:
         | 
| 250 | 
            +
                        # If the item is not already in the cart, create a new entry
         | 
| 251 | 
            +
                        addons_string = "None"
         | 
| 252 | 
            +
                        if addons:
         | 
| 253 | 
            +
                            addons_string = new_addons
         | 
| 254 | 
            +
             | 
| 255 | 
            +
                        total_price = item_price * quantity + addons_price  # Multiply by the quantity
         | 
| 256 | 
            +
             | 
| 257 | 
            +
                        # Create new cart item in Salesforce
         | 
| 258 | 
            +
                        sf.Cart_Item__c.create({
         | 
| 259 | 
            +
                            "Name": item_name,
         | 
| 260 | 
            +
                            "Price__c": total_price,
         | 
| 261 | 
            +
                            "Base_Price__c": item_price,
         | 
| 262 | 
            +
                            "Quantity__c": quantity,  # Use the selected quantity
         | 
| 263 | 
            +
                            "Add_Ons_Price__c": addons_price,
         | 
| 264 | 
            +
                            "Add_Ons__c": addons_string,
         | 
| 265 | 
            +
                            "Image1__c": item_image,
         | 
| 266 | 
            +
                            "Customer_Email__c": customer_email,
         | 
| 267 | 
            +
                            "Instructions__c": instructions,
         | 
| 268 | 
            +
                            "Category__c": category,
         | 
| 269 | 
            +
                            "Section__c": section
         | 
| 270 | 
            +
                        })
         | 
| 271 | 
            +
             | 
| 272 | 
            +
                    return jsonify({"success": True, "message": "Item added to cart successfully."})
         | 
| 273 | 
            +
             | 
| 274 | 
            +
                except KeyError as e:
         | 
| 275 | 
            +
                    # Handle missing expected keys in request data
         | 
| 276 | 
            +
                    return jsonify({"success": False, "error": f"Missing required field: {str(e)}"}), 400
         | 
| 277 | 
            +
             | 
| 278 | 
            +
                except Exception as e:
         | 
| 279 | 
            +
                    # Log the error for debugging and return a general error message
         | 
| 280 | 
            +
                    print(f"Error adding item to cart: {str(e)}")
         | 
| 281 | 
            +
                    return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
         | 
| 282 | 
            +
             | 
| 283 | 
            +
             | 
| 284 |  |