1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
<?php
if (!defined('ABSPATH')) {
exit;
}
class WC_ER_Cart {
public function __construct() {
add_filter('woocommerce_add_cart_item_data', array($this, 'add_rental_data_to_cart'), 10, 3);
add_filter('woocommerce_get_item_data', array($this, 'display_rental_data_in_cart'), 10, 2);
add_action('woocommerce_before_calculate_totals', array($this, 'calculate_rental_price'));
add_filter('woocommerce_add_to_cart_validation', array($this, 'validate_rental_data'), 10, 3);
}
public function add_rental_data_to_cart($cart_item_data, $product_id, $variation_id) {
if (get_post_meta($product_id, '_rental_enabled', true) !== 'yes') {
return $cart_item_data;
}
if (!isset($_POST['rental_start_date']) || !isset($_POST['rental_start_time']) || !isset($_POST['rental_duration'])) {
return $cart_item_data;
}
$start_date = sanitize_text_field($_POST['rental_start_date']);
$start_time = sanitize_text_field($_POST['rental_start_time']);
$duration = intval($_POST['rental_duration']);
$duration_unit = get_post_meta($product_id, '_rental_duration_unit', true);
// Combine date and time
$start_datetime = date('Y-m-d H:i:s', strtotime("$start_date $start_time"));
// Calculate end datetime
$booking = new WC_ER_Booking();
$end_datetime = $booking->calculate_end_date($start_datetime, $duration, $duration_unit);
// Get the base price (regular price)
$product = wc_get_product($product_id);
$base_price = floatval($product->get_regular_price());
// Calculate deposit
$deposit_type = get_post_meta($product_id, '_rental_deposit_type', true);
$deposit_amount = floatval(get_post_meta($product_id, '_rental_deposit', true));
if ($deposit_type === 'percentage') {
$deposit = $base_price * ($deposit_amount / 100);
} else {
$deposit = $deposit_amount;
}
$cart_item_data['rental_data'] = array(
'start_datetime' => $start_datetime,
'end_datetime' => $end_datetime,
'duration' => $duration,
'duration_unit' => $duration_unit,
'base_price' => $base_price,
'deposit_amount' => $deposit
);
return $cart_item_data;
}
public function display_rental_data_in_cart($item_data, $cart_item) {
if (isset($cart_item['rental_data'])) {
$rental_data = $cart_item['rental_data'];
$item_data[] = array(
'key' => __('Base Price per Day', 'wc-equipment-rental'),
'value' => wc_price($rental_data['base_price'])
);
$item_data[] = array(
'key' => __('Duration', 'wc-equipment-rental'),
'value' => sprintf(
'%d %s',
$rental_data['duration'],
$rental_data['duration_unit']
)
);
$item_data[] = array(
'key' => __('Total', 'wc-equipment-rental'),
'value' => wc_price($rental_data['base_price'] * $rental_data['duration'])
);
$item_data[] = array(
'key' => __('Rental Start', 'wc-equipment-rental'),
'value' => date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($rental_data['start_datetime']))
);
$item_data[] = array(
'key' => __('Rental End', 'wc-equipment-rental'),
'value' => date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($rental_data['end_datetime']))
);
$item_data[] = array(
'key' => __('Deposit Required', 'wc-equipment-rental'),
'value' => wc_price($rental_data['deposit_amount'])
);
}
return $item_data;
}
public function calculate_rental_price($cart) {
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
if (did_action('woocommerce_before_calculate_totals') >= 2) {
return;
}
foreach ($cart->get_cart() as $cart_item) {
if (isset($cart_item['rental_data'])) {
$rental_data = $cart_item['rental_data'];
$total_price = $rental_data['base_price'] * $rental_data['duration'];
$cart_item['data']->set_price($total_price);
}
}
}
public function validate_rental_data($passed, $product_id, $quantity) {
if (get_post_meta($product_id, '_rental_enabled', true) !== 'yes') {
return $passed;
}
if (!isset($_POST['rental_start_date']) || !isset($_POST['rental_start_time']) || !isset($_POST['rental_duration'])) {
wc_add_notice(__('Please select rental dates and duration.', 'wc-equipment-rental'), 'error');
return false;
}
$start_date = sanitize_text_field($_POST['rental_start_date']);
$start_time = sanitize_text_field($_POST['rental_start_time']);
$duration = intval($_POST['rental_duration']);
// Validate minimum and maximum duration
$min_duration = get_post_meta($product_id, '_rental_min_duration', true);
$max_duration = get_post_meta($product_id, '_rental_max_duration', true);
if ($duration < $min_duration || $duration > $max_duration) {
wc_add_notice(
sprintf(
__('Rental duration must be between %d and %d.', 'wc-equipment-rental'),
$min_duration,
$max_duration
),
'error'
);
return false;
}
// Check availability
$start_datetime = date('Y-m-d H:i:s', strtotime("$start_date $start_time"));
$booking = new WC_ER_Booking();
$end_datetime = $booking->calculate_end_date(
$start_datetime,
$duration,
get_post_meta($product_id, '_rental_duration_unit', true)
);
if (!$booking->check_availability($product_id, $start_datetime, $end_datetime)) {
wc_add_notice(__('The selected dates are not available for rental.', 'wc-equipment-rental'), 'error');
return false;
}
return $passed;
}
} |
Partager