Add WooCommerce integration for payments, invoices, and order management (v0.11.0)
All checks were successful
Create Release Package / build-release (push) Successful in 1m11s
All checks were successful
Create Release Package / build-release (push) Successful in 1m11s
- Product sync: Virtual WC products for rooms with bidirectional linking - Cart/Checkout: Booking data in cart items, availability validation, dynamic pricing - Orders: Automatic booking creation on payment, status mapping, guest record creation - Invoices: PDF generation via mPDF, auto-attach to emails, configurable numbering - Refunds: Full refund cancels booking, partial refund records amount only - Admin: Cross-linked columns and row actions between bookings and orders - Settings: WooCommerce tab with subtabs (General, Products, Orders, Invoices) - HPOS compatibility declared for High-Performance Order Storage Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -598,12 +598,36 @@
|
||||
display: inline-block;
|
||||
padding: 3px 8px;
|
||||
border-radius: 3px;
|
||||
color: #fff;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.bnb-status-pending {
|
||||
background: #fff8e5;
|
||||
color: #9d6a00;
|
||||
}
|
||||
|
||||
.bnb-status-confirmed {
|
||||
background: #e6f4ea;
|
||||
color: #0a6e31;
|
||||
}
|
||||
|
||||
.bnb-status-checked_in {
|
||||
background: #e3f2fd;
|
||||
color: #1565c0;
|
||||
}
|
||||
|
||||
.bnb-status-checked_out {
|
||||
background: #f5f5f5;
|
||||
color: #616161;
|
||||
}
|
||||
|
||||
.bnb-status-cancelled {
|
||||
background: #ffeaea;
|
||||
color: #d63638;
|
||||
}
|
||||
|
||||
/* Room Details Meta Box */
|
||||
#bnb_room_details .form-table td label {
|
||||
display: inline-block;
|
||||
|
||||
443
assets/css/wc-integration.css
Normal file
443
assets/css/wc-integration.css
Normal file
@@ -0,0 +1,443 @@
|
||||
/**
|
||||
* WooCommerce Integration Styles
|
||||
*
|
||||
* Styles for WP BnB - WooCommerce integration
|
||||
*
|
||||
* @package Magdev\WpBnb
|
||||
*/
|
||||
|
||||
/* ==========================================================================
|
||||
Cart Item - Booking Data Display
|
||||
========================================================================== */
|
||||
|
||||
.woocommerce-cart .cart_item .bnb-booking-info {
|
||||
margin-top: 8px;
|
||||
padding: 10px;
|
||||
background: #f9f9f9;
|
||||
border-radius: 4px;
|
||||
font-size: 0.9em;
|
||||
}
|
||||
|
||||
.woocommerce-cart .cart_item .bnb-booking-info dt {
|
||||
display: inline-block;
|
||||
font-weight: 600;
|
||||
min-width: 80px;
|
||||
color: #50575e;
|
||||
}
|
||||
|
||||
.woocommerce-cart .cart_item .bnb-booking-info dd {
|
||||
display: inline-block;
|
||||
margin: 0 0 4px 0;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Checkout - Booking Summary
|
||||
========================================================================== */
|
||||
|
||||
.bnb-checkout-booking-summary {
|
||||
margin: 20px 0;
|
||||
padding: 15px;
|
||||
background: #f8f9fa;
|
||||
border: 1px solid #e1e4e8;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.bnb-checkout-booking-summary h3 {
|
||||
margin: 0 0 15px 0;
|
||||
padding: 0 0 10px 0;
|
||||
border-bottom: 1px solid #e1e4e8;
|
||||
font-size: 1.1em;
|
||||
color: #2271b1;
|
||||
}
|
||||
|
||||
.bnb-booking-item {
|
||||
margin-bottom: 12px;
|
||||
padding-bottom: 12px;
|
||||
border-bottom: 1px dashed #e1e4e8;
|
||||
}
|
||||
|
||||
.bnb-booking-item:last-child {
|
||||
margin-bottom: 0;
|
||||
padding-bottom: 0;
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.bnb-booking-item strong {
|
||||
display: block;
|
||||
font-size: 1em;
|
||||
color: #1d2327;
|
||||
}
|
||||
|
||||
.bnb-building-name {
|
||||
display: block;
|
||||
font-size: 0.85em;
|
||||
color: #787c82;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
.bnb-booking-details {
|
||||
margin-top: 8px;
|
||||
font-size: 0.9em;
|
||||
color: #50575e;
|
||||
}
|
||||
|
||||
.bnb-booking-details span {
|
||||
display: inline-block;
|
||||
margin-right: 15px;
|
||||
}
|
||||
|
||||
.bnb-booking-details span::before {
|
||||
content: "•";
|
||||
margin-right: 5px;
|
||||
color: #c3c4c7;
|
||||
}
|
||||
|
||||
.bnb-booking-details span:first-child::before {
|
||||
content: "";
|
||||
margin-right: 0;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Thank You Page - Booking Confirmation
|
||||
========================================================================== */
|
||||
|
||||
.woocommerce-booking-confirmation {
|
||||
margin: 30px 0;
|
||||
padding: 20px;
|
||||
background: #f0f8f1;
|
||||
border: 1px solid #d1e7d7;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.woocommerce-booking-confirmation h2 {
|
||||
margin: 0 0 15px 0;
|
||||
color: #00a32a;
|
||||
font-size: 1.3em;
|
||||
}
|
||||
|
||||
.woocommerce-table--booking-details {
|
||||
width: 100%;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.woocommerce-table--booking-details th {
|
||||
text-align: left;
|
||||
width: 40%;
|
||||
padding: 8px 12px 8px 0;
|
||||
color: #50575e;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.woocommerce-table--booking-details td {
|
||||
padding: 8px 0;
|
||||
}
|
||||
|
||||
.woocommerce-table--booking-details small {
|
||||
display: block;
|
||||
color: #787c82;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
.woocommerce-booking-reference {
|
||||
margin: 20px 0;
|
||||
padding: 10px 15px;
|
||||
background: #f6f7f7;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.woocommerce-booking-reference .bnb-status-badge {
|
||||
margin-left: 10px;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Status Badges
|
||||
========================================================================== */
|
||||
|
||||
.bnb-status-badge {
|
||||
display: inline-block;
|
||||
padding: 2px 8px;
|
||||
border-radius: 3px;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.bnb-status-pending {
|
||||
background: #fff8e5;
|
||||
color: #9d6a00;
|
||||
}
|
||||
|
||||
.bnb-status-confirmed {
|
||||
background: #e6f4ea;
|
||||
color: #0a6e31;
|
||||
}
|
||||
|
||||
.bnb-status-checked_in {
|
||||
background: #e3f2fd;
|
||||
color: #1565c0;
|
||||
}
|
||||
|
||||
.bnb-status-checked_out {
|
||||
background: #f5f5f5;
|
||||
color: #616161;
|
||||
}
|
||||
|
||||
.bnb-status-cancelled {
|
||||
background: #ffeaea;
|
||||
color: #d63638;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Booking Form (Frontend)
|
||||
========================================================================== */
|
||||
|
||||
.bnb-wc-booking-form {
|
||||
margin: 20px 0;
|
||||
padding: 20px;
|
||||
background: #fff;
|
||||
border: 1px solid #e1e4e8;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.bnb-wc-booking-form h3 {
|
||||
margin: 0 0 20px 0;
|
||||
padding: 0 0 15px 0;
|
||||
border-bottom: 1px solid #e1e4e8;
|
||||
}
|
||||
|
||||
.bnb-wc-booking-form .form-row {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.bnb-wc-booking-form label {
|
||||
display: block;
|
||||
margin-bottom: 5px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.bnb-wc-booking-form input[type="date"],
|
||||
.bnb-wc-booking-form input[type="number"],
|
||||
.bnb-wc-booking-form select {
|
||||
width: 100%;
|
||||
padding: 10px;
|
||||
border: 1px solid #c3c4c7;
|
||||
border-radius: 4px;
|
||||
font-size: 1em;
|
||||
}
|
||||
|
||||
.bnb-wc-booking-form input[type="date"]:focus,
|
||||
.bnb-wc-booking-form input[type="number"]:focus,
|
||||
.bnb-wc-booking-form select:focus {
|
||||
border-color: #2271b1;
|
||||
outline: none;
|
||||
box-shadow: 0 0 0 1px #2271b1;
|
||||
}
|
||||
|
||||
.bnb-wc-booking-form .form-row-inline {
|
||||
display: flex;
|
||||
gap: 15px;
|
||||
}
|
||||
|
||||
.bnb-wc-booking-form .form-row-inline > div {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
/* Availability status */
|
||||
.bnb-availability-status {
|
||||
margin: 15px 0;
|
||||
padding: 10px 15px;
|
||||
border-radius: 4px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.bnb-availability-status.checking {
|
||||
background: #f6f7f7;
|
||||
color: #787c82;
|
||||
}
|
||||
|
||||
.bnb-availability-status.available {
|
||||
background: #e6f4ea;
|
||||
color: #0a6e31;
|
||||
}
|
||||
|
||||
.bnb-availability-status.unavailable {
|
||||
background: #ffeaea;
|
||||
color: #d63638;
|
||||
}
|
||||
|
||||
.bnb-availability-status .dashicons {
|
||||
font-size: 18px;
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
}
|
||||
|
||||
/* Price display */
|
||||
.bnb-wc-price-display {
|
||||
margin: 15px 0;
|
||||
padding: 15px;
|
||||
background: #f8f9fa;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.bnb-wc-price-display .price-row {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
padding: 5px 0;
|
||||
}
|
||||
|
||||
.bnb-wc-price-display .price-row.total {
|
||||
border-top: 1px solid #e1e4e8;
|
||||
margin-top: 10px;
|
||||
padding-top: 10px;
|
||||
font-weight: 700;
|
||||
font-size: 1.1em;
|
||||
}
|
||||
|
||||
/* Services selection */
|
||||
.bnb-wc-services {
|
||||
margin: 15px 0;
|
||||
}
|
||||
|
||||
.bnb-wc-services h4 {
|
||||
margin: 0 0 10px 0;
|
||||
font-size: 1em;
|
||||
}
|
||||
|
||||
.bnb-wc-service-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 10px;
|
||||
margin-bottom: 8px;
|
||||
background: #f9f9f9;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.bnb-wc-service-item:hover {
|
||||
background: #f0f0f0;
|
||||
}
|
||||
|
||||
.bnb-wc-service-item.selected {
|
||||
background: #e6f4ea;
|
||||
border: 1px solid #d1e7d7;
|
||||
}
|
||||
|
||||
.bnb-wc-service-item input[type="checkbox"] {
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.bnb-wc-service-item .service-name {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.bnb-wc-service-item .service-price {
|
||||
color: #2271b1;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.bnb-wc-service-item .service-qty {
|
||||
margin-left: 10px;
|
||||
width: 60px;
|
||||
}
|
||||
|
||||
/* Add to cart button */
|
||||
.bnb-wc-booking-form .add-to-cart-btn {
|
||||
width: 100%;
|
||||
padding: 12px 20px;
|
||||
background: #2271b1;
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
font-size: 1.1em;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.bnb-wc-booking-form .add-to-cart-btn:hover {
|
||||
background: #135e96;
|
||||
}
|
||||
|
||||
.bnb-wc-booking-form .add-to-cart-btn:disabled {
|
||||
background: #c3c4c7;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Admin Order - Booking Info
|
||||
========================================================================== */
|
||||
|
||||
.bnb-order-booking-info {
|
||||
margin-top: 20px;
|
||||
padding: 15px;
|
||||
background: #f6f7f7;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.bnb-order-booking-info h3 {
|
||||
margin: 0 0 12px 0;
|
||||
font-size: 13px;
|
||||
color: #1d2327;
|
||||
}
|
||||
|
||||
.bnb-order-booking-info p {
|
||||
margin: 0 0 8px 0;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.bnb-order-booking-info a {
|
||||
color: #2271b1;
|
||||
}
|
||||
|
||||
.bnb-order-booking-info a:hover {
|
||||
color: #135e96;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Admin List Table - Order Columns
|
||||
========================================================================== */
|
||||
|
||||
.column-wc_order {
|
||||
width: 10%;
|
||||
}
|
||||
|
||||
.column-bnb_booking {
|
||||
width: 15%;
|
||||
}
|
||||
|
||||
.column-bnb_booking small {
|
||||
display: block;
|
||||
color: #787c82;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
/* Order action buttons */
|
||||
.wc-action-button-view_booking::after {
|
||||
font-family: dashicons;
|
||||
content: "\f513";
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Responsive
|
||||
========================================================================== */
|
||||
|
||||
@media screen and (max-width: 768px) {
|
||||
.bnb-wc-booking-form .form-row-inline {
|
||||
flex-direction: column;
|
||||
gap: 0;
|
||||
}
|
||||
|
||||
.bnb-booking-details span {
|
||||
display: block;
|
||||
margin-right: 0;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.bnb-booking-details span::before {
|
||||
content: "";
|
||||
margin-right: 0;
|
||||
}
|
||||
}
|
||||
358
assets/js/wc-integration.js
Normal file
358
assets/js/wc-integration.js
Normal file
@@ -0,0 +1,358 @@
|
||||
/**
|
||||
* WooCommerce Integration Scripts
|
||||
*
|
||||
* Handles booking form interactions for WooCommerce integration.
|
||||
*
|
||||
* @package Magdev\WpBnb
|
||||
*/
|
||||
|
||||
(function ($) {
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* WP BnB WooCommerce Integration
|
||||
*/
|
||||
const WpBnbWC = {
|
||||
/**
|
||||
* Settings from localization
|
||||
*/
|
||||
settings: {},
|
||||
|
||||
/**
|
||||
* Initialize
|
||||
*/
|
||||
init: function () {
|
||||
this.settings = window.wpBnbWC || {};
|
||||
this.bindEvents();
|
||||
this.initBookingForms();
|
||||
},
|
||||
|
||||
/**
|
||||
* Bind global events
|
||||
*/
|
||||
bindEvents: function () {
|
||||
// Admin: Sync all rooms button
|
||||
$(document).on('click', '.bnb-sync-rooms-btn', this.handleSyncRooms.bind(this));
|
||||
|
||||
// Admin: Generate invoice button
|
||||
$(document).on('click', '.bnb-generate-invoice-btn', this.handleGenerateInvoice.bind(this));
|
||||
},
|
||||
|
||||
/**
|
||||
* Initialize booking forms
|
||||
*/
|
||||
initBookingForms: function () {
|
||||
$('.bnb-wc-booking-form').each(function () {
|
||||
new BookingForm($(this));
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Handle sync all rooms button
|
||||
*/
|
||||
handleSyncRooms: function (e) {
|
||||
e.preventDefault();
|
||||
const $btn = $(e.currentTarget);
|
||||
const $status = $btn.siblings('.sync-status');
|
||||
|
||||
$btn.prop('disabled', true).addClass('updating');
|
||||
$status.text(this.settings.i18n?.syncing || 'Syncing...');
|
||||
|
||||
$.ajax({
|
||||
url: this.settings.ajaxUrl,
|
||||
type: 'POST',
|
||||
data: {
|
||||
action: 'wp_bnb_sync_all_rooms',
|
||||
nonce: this.settings.nonce
|
||||
},
|
||||
success: function (response) {
|
||||
if (response.success) {
|
||||
$status.html('<span class="success">' + response.data.message + '</span>');
|
||||
} else {
|
||||
$status.html('<span class="error">' + (response.data?.message || 'Error') + '</span>');
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
$status.html('<span class="error">' + (WpBnbWC.settings.i18n?.error || 'Error occurred') + '</span>');
|
||||
},
|
||||
complete: function () {
|
||||
$btn.prop('disabled', false).removeClass('updating');
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Handle generate invoice button
|
||||
*/
|
||||
handleGenerateInvoice: function (e) {
|
||||
e.preventDefault();
|
||||
const $btn = $(e.currentTarget);
|
||||
const orderId = $btn.data('order-id');
|
||||
|
||||
$btn.prop('disabled', true).addClass('updating');
|
||||
|
||||
$.ajax({
|
||||
url: this.settings.ajaxUrl,
|
||||
type: 'POST',
|
||||
data: {
|
||||
action: 'wp_bnb_generate_invoice',
|
||||
nonce: this.settings.nonce,
|
||||
order_id: orderId
|
||||
},
|
||||
success: function (response) {
|
||||
if (response.success) {
|
||||
location.reload();
|
||||
} else {
|
||||
alert(response.data?.message || 'Error generating invoice');
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
alert(WpBnbWC.settings.i18n?.error || 'Error occurred');
|
||||
},
|
||||
complete: function () {
|
||||
$btn.prop('disabled', false).removeClass('updating');
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Booking Form Handler
|
||||
*/
|
||||
class BookingForm {
|
||||
constructor($form) {
|
||||
this.$form = $form;
|
||||
this.roomId = $form.data('room-id');
|
||||
this.productId = $form.data('product-id');
|
||||
this.checkAvailabilityTimeout = null;
|
||||
|
||||
this.bindEvents();
|
||||
}
|
||||
|
||||
bindEvents() {
|
||||
this.$form.on('change', '.bnb-date-input', this.onDateChange.bind(this));
|
||||
this.$form.on('change', '.bnb-guests-input', this.onGuestsChange.bind(this));
|
||||
this.$form.on('change', '.bnb-service-checkbox', this.onServiceChange.bind(this));
|
||||
this.$form.on('change', '.bnb-service-qty', this.onServiceQtyChange.bind(this));
|
||||
this.$form.on('submit', this.onSubmit.bind(this));
|
||||
}
|
||||
|
||||
onDateChange() {
|
||||
const checkIn = this.$form.find('[name="bnb_check_in"]').val();
|
||||
const checkOut = this.$form.find('[name="bnb_check_out"]').val();
|
||||
|
||||
// Validate dates
|
||||
if (!checkIn || !checkOut) {
|
||||
this.updateAvailabilityStatus('');
|
||||
return;
|
||||
}
|
||||
|
||||
const checkInDate = new Date(checkIn);
|
||||
const checkOutDate = new Date(checkOut);
|
||||
const today = new Date();
|
||||
today.setHours(0, 0, 0, 0);
|
||||
|
||||
if (checkInDate < today) {
|
||||
this.updateAvailabilityStatus('unavailable', WpBnbWC.settings.i18n?.pastDate || 'Check-in cannot be in the past');
|
||||
return;
|
||||
}
|
||||
|
||||
if (checkOutDate <= checkInDate) {
|
||||
this.updateAvailabilityStatus('unavailable', WpBnbWC.settings.i18n?.invalidDates || 'Check-out must be after check-in');
|
||||
return;
|
||||
}
|
||||
|
||||
// Check availability
|
||||
this.checkAvailability(checkIn, checkOut);
|
||||
}
|
||||
|
||||
onGuestsChange() {
|
||||
// Re-validate if needed
|
||||
const checkIn = this.$form.find('[name="bnb_check_in"]').val();
|
||||
const checkOut = this.$form.find('[name="bnb_check_out"]').val();
|
||||
|
||||
if (checkIn && checkOut) {
|
||||
this.checkAvailability(checkIn, checkOut);
|
||||
}
|
||||
}
|
||||
|
||||
onServiceChange(e) {
|
||||
const $checkbox = $(e.currentTarget);
|
||||
const $item = $checkbox.closest('.bnb-wc-service-item');
|
||||
const $qtyInput = $item.find('.bnb-service-qty');
|
||||
|
||||
if ($checkbox.is(':checked')) {
|
||||
$item.addClass('selected');
|
||||
$qtyInput.prop('disabled', false);
|
||||
} else {
|
||||
$item.removeClass('selected');
|
||||
$qtyInput.prop('disabled', true);
|
||||
}
|
||||
|
||||
this.updatePriceDisplay();
|
||||
}
|
||||
|
||||
onServiceQtyChange() {
|
||||
this.updatePriceDisplay();
|
||||
}
|
||||
|
||||
checkAvailability(checkIn, checkOut) {
|
||||
// Debounce
|
||||
clearTimeout(this.checkAvailabilityTimeout);
|
||||
|
||||
this.updateAvailabilityStatus('checking', WpBnbWC.settings.i18n?.checking || 'Checking availability...');
|
||||
|
||||
this.checkAvailabilityTimeout = setTimeout(() => {
|
||||
const guests = this.$form.find('[name="bnb_guests"]').val() || 1;
|
||||
|
||||
$.ajax({
|
||||
url: WpBnbWC.settings.ajaxUrl,
|
||||
type: 'POST',
|
||||
data: {
|
||||
action: 'wp_bnb_get_availability',
|
||||
nonce: WpBnbWC.settings.nonce,
|
||||
room_id: this.roomId,
|
||||
check_in: checkIn,
|
||||
check_out: checkOut,
|
||||
guests: guests
|
||||
},
|
||||
success: (response) => {
|
||||
if (response.success) {
|
||||
const data = response.data;
|
||||
|
||||
if (data.available) {
|
||||
this.updateAvailabilityStatus('available', WpBnbWC.settings.i18n?.available || 'Available');
|
||||
this.updatePriceDisplay(data.price, data.breakdown);
|
||||
this.enableSubmit();
|
||||
} else {
|
||||
this.updateAvailabilityStatus('unavailable', data.message || WpBnbWC.settings.i18n?.unavailable || 'Not available');
|
||||
this.disableSubmit();
|
||||
}
|
||||
} else {
|
||||
this.updateAvailabilityStatus('unavailable', response.data?.message || 'Error checking availability');
|
||||
this.disableSubmit();
|
||||
}
|
||||
},
|
||||
error: () => {
|
||||
this.updateAvailabilityStatus('unavailable', WpBnbWC.settings.i18n?.error || 'Error checking availability');
|
||||
this.disableSubmit();
|
||||
}
|
||||
});
|
||||
}, 500);
|
||||
}
|
||||
|
||||
updateAvailabilityStatus(status, message) {
|
||||
const $statusEl = this.$form.find('.bnb-availability-status');
|
||||
|
||||
if (!status) {
|
||||
$statusEl.hide();
|
||||
return;
|
||||
}
|
||||
|
||||
$statusEl.removeClass('checking available unavailable').addClass(status);
|
||||
|
||||
let icon = '';
|
||||
switch (status) {
|
||||
case 'checking':
|
||||
icon = '<span class="dashicons dashicons-update"></span>';
|
||||
break;
|
||||
case 'available':
|
||||
icon = '<span class="dashicons dashicons-yes-alt"></span>';
|
||||
break;
|
||||
case 'unavailable':
|
||||
icon = '<span class="dashicons dashicons-no-alt"></span>';
|
||||
break;
|
||||
}
|
||||
|
||||
$statusEl.html(icon + ' ' + message).show();
|
||||
}
|
||||
|
||||
updatePriceDisplay(roomPrice, breakdown) {
|
||||
const $priceDisplay = this.$form.find('.bnb-wc-price-display');
|
||||
|
||||
if (!roomPrice) {
|
||||
$priceDisplay.hide();
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate services total
|
||||
let servicesTotal = 0;
|
||||
const nights = breakdown?.nights || 1;
|
||||
|
||||
this.$form.find('.bnb-wc-service-item').each(function () {
|
||||
const $item = $(this);
|
||||
const $checkbox = $item.find('.bnb-service-checkbox');
|
||||
|
||||
if ($checkbox.is(':checked')) {
|
||||
const price = parseFloat($item.data('price')) || 0;
|
||||
const pricingType = $item.data('pricing-type');
|
||||
const qty = parseInt($item.find('.bnb-service-qty').val()) || 1;
|
||||
|
||||
if (pricingType === 'per_night') {
|
||||
servicesTotal += price * qty * nights;
|
||||
} else if (pricingType === 'per_booking') {
|
||||
servicesTotal += price * qty;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const grandTotal = roomPrice + servicesTotal;
|
||||
|
||||
// Update display
|
||||
let html = '<div class="price-row"><span>' + (WpBnbWC.settings.i18n?.roomTotal || 'Room') + '</span><span>' + WpBnbWC.formatPrice(roomPrice) + '</span></div>';
|
||||
|
||||
if (servicesTotal > 0) {
|
||||
html += '<div class="price-row"><span>' + (WpBnbWC.settings.i18n?.services || 'Services') + '</span><span>' + WpBnbWC.formatPrice(servicesTotal) + '</span></div>';
|
||||
}
|
||||
|
||||
html += '<div class="price-row total"><span>' + (WpBnbWC.settings.i18n?.total || 'Total') + '</span><span>' + WpBnbWC.formatPrice(grandTotal) + '</span></div>';
|
||||
|
||||
$priceDisplay.html(html).show();
|
||||
}
|
||||
|
||||
enableSubmit() {
|
||||
this.$form.find('.add-to-cart-btn').prop('disabled', false);
|
||||
}
|
||||
|
||||
disableSubmit() {
|
||||
this.$form.find('.add-to-cart-btn').prop('disabled', true);
|
||||
}
|
||||
|
||||
onSubmit(e) {
|
||||
// Form will submit normally - WooCommerce handles the add to cart
|
||||
// Just validate one more time
|
||||
const checkIn = this.$form.find('[name="bnb_check_in"]').val();
|
||||
const checkOut = this.$form.find('[name="bnb_check_out"]').val();
|
||||
|
||||
if (!checkIn || !checkOut) {
|
||||
e.preventDefault();
|
||||
alert(WpBnbWC.settings.i18n?.selectDates || 'Please select check-in and check-out dates');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Format price
|
||||
*/
|
||||
WpBnbWC.formatPrice = function (price) {
|
||||
const currency = this.settings.currency || 'CHF';
|
||||
const symbol = this.settings.currencySymbol || currency;
|
||||
const decimals = this.settings.priceDecimals || 2;
|
||||
const decimalSep = this.settings.decimalSeparator || '.';
|
||||
const thousandSep = this.settings.thousandSeparator || "'";
|
||||
|
||||
const formatted = price.toFixed(decimals).replace(/\B(?=(\d{3})+(?!\d))/g, thousandSep).replace('.', decimalSep);
|
||||
|
||||
return symbol + ' ' + formatted;
|
||||
};
|
||||
|
||||
// Initialize on DOM ready
|
||||
$(document).ready(function () {
|
||||
WpBnbWC.init();
|
||||
});
|
||||
|
||||
// Export for external use
|
||||
window.WpBnbWC = WpBnbWC;
|
||||
|
||||
})(jQuery);
|
||||
Reference in New Issue
Block a user