Fix discounts on order put

This commit is contained in:
Mark Pinkster 2026-01-10 17:44:11 +01:00
parent cadd9d962e
commit 61b0970465
2 changed files with 47 additions and 5 deletions

View File

@ -22,6 +22,11 @@ function handleCreateOrder(): void
$input['payment_method'] = 'cod'; $input['payment_method'] = 'cod';
$input['payment_method_title'] = 'Sales Panel Order'; $input['payment_method_title'] = 'Sales Panel Order';
$input['status'] = 'on-hold'; $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 // Check for existing customer
$existing = $woocommerce->get('customers', ['email' => $email]); $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' => '_wc_order_attribution_utm_campaign', 'value' => $mediacode];
$input['meta_data'][] = ['key' => 'Mediacode', 'value' => $mediacode]; $input['meta_data'][] = ['key' => 'Mediacode', 'value' => $mediacode];
$input['meta_data'][] = ['key' => 'Bron', 'value' => 'SalesPanel']; $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 // Create the order
$order = $woocommerce->post('orders', $input); $order = $woocommerce->post('orders', $input);

View File

@ -91,6 +91,7 @@ const FormsComponent = {
/** /**
* Build order payload from form data * Build order payload from form data
* Includes prices from sales screen to ensure discounts and free items are applied correctly
* @returns {Object} * @returns {Object}
*/ */
buildOrderPayload() { buildOrderPayload() {
@ -119,11 +120,44 @@ const FormsComponent = {
email: this.form.email, email: this.form.email,
phone: this.form.phone phone: this.form.phone
}, },
line_items: this.cart.map(i => ({ line_items: this.cart.map(i => {
product_id: i.id, // Calculate price excluding VAT (21%)
variation_id: i.variation_id || 0, const priceExclVat = (parseFloat(i.price) / 1.21).toFixed(4);
quantity: 1
})) // 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;
})
}; };
}, },