Code Flash Sale Countdown và Sản phẩm đã bán vào child theme đơn giản
<?php
function isures_flashsale_countdown_and_stock_prod()
{
$thepostid = get_the_ID();
$sale_price_dates_to = ($date_to = get_post_meta($thepostid, ‘_sale_price_dates_to’, true)) ? date_i18n(‘Y-m-d’, $date_to) : ”;
$sale_from = ($date_from = get_post_meta($thepostid, ‘_sale_price_dates_from’, true)) ? date_i18n(‘Y-m-d’, $date_from) : ”;
$strtotime_date_from = strtotime($sale_from);
$strtotime_date_to = strtotime($sale_price_dates_to);
if (!empty($sale_price_dates_to) && $strtotime_date_to > $strtotime_date_from && $strtotime_date_to >= strtotime(date(“Y/m/d”))) {
?>
<div class=”crazy-deal-details pc”>
<div class=”crazy-deal-details-right”>
<time class=”crazy-deal-details-countdown”>Kết thúc sau <span data-count=”<?php echo $sale_price_dates_to; ?>” id=”isures-sale-fl”></span></time>
<?php
global $product;
$stock = $product->get_stock_quantity();
$buy = get_post_meta($product->get_id(), ‘total_sales’, true);
if (!empty($stock) && $buy > 0) {
$round = round(($buy * 100 / ($stock + $buy)), 0);
?>
<div class=”crazy-deal-details-process”>
<div class=”crazy-deal-details-procressbar”>
<div class=”crazy-deal-details-procressbar-inner” style=”width:<?php echo $round . ‘%’; ?>”></div>
</div>
<span class=”crazy-deal-details-soldtext”><?php echo sprintf(__(‘Đã bán %s sp’, ‘woocommerce’), $buy); ?></span>
</div>
<?php
}
?>
</div>
</div>
<script>
jQuery(function($) {
function updateTimer() {
var isures_count = $(“#isures-sale-fl”).attr(‘data-count’);
future = Date.parse(isures_count);
now = new Date();
diff = future – now;
days = Math.floor( diff / (1000*60*60*24) );
hours = Math.floor( diff / (1000*60*60) );
mins = Math.floor( diff / (1000*60) );
secs = Math.floor( diff / 1000 );
d = days;
h = hours – days * 24;
m = mins – hours * 60;
s = secs – mins * 60;
$(‘#isures-sale-fl’).html(d + ‘:’ + h + ‘:’ + m + ‘:’ + s);
}
setInterval(function() {
updateTimer();
}, 1000);
});
</script>
<style>
.crazy-deal-details.pc {
margin: 25px auto;
height: 29px;
overflow: hidden;
background-image: url(https://huanvm.com/wp-content/uploads/2022/12/huanvmdotcom-flashsale-pc.png);
background-position: 0 0;
background-repeat: no-repeat;
background-size: 100% 100%;
}
.crazy-deal-details-right {
position: relative;
margin-left: 140px;
height: 100%;
display: flex;
align-items: center;
flex-direction: row;
justify-content: space-between;
}
time.crazy-deal-details-countdown {
text-align: left;
font-size: 14px;
}
span#isures-sale-fl {
color: #ff3d00;
font-size: 14px;
}
span.crazy-deal-details-soldtext {
color: #999;
font-size: 14px;
font-weight: bold;
margin-right: 10px;
}
.crazy-deal-details.pc .crazy-deal-details-process {
display: flex;
flex-direction: row-reverse;
align-items: center;
color: #999;
font-weight: 700;
}
.crazy-deal-details.pc .crazy-deal-details-procressbar {
width: 90px;
height: 8px;
background: #ffd1c2;
border-radius: 4px;
display: inline-block;
margin-right: 6px;
margin-left: 6px;
}
.crazy-deal-details.pc .crazy-deal-details-procressbar-inner {
background: linear-gradient(90deg, #ff8763, #ff330c);
border-radius: 4px;
width: 0;
height: 100%;
}
@media screen and (max-width: 1024px) {
.crazy-deal-details.pc {
height: 50px;
background-image: url(https://huanvm.com/wp-content/uploads/2022/12/huanvmdotcom-flashsale-mobile.png);
}
.crazy-deal-details-right {
position: relative;
display: flex;
color: #fff;
margin-right: 10px;
height: 100%;
flex-direction: column;
justify-content: center;
align-items: flex-end;
}
.crazy-deal-details-procressbar {
background: hsla(0, 0%, 100%, .5);
}
.crazy-deal-details.pc .crazy-deal-details-procressbar-inner {
background: #fff;
}
.isures-sale-fl,
span.crazy-deal-details-soldtext {
color: #fff !important
}
}
</style>
<?php
}
}
add_action(‘woocommerce_before_add_to_cart_button’, ‘isures_flashsale_countdown_and_stock_prod’);