سلام
کد زیر برای نمایش 1 1 1 هست برای محصولات تخفیف دار تایمر نمایش میده ولی برای همه محصولات تخفیف تایمر یکجور نمایش میده (درصورتی که زمان پایان تخفیف محصولات با هم فرق دارند) باید چیکار کنم؟
/*تایمر تخفیف محصول*/
function show_sale_timer_shortcode() {
global $product;
if ( is_a($product, 'WC_Product') && $product->is_on_sale() ) {
$sale_start_date = get_post_meta( get_the_ID(), '_sale_price_dates_from', true );
$sale_end_date = get_post_meta( get_the_ID(), '_sale_price_dates_to', true );
if ( $sale_start_date && $sale_end_date ) {
$current_time = current_time( 'timestamp', true );
if ( $current_time >= $sale_start_date && $current_time <= $sale_end_date ) {
$time_remaining = $sale_end_date - $current_time;
// افزودن تایمر و اسکریپت جاوااسکریپت برای بهروزرسانی تایمر بدون رفرش صفحه
$output = '<p id="sale-timer">تخفیف تا: <span class="timer">' . gmdate("H:i:s", $time_remaining) . '</span></p>';
$output .= '
<script>
jQuery(function($) {
function updateTimer() {
$.ajax({
url: "'.admin_url('admin-ajax.php').'",
type: "POST",
data: {
"action": "update_sale_timer",
"product_id": "'.get_the_ID().'"
},
success: function(response) {
$(".timer").html(response);
}
});
}
setInterval(updateTimer, 1000);
});
</script>';
return $output;
}
}
}
}
add_shortcode('show_sale_timer', 'show_sale_timer_shortcode');
// تابع برای بهروزرسانی زمان تایمر با استفاده از AJAX
add_action('wp_ajax_update_sale_timer', 'update_sale_timer_callback');
add_action('wp_ajax_nopriv_update_sale_timer', 'update_sale_timer_callback');
function update_sale_timer_callback() {
if ( isset($_POST['product_id']) ) {
$product_id = $_POST['product_id'];
// بازگردانی محصول با استفاده از ID محصول
$product = wc_get_product($product_id);
$sale_start_date = get_post_meta( $product_id, '_sale_price_dates_from', true );
$sale_end_date = get_post_meta( $product_id, '_sale_price_dates_to', true );
if ( $sale_start_date && $sale_end_date ) {
$current_time = current_time( 'timestamp', true );
if ( $current_time >= $sale_start_date && $current_time <= $sale_end_date ) {
$time_remaining = $sale_end_date - $current_time;
// کم کردن مقدار ثابت از زمان باقیمانده
$time_remaining -= 60; // مثال: از هر بار بهروزرسانی، یک دقیقه (60 ثانیه) کم میکنیم
$formatted_time = gmdate("H:i:s", $time_remaining);
// ارسال زمان به فرانتاند برای نمایش به کاربر
echo $formatted_time;
wp_die();
}
}
}
echo 'متاسفانه خطا رخ داده است.';
wp_die();
}
با [show_sale_timer] نمایش داده میشود
1 نوشته - 1 participants
برچسب:
نویسنده: استخدام کار