Fix discounts on order put
This commit is contained in:
parent
cadd9d962e
commit
61b0970465
@ -23,6 +23,11 @@ function handleCreateOrder(): void
|
|||||||
$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]);
|
||||||
$input['customer_id'] = !empty($existing) ? $existing[0]->id : 0;
|
$input['customer_id'] = !empty($existing) ? $existing[0]->id : 0;
|
||||||
@ -47,6 +52,9 @@ function handleCreateOrder(): void
|
|||||||
$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);
|
||||||
|
|
||||||
|
|||||||
@ -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 => {
|
||||||
|
// 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,
|
product_id: i.id,
|
||||||
variation_id: i.variation_id || 0,
|
variation_id: i.variation_id || 0,
|
||||||
quantity: 1
|
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;
|
||||||
|
})
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user