dwindown
857d6315e6
fix(orders): Use WooCommerce cart for shipping calculation in order creation
...
## Issues Fixed:
1. ✅ Shipping cost was zero in created orders
2. ✅ Live rates (UPS, Rajaongkir) not calculated
3. ✅ Shipping title shows service level (e.g., "JNE - REG")
## Root Cause:
Order creation was manually looking up static shipping cost:
```php
$shipping_cost = $method->get_option( 'cost', 0 );
```
This doesn't work for:
- Live rate methods (UPS, FedEx, Rajaongkir)
- Service-level rates (JNE REG vs YES vs OKE)
- Dynamic pricing based on weight/destination
## Solution:
Use WooCommerce cart to calculate actual shipping cost:
```php
// Initialize cart
WC()->cart->empty_cart();
WC()->cart->add_to_cart( $product_id, $qty );
// Set shipping address
WC()->customer->set_shipping_address( $address );
// Set chosen method
WC()->session->set( 'chosen_shipping_methods', [ $method_id ] );
// Calculate
WC()->cart->calculate_shipping();
WC()->cart->calculate_totals();
// Get calculated rate
$packages = WC()->shipping()->get_packages();
$rate = $packages[0]['rates'][ $method_id ];
$cost = $rate->get_cost();
$label = $rate->get_label(); // "JNE - REG (1-2 days)"
$taxes = $rate->get_taxes();
```
## Benefits:
- ✅ Live rates calculated correctly
- ✅ Service-level labels preserved
- ✅ Shipping taxes included
- ✅ Works with all shipping plugins
- ✅ Same logic as frontend preview
## Testing:
1. Create order with UPS → Shows "UPS Ground" + correct cost
2. Create order with Rajaongkir → Shows "JNE - REG" + correct cost
3. Order detail page → Shows full service name
4. Shipping cost → Matches preview calculation
2025-11-10 16:25:52 +07:00
dwindown
619fe45055
feat(orders): Add WooCommerce-native calculation endpoints
...
## Problem:
1. No shipping service options (UPS Ground, UPS Express, etc.)
2. Tax not calculated (11% PPN not showing)
3. Manual cost calculation instead of using WooCommerce core
## Root Cause:
Current implementation manually sets shipping costs from static config:
```php
$shipping_cost = $method->get_option( 'cost', 0 );
$ship_item->set_total( $shipping_cost );
```
This doesn't work for:
- Live rate methods (UPS, FedEx) - need dynamic calculation
- Tax calculation - WooCommerce needs proper context
- Service-level rates (UPS Ground vs Express)
## Solution: Use WooCommerce Native Calculation
### New Endpoints:
1. **POST /woonoow/v1/shipping/calculate**
- Calculates real-time shipping rates
- Uses WooCommerce cart + customer address
- Returns all available methods with costs
- Supports live rate plugins (UPS, FedEx)
- Returns service-level options
2. **POST /woonoow/v1/orders/preview**
- Previews order totals before creation
- Calculates: subtotal, shipping, tax, discounts, total
- Uses WooCommerce cart engine
- Respects tax settings and rates
- Applies coupons correctly
### How It Works:
```php
// Temporarily use WooCommerce cart
WC()->cart->empty_cart();
WC()->cart->add_to_cart( $product_id, $qty );
WC()->customer->set_shipping_address( $address );
WC()->cart->calculate_shipping();
WC()->cart->calculate_totals();
// Get calculated rates
$packages = WC()->shipping()->get_packages();
foreach ( $packages as $package ) {
$rates = $package['rates']; // UPS Ground, UPS Express, etc.
}
// Get totals with tax
$total = WC()->cart->get_total();
$tax = WC()->cart->get_total_tax();
```
### Benefits:
- ✅ Live shipping rates work
- ✅ Service-level options appear
- ✅ Tax calculated correctly
- ✅ Coupons applied properly
- ✅ Uses WooCommerce core logic
- ✅ No reinventing the wheel
### Next Steps (Frontend):
1. Call `/shipping/calculate` when address changes
2. Show service options in dropdown
3. Call `/orders/preview` to show totals with tax
4. Update UI to display tax breakdown
2025-11-10 15:53:58 +07:00