Reputation: 1
Party Rental Equipment website. Using the JQuery datepicker in 3 woocommerce_form_field, one for Event Date, One for Delivery Date, and one for Pickup Date. I am trying to charge a $440 extra fee if the user selects a Sunday for Delivery Date or Pickup Date. In this datepicker Sunday is the first day of the week, Day 0.
The fee appears after the customer checkouts and receives the order meta or order email, but I can't get it to show on the checkout page on ajax update, which is important because i want them to know how much it costs before they checkout. I am hoping someone out there would be generous enough to help me with this, :)
Here's the code i am using - as mentioned the fees are visible when the checkout is processed but not on the actual checkout page, even after update_cart. this is in my child theme functions.php
//DATEPICKER STUFF
//
//
//
// enqueue jquery script
function enqueue_datepicker_scripts() {
// Enqueue jQuery UI
wp_enqueue_script('jquery-ui-datepicker');
}
add_action('wp_enqueue_scripts', 'enqueue_datepicker_scripts');
// Enqueue jquery options
function enqueue_datepicker_script() {
?>
<script>
jQuery(function ($) {
// Datepicker options
var datepickerOptions = {
dateFormat: 'mm/dd/yy',
minDate: '+3d', // Minimum date is today + 3 days
firstDay: 0, // Start week with Sunday
onSelect: function (dateText, inst) {
setTimeout(function () {
$('body').trigger('update_checkout');
}, 1);
}
};
// Event Date
$('#event_date').datepicker($.extend({}, datepickerOptions));
// Delivery Date
$('#delivery_date').datepicker($.extend({}, datepickerOptions));
// Pickup Date
$('#pickup_date').datepicker($.extend({}, datepickerOptions));
});
</script>
<?php
}
add_action('wp_footer', 'enqueue_datepicker_script');
// Add datepicker to checkout fields
function custom_add_date_picker_fields($checkout) {
echo '<div id="custom_date_picker"><h2>' . __('Delivery and Pickup Details') . '</h2>';
// Event Date
woocommerce_form_field('event_date', array(
'type' => 'text',
'class' => array('form-row-wide datepicker'),
'label' => __('Event Date'),
'required' => true,
'default' => date('m/d/Y', strtotime('+3 days')), // Default is 3 days from today
'custom_attributes' => array(
'min' => date('m/d/Y', strtotime('+3 days')), // Minimum date is 3 days from today
'max' => date('m/d/Y', strtotime('+2 years')), // Maximum date is 2 years from today
),
), $checkout->get_value('event_date'));
// Delivery Date
woocommerce_form_field('delivery_date', array(
'type' => 'text',
'class' => array('form-row-wide datepicker'),
'label' => __('Delivery Date'),
'required' => true,
'default' => date('m/d/Y', strtotime('+2 days')), // Default is 1 day before the event date
), $checkout->get_value('delivery_date'));
// Pickup Date
woocommerce_form_field('pickup_date', array(
'type' => 'text',
'class' => array('form-row-wide datepicker'),
'label' => __('Pickup Date'),
'required' => true,
'default' => date('m/d/Y', strtotime('+4 days')), // Default is 1 day after the event date
), $checkout->get_value('pickup_date'));
echo '</div>';
}
add_action('woocommerce_before_order_notes', 'custom_add_date_picker_fields');
function custom_process_date_picker_fields() {
}
add_action('woocommerce_checkout_process', 'custom_process_date_picker_fields');
// save to order meta
function custom_update_date_picker_fields($order_id) {
// Save Event Date
if (!empty($_POST['event_date'])) {
update_post_meta($order_id, 'Event Date', sanitize_text_field($_POST['event_date']));
}
// Save Delivery Date
if (!empty($_POST['delivery_date'])) {
update_post_meta($order_id, 'Delivery Date', sanitize_text_field($_POST['delivery_date']));
}
// Save Pickup Date
if (!empty($_POST['pickup_date'])) {
update_post_meta($order_id, 'Pickup Date', sanitize_text_field($_POST['pickup_date']));
}
}
add_action('woocommerce_checkout_update_order_meta', 'custom_update_date_picker_fields');
// Add fee with standard tax
function custom_add_sunday_delivery_fee() {
// Check if delivery date is set
if (isset($_POST['delivery_date'])) {
$delivery_date = sanitize_text_field($_POST['delivery_date']);
$delivery_day = date('w', strtotime($delivery_date));
// Check if delivery date is Sunday
if ($delivery_day == 0) { // Sunday
WC()->cart->add_fee(__('Sunday Delivery', 'woocommerce'), 440, true, 'standard');
}
}
}
add_action('woocommerce_cart_calculate_fees', 'custom_add_sunday_delivery_fee', 10);
// pickup fee
function custom_add_sunday_pickup_fee() {
// Check if pickup date is set
if (isset($_POST['pickup_date'])) {
$pickup_date = sanitize_text_field($_POST['pickup_date']);
$pickup_day = date('w', strtotime($pickup_date));
// Check if pickup date is Sunday
if ($pickup_day == 0) { // Sunday
WC()->cart->add_fee(__('Sunday Pickup', 'woocommerce'), 440, true, 'standard');
}
}
}
add_action('woocommerce_cart_calculate_fees', 'custom_add_sunday_pickup_fee', 10);
Upvotes: 0
Views: 53
Reputation: 1
like this mybe?
function enqueue_datepicker_scripts() {
wp_enqueue_script('jquery-ui-datepicker');
}
add_action('wp_enqueue_scripts', 'enqueue_datepicker_scripts');
function enqueue_datepicker_script() {
?>
<script>
jQuery(function ($) {
var datepickerOptions = {
dateFormat: 'mm/dd/yy',
minDate: '+3d',
firstDay: 0,
onSelect: function (dateText, inst) {
updateCheckoutWithFee();
}
};
$('#event_date').datepicker($.extend({}, datepickerOptions));
$('#delivery_date').datepicker($.extend({}, datepickerOptions));
$('#pickup_date').datepicker($.extend({}, datepickerOptions));
function updateCheckoutWithFee() {
var deliveryDate = $('#delivery_date').val();
var pickupDate = $('#pickup_date').val();
var deliveryDay = new Date(deliveryDate).getDay();
var pickupDay = new Date(pickupDate).getDay();
var extraFee = 440;
if (deliveryDay === 0 || pickupDay === 0) {
$('body').trigger('update_checkout');
alert('Extra fee of $' + extraFee + ' added for Sunday delivery or pickup.');
}
}
});
</script>
<?php
}
add_action('wp_footer', 'enqueue_datepicker_script');
Upvotes: 0