feat: Implement default payment method for pickup and enhance payment method handling in CartTab and PaymentMethodSelector components

This commit is contained in:
sebseb7
2026-03-24 06:51:47 +01:00
parent e6f3fb7c18
commit 1840512aac
3 changed files with 43 additions and 8 deletions

View File

@@ -1,4 +1,4 @@
import React, { useEffect, useCallback } from "react";
import React, { useEffect, useCallback, useRef } from "react";
import { Box, Typography, Radio } from "@mui/material";
import { withI18n } from "../../i18n/withTranslation.js";
@@ -7,6 +7,7 @@ const PaymentMethodSelector = ({ paymentMethod, onChange, deliveryMethod, onDeli
// Calculate total amount
const subtotal = cartItems.reduce((total, item) => total + item.price * item.quantity, 0);
const totalAmount = subtotal + deliveryCost;
const previousDeliveryMethodRef = useRef(deliveryMethod);
// Handle payment method changes with automatic delivery method adjustment
const handlePaymentMethodChange = useCallback((event) => {
@@ -18,15 +19,34 @@ const PaymentMethodSelector = ({ paymentMethod, onChange, deliveryMethod, onDeli
onDeliveryMethodChange({ target: { value: "Abholung" } });
}
}
// If "Nachnahme" is selected, force delivery method to "DHL"
if (selectedPaymentMethod === "onDelivery" && deliveryMethod !== "DHL") {
if (onDeliveryMethodChange) {
onDeliveryMethodChange({ target: { value: "DHL" } });
}
}
onChange(event);
}, [deliveryMethod, onDeliveryMethodChange, onChange]);
// Handle delivery method changes - auto-switch to stripe when DHL/DPD is selected
// Handle delivery method changes - auto-switch to wire when DHL/DPD is selected
useEffect(() => {
if ((deliveryMethod === "DHL" || deliveryMethod === "DPD") && paymentMethod === "cash") {
const previousDeliveryMethod = previousDeliveryMethodRef.current;
const deliveryMethodChanged = previousDeliveryMethod !== deliveryMethod;
if (deliveryMethodChanged && (deliveryMethod === "DHL" || deliveryMethod === "DPD") && paymentMethod === "cash") {
handlePaymentMethodChange({ target: { value: "wire" /*stripe*/ } });
}
previousDeliveryMethodRef.current = deliveryMethod;
}, [deliveryMethod, paymentMethod, handlePaymentMethodChange]);
// If switching to pickup while "Nachnahme" is selected, default to in-store payment
useEffect(() => {
if (deliveryMethod === "Abholung" && paymentMethod === "onDelivery") {
handlePaymentMethodChange({ target: { value: "cash" } });
}
}, [deliveryMethod, paymentMethod, handlePaymentMethodChange]);
// Auto-switch to cash when total amount is 0
@@ -75,7 +95,7 @@ const PaymentMethodSelector = ({ paymentMethod, onChange, deliveryMethod, onDeli
id: "onDelivery",
name: t ? t('payment.methods.cashOnDelivery') : "Nachnahme",
description: t ? t('payment.methods.cashOnDeliveryDescription') : "Bezahlen Sie bei Lieferung (8,99 € Aufschlag)",
disabled: totalAmount === 0 || deliveryMethod !== "DHL",
disabled: totalAmount === 0,
icons: ["/assets/images/cash.png"],
},
{