diff --git a/api/actions/orders.php b/api/actions/orders.php index 13b0833..e2a8b86 100644 --- a/api/actions/orders.php +++ b/api/actions/orders.php @@ -22,6 +22,11 @@ function handleCreateOrder(): void $input['payment_method'] = 'cod'; $input['payment_method_title'] = 'Sales Panel Order'; $input['status'] = 'on-hold'; + + // IMPORTANT: Disable automatic price calculation + // This ensures WooCommerce uses the prices from the sales panel + // instead of recalculating them (which would ignore discounts/free items) + $input['set_paid'] = false; // Check for existing customer $existing = $woocommerce->get('customers', ['email' => $email]); @@ -46,6 +51,9 @@ function handleCreateOrder(): void $input['meta_data'][] = ['key' => '_wc_order_attribution_utm_campaign', 'value' => $mediacode]; $input['meta_data'][] = ['key' => 'Mediacode', 'value' => $mediacode]; $input['meta_data'][] = ['key' => 'Bron', 'value' => 'SalesPanel']; + + // Add fee line for any BOGO discounts that need to be shown + // This is handled via line_items with subtotal/total already set from frontend // Create the order $order = $woocommerce->post('orders', $input); diff --git a/js/components/forms.js b/js/components/forms.js index 6126508..0a6e052 100644 --- a/js/components/forms.js +++ b/js/components/forms.js @@ -91,6 +91,7 @@ const FormsComponent = { /** * Build order payload from form data + * Includes prices from sales screen to ensure discounts and free items are applied correctly * @returns {Object} */ buildOrderPayload() { @@ -119,11 +120,44 @@ const FormsComponent = { email: this.form.email, phone: this.form.phone }, - line_items: this.cart.map(i => ({ - product_id: i.id, - variation_id: i.variation_id || 0, - quantity: 1 - })) + line_items: this.cart.map(i => { + // Calculate price excluding VAT (21%) + const priceExclVat = (parseFloat(i.price) / 1.21).toFixed(4); + + // For free items, use originalPrice if available, otherwise use price + const originalPriceExclVat = i.originalPrice + ? (parseFloat(i.originalPrice) / 1.21).toFixed(4) + : priceExclVat; + + // Build line item object + const lineItem = { + product_id: i.id, + variation_id: i.variation_id || 0, + quantity: 1, + // Set subtotal and total to override WooCommerce's price calculation + // subtotal = price before line-level discounts (shows original value) + // total = price after line-level discounts (actual charged amount) + subtotal: i.isFree ? originalPriceExclVat : priceExclVat, + total: i.isFree ? '0.0000' : priceExclVat + }; + + // Add metadata for tracking discounts and free items + if (i.isFree) { + lineItem.meta_data = [ + { key: '_is_free_item', value: 'yes' }, + { key: '_bogo_rule_id', value: String(i.ruleId || '') }, + { key: '_original_price_incl_vat', value: String(i.originalPrice || i.price) } + ]; + } else if (i.isDiscounted) { + lineItem.meta_data = [ + { key: '_is_discounted_item', value: 'yes' }, + { key: '_original_price_incl_vat', value: String(i.originalPrice || '') }, + { key: '_bogo_rule_id', value: String(i.ruleId || '') } + ]; + } + + return lineItem; + }) }; },