我目前正在为出租产品确定日期。为此,我使用了Jquery UI Datepicker。
这使我可以选择两个日期并计算这些日期范围内的天数。
这是我的代码:
add_action('wp_enqueue_scripts', 'enabling_date_picker');
function enabling_date_picker() {
// Only on front-end and product page
if (is_product() && !is_wc_endpoint_url()):
// Load the Datepicker jQuery-ui plugin script
wp_enqueue_style('jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css');
wp_enqueue_script('jquery-ui-datepicker');
endif;
}
// The jQuery script
add_action('wp_footer', 'rental_date_jquery_script');
function rental_date_jquery_script() {
// Only on front-end and product page
if (is_product() && !is_wc_endpoint_url()):
?>
<script>
jQuery(function($) {
var from = new Date();
var to = new Date();
var dayDiff = 1;
var dates = $("#from, #to").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
dateFormat: "dd.mm.yy",
minDate: 0,
maxDate: 14,
onSelect: function(selectedDate) {
var option = this.id == "from" ? "minDate" : "maxDate",
instance = $(this).data("datepicker"),
date = $.datepicker.parseDate(
instance.settings.dateFormat ||
$.datepicker._defaults.dateFormat,
selectedDate, instance.settings);
dates.not(this).datepicker("option", option, date);
if (this.id == "from") {
from = $(this).datepicker('getDate');
if (!(to == "")) {
update_days()
}
}
if (this.id == "to") {
to = $(this).datepicker('getDate');
update_days()
}
}
});
function update_days() {
dayDiff = Math.ceil((to - from) / (1000 * 60 * 60 * 24));
$("#days").empty()
$("#days").append(dayDiff)
}
});
</script>
<?php
endif;
}
// Add a custom field before single add to cart
add_action('woocommerce_before_variations_form', 'display_rental_date_custom_fields', 5);
function display_rental_date_custom_fields() {
echo '<div>
<h3>From:</h3>
<input id="from" type="text" name="from" readonly />
</div>
<div>
<h3>To:</h3>
<input id="to" type="text" name="to" readonly />
</div>
<div>
<span>You have chosen: </span>
<span id="days">< /span> days.
</div>';
}
更新:
我想进行动态折扣计算。我会解释我的意思:
例如,如果客户在5天的时间内每天租借价值100美元的产品,则会获得以下信息:
Day 1 - $100 (100%)
Day 2 - $50 (50%)
Day 3 - $50 (50%)
Day 4 - $50 (50%)
Day 5 - $50 (50%)
最后,它应该可以工作-5天$ 300。
同时,有必要计算总租金价格并在单个产品的页面上显示新价格。所有产品都是可变的。
如何实现这种功能?需要您的帮助!
我希望将来很多开发人员都需要此功能。
答案 0 :(得分:2)
在display_rental_date_custom_fields()
代码中:
我更改了字段name
和id
,使它们以rental_period_
开头,因为to
和from
太通用了。 p>
我添加了一个名为input
的隐藏is_rental
字段,该字段指示正在购买的产品可以租用。
我在日期字段中添加了value
属性。
function display_rental_date_custom_fields() {
?>
<div>
<h3>From:</h3>
<input id="rental_period_from" type="text" name="rental_period_from" readonly value="<?php echo esc_attr( filter_input( INPUT_POST, 'rental_period_from' ) ); ?>" />
</div>
<div>
<h3>To:</h3>
<input id="rental_period_to" type="text" name="rental_period_to" readonly value="<?php echo esc_attr( filter_input( INPUT_POST, 'rental_period_to' ) ); ?>" />
</div>
<div>
<span>You have chosen: </span>
<span id="days">0</span> days.
</div>
<input type="hidden" name="is_rental" value="1">
<?php
}
您应该使用修订后的日期选择器脚本here-只需访问页面即可查看更改。
(将下面的代码段复制并粘贴到display_rental_date_custom_fields
代码下面。)
SNIPPET 1:用于进行计算的PHP函数:
在下面的SNIPPET#3中查看。
SNIPPET 2:处理提交的租赁期(即开始和结束日期):
add_filter( 'woocommerce_add_cart_item_data', 'my_add_rental_period_data' );
function my_add_rental_period_data( $cart_item_data ) {
if ( ! empty( $_POST['is_rental'] ) ) {
// Throwing an Exception will prevent the product from being added to the cart.
// Validate POSTed values.
if ( empty( $_POST['rental_period_from'] ) ||
empty( $_POST['rental_period_to'] ) ) {
throw new Exception( 'Rental start and end dates must both be specified.' );
}
$now = date_create( 'now' );
$from = date_create( $_POST['rental_period_from'] );
$to = date_create( $_POST['rental_period_to'] );
// Validate rental dates.
if ( ! $from || ! $to || $to < $from ) {
throw new Exception( 'Invalid rental dates.' );
}
$format = 'd.m.Y'; // dd.mm.yyyy
$rental_days = date_diff( $from, $to )->days;
$cart_item_data['rental_period'] = [
'from' => $from->format( $format ),
'to' => $to->format( $format ),
'days' => $rental_days,
];
}
return $cart_item_data;
}
SNIPPET 3:将折扣应用于购物车中的租赁产品:
function my_set_rental_product_price( array $cart_item ) {
if ( ! empty( $cart_item['rental_period'] ) ) {
$rental_days = $cart_item['rental_period']['days'];
if ( $rental_days > 1 ) {
$regular_price = $cart_item['data']->get_regular_price();
$price = $regular_price / 2 * ( $rental_days - 1 ) + $regular_price;
$cart_item['data']->set_price( $price );
}
// else, no discount given.
return $cart_item['data'];
}
}
add_action( 'woocommerce_before_calculate_totals', 'my_apply_discount_to_rental_products' );
function my_apply_discount_to_rental_products() {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
my_set_rental_product_price( $cart_item );
}
}
add_filter( 'woocommerce_cart_item_product', 'my_apply_discount_to_rental_product', 10, 2 );
function my_apply_discount_to_rental_product( $product, $cart_item ) {
if ( $rental_product = my_set_rental_product_price( $cart_item ) ) {
return $rental_product;
}
return $product;
}
SNIPPET 4:在购物车页面的主表中显示租赁期:
add_filter( 'woocommerce_get_item_data', 'my_add_rental_period_meta', 10, 2 );
function my_add_rental_period_meta( $item_data, $cart_item ) {
if ( ! empty( $cart_item['rental_period'] ) ) {
$period =& $cart_item['rental_period'];
$days = $period['days'] . ' ' . _n( 'day', 'days', $period['days'] );
$range = ( $period['from'] === $period['to'] ) ? $period['from'] . ' (today)' :
$days . ' (' . $period['from'] . ' - ' . $period['to'] . ')';
$item_data[] = [
'key' => 'Rental Period',
'value' => $range,
];
}
return $item_data;
}
SNIPPET 5:按以下顺序将租借期添加为产品的元数据:(这对于后端/管理员或以后的访问特别需要)
add_action( 'woocommerce_checkout_create_order_line_item', 'my_add_rental_period_meta2', 10, 3 );
function my_add_rental_period_meta2( $item, $cart_item_key, $cart_item ) {
if ( ! empty( $cart_item['rental_period'] ) ) {
$item->add_meta_data( '_rental_period', $cart_item['rental_period'] );
}
return $item;
}
SNIPPET 6:例如,可以在“订单详细信息”表和“新订单”管理电子邮件中显示上面保存的元数据
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'my_add_rental_period_meta3', 10, 2 );
function my_add_rental_period_meta3( $formatted_meta, $item ) {
if ( $period = $item->get_meta( '_rental_period', true ) ) {
$days = $period['days'] . ' ' . _n( 'day', 'days', $period['days'] );
$range = ( $period['from'] === $period['to'] ) ? $period['from'] . ' (today)' :
$days . ' (' . $period['from'] . ' - ' . $period['to'] . ')';
$formatted_meta[] = (object) [
'key' => 'rental_period',
'value' => $period,
'display_key' => 'Rental Period',
'display_value' => $range,
];
}
return $formatted_meta;
}