feat: Multiple fixes and features

1. Add allow_custom_avatar toggle to Customer Settings
2. Implement coupon apply/remove in Cart and Checkout pages
3. Update Cart interface with coupons array and discount_total
4. Implement Downloads page to fetch from /account/downloads API
This commit is contained in:
Dwindi Ramadhana
2026-01-04 20:03:33 +07:00
parent befacf9d29
commit 0f542ad452
13 changed files with 420 additions and 32 deletions

View File

@@ -109,3 +109,57 @@ export async function fetchCart(): Promise<Cart> {
const data = await response.json();
return data;
}
/**
* Apply coupon to cart via API
*/
export async function applyCoupon(couponCode: string): Promise<Cart> {
const { apiRoot, nonce } = getApiConfig();
const response = await fetch(`${apiRoot}/cart/apply-coupon`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': nonce,
},
credentials: 'include',
body: JSON.stringify({
coupon_code: couponCode,
}),
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.message || 'Failed to apply coupon');
}
const data = await response.json();
return data.cart;
}
/**
* Remove coupon from cart via API
*/
export async function removeCoupon(couponCode: string): Promise<Cart> {
const { apiRoot, nonce } = getApiConfig();
const response = await fetch(`${apiRoot}/cart/remove-coupon`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': nonce,
},
credentials: 'include',
body: JSON.stringify({
coupon_code: couponCode,
}),
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.message || 'Failed to remove coupon');
}
const data = await response.json();
return data.cart;
}

View File

@@ -24,12 +24,19 @@ export interface Cart {
code: string;
discount: number;
};
coupons?: {
code: string;
discount: number;
type?: string;
}[];
discount_total?: number;
shipping_total?: number;
}
interface CartStore {
cart: Cart;
isOpen: boolean;
// Actions
setCart: (cart: Cart) => void;
addItem: (item: CartItem) => void;
@@ -60,7 +67,7 @@ export const useCartStore = create<CartStore>()(
addItem: (item) =>
set((state) => {
const existingItem = state.cart.items.find((i) => i.key === item.key);
if (existingItem) {
// Update quantity if item exists
return {
@@ -74,7 +81,7 @@ export const useCartStore = create<CartStore>()(
},
};
}
// Add new item
return {
cart: {