Forked from imran-khan1/flatsome-product-on-sale-end-counter.php
Created
March 17, 2020 13:01
-
-
Save KoolPal/ad80a4e812fba69fdbc34cad87979506 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Flatsome Theme Product On-Sale End Counter | |
*/ | |
function display_onsale_product_counter() { | |
global $product; | |
if ( $product->is_on_sale() ) | |
{ | |
//if starts | |
if ( $product->is_type( 'variable' ) ) { | |
global $post; | |
$variations = $product->get_available_variations(); | |
$i=0; | |
foreach ( $variations as $variation ) { | |
// get variation ID | |
$variation_id = $variation['variation_id']; | |
$sales_price_to = get_post_meta($variation_id, '_sale_price_dates_to', true); | |
if($sales_price_to !="") | |
{ | |
$end_date = date('Y-m-d', $sales_price_to); | |
$today_date = date('Y-m-d'); | |
if($end_date >= $today_date) | |
{ | |
if($i >= 1){$counter = 'none';} | |
else{$counter = 'block';} | |
display_counter_sale_end_date($end_date, $variation_id, $display=$counter ); | |
$i++; | |
} | |
} | |
} | |
} | |
else{ | |
$get_sale_end = $product->get_date_on_sale_to(); | |
if($get_sale_end !="") | |
{ | |
$get_end_date = explode("T", $get_sale_end); | |
$end_date = $get_end_date[0]; | |
$today_date = date('Y-m-d'); | |
if($end_date >= $today_date) | |
{ | |
display_counter_sale_end_date($end_date); | |
} | |
} | |
} //else end | |
?> | |
<script> | |
jQuery(document).ready(function($) { | |
$('input.variation_id').change( function(){ | |
if( '' != $('input.variation_id').val() ) { | |
var var_id = $('input.variation_id').val(); | |
if ( $('#counter_'+var_id).length ) { | |
$('.all_date_counters').css("display","none"); | |
$('#counter_'+var_id).css("display", "block"); | |
} | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
} | |
add_action('woocommerce_single_product_summary', 'display_onsale_product_counter', 45); | |
//display_counter_sale_end_date | |
function display_counter_sale_end_date($end_date, $var_id='', $display=''){ | |
$break_end_date = explode("-", $end_date); | |
$end_date_year = $break_end_date[0]; | |
$end_date_month = $break_end_date[1]; | |
$end_date_day = $break_end_date[2]; | |
echo "<div id='counter_".$var_id."' class='all_date_counters' style='display:".$display."'> | |
<div class='sale-end-in'>Sale Ends In:</div>". do_shortcode('[ux_countdown year="'.$end_date_year.'" month="'.$end_date_month.'" day="'.$end_date_day.'"]')."</div>"; | |
} | |
function counter_css() | |
{ | |
?> | |
<style> | |
.sale-end-in { | |
color: red; | |
font-size: 24px; | |
font-weight: bold; | |
text-align: center; | |
} | |
</style> | |
<?php | |
} | |
add_action('wp_head', 'counter_css'); | |
/* | |
Product onSale Counter Ends | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment