fix: checkout improvements

1. Hidden fields now respected in SPA
   - Added isFieldHidden helper to check if PHP sets type to 'hidden'
   - Country/state/city fields conditionally rendered based on API response
   - Set default country value for hidden country fields (Indonesia-only stores)

2. Coupon discount now shows correct amount
   - Added calculate_totals() before reading discount
   - Changed coupons response to include {code, discount, type} per coupon
   - Added discount_total at root level for frontend compatibility

3. Order details page now shows shipping info and AWB tracking
   - Added shipping_lines, tracking_number, tracking_url to Order interface
   - Added Shipping Method section with courier name and cost
   - Added AWB tracking section for processing/completed orders
   - Track Shipment button with link to tracking URL
This commit is contained in:
Dwindi Ramadhana
2026-01-08 20:51:26 +07:00
parent 26faa008cb
commit e8c60b3a09
3 changed files with 190 additions and 73 deletions

View File

@@ -131,6 +131,21 @@ class CartController extends WP_REST_Controller {
$cart = WC()->cart;
// Calculate totals to ensure discounts are computed
$cart->calculate_totals();
// Format coupons with discount amounts
$coupons_with_discounts = [];
foreach ($cart->get_applied_coupons() as $coupon_code) {
$coupon = new \WC_Coupon($coupon_code);
$discount = $cart->get_coupon_discount_amount($coupon_code);
$coupons_with_discounts[] = [
'code' => $coupon_code,
'discount' => (float) $discount,
'type' => $coupon->get_discount_type(),
];
}
return new WP_REST_Response([
'items' => $this->format_cart_items($cart->get_cart()),
'totals' => [
@@ -145,7 +160,8 @@ class CartController extends WP_REST_Controller {
'total' => $cart->get_total(''),
'total_tax' => $cart->get_total_tax(),
],
'coupons' => $cart->get_applied_coupons(),
'coupons' => $coupons_with_discounts,
'discount_total' => (float) $cart->get_discount_total(), // Root level for frontend
'needs_shipping' => $cart->needs_shipping(),
'needs_payment' => $cart->needs_payment(),
'item_count' => $cart->get_cart_contents_count(),