Skip to content

Instantly share code, notes, and snippets.

@sholance
Created July 9, 2022 08:27
Show Gist options
  • Save sholance/834e4f0e420e74d60570a4158ccdc084 to your computer and use it in GitHub Desktop.
Save sholance/834e4f0e420e74d60570a4158ccdc084 to your computer and use it in GitHub Desktop.
wc product timer
<?php
/**
* Theme functions and definitions
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*/
/* Display in vendor dashboard */
function add_custom_product_data_content_pob( $id, $product_obj, $post ) {
$hh6 = get_post_meta( $id , '_custom_product_date', true );
$hhd = get_post_meta($id , '_my_date_field', true );
$hht = get_post_meta($id , '_my_time_field', true );
$hht2 = get_post_meta($id , '_my_time_field2', true );
?>
<div class="form-group">
<label class="control-label col-sm-3 col-md-3" for="product_condition">Set Product availability time</label>
<div class="col-md-6 col-sm-9">
<select name="product_condition_value" id="product_condition_value" class="form-control regular-select">
<option value="Everyday"
data-show="none" <?php echo( $hh6 == 'Everyday' )? 'selected' : '' ; ?>>Everyday</option>
<option value="Weekdays"
data-show="date" <?php echo( $hh6 == 'On')? 'selected' : '' ; ?>>Weekdays</option>
<option value="Weekends"
data-show="date" <?php echo( $hh6 == 'Weekends')? 'selected' : '' ; ?>>Weekends</option>
<option value=""
data-show="date" <?php echo( $hh6 == '' )? 'selected' : '' ; ?>>Before date</option>
<option value=""
data-show="date" <?php echo( $hh6 == '' )? 'selected' : '' ; ?>>After date</option>
</select>
<div>
<div>
<label class="control-label col-sm-3 col-md-3" for="product_condition">Till</label>
<input type="date" name="my_date_field" class="form-control time-set" value="<?php echo $hhd;?>"/>
</div>
<label class="control-label col-sm-3 col-md-3" for="product_condition">From</label>
<input type="time" name="my_time_field" class="form-control time-set" value="<?php echo $hht;?>"/>
<label class="control-label col-sm-3 col-md-3" for="product_condition">To</label>
<input type="time" name="my_time_field2" class="form-control time-set" value="<?php echo $hht2;?>"/>
Current date--<code><?php echo current_time( 'm/d/Y' ); ?></code>--
</div>
</div>
</div>
<?php
}
add_action( 'wcmp_afm_after_general_product_data', 'add_custom_product_data_content_pob',10,3);
/*
Save Custom Tab content data.
*/
function save_custom_general_data( $product, $post_data ) {
if( isset($post_data['post_ID']) && isset($post_data['product_condition_value'])) {
update_post_meta( absint( $post_data['post_ID'] ), '_custom_product_date', $post_data['product_condition_value']);
}
if( isset($post_data['post_ID']) && isset($post_data['my_date_field'])){
update_post_meta( absint( $post_data['post_ID'] ), '_my_date_field', $post_data['my_date_field']);
}
if( isset($post_data['post_ID']) && isset($post_data['my_time_field'])){
update_post_meta( absint( $post_data['post_ID'] ), '_my_time_field', $post_data['my_time_field']);
}
if( isset($post_data['post_ID']) && isset($post_data['my_time_field2'])){
update_post_meta( absint( $post_data['post_ID'] ), '_my_time_field2', $post_data['my_time_field2']);
}
}
add_action( 'wcmp_process_product_object', 'save_custom_general_data', 100, 2 );
function wcmp_display_custom_field() {
global $post;
// Check for the custom field value
$product = wc_get_product( $post->ID );
$title = $product->get_meta( '_custom_product_date' );
$date = $product->get_meta( '_my_date_field');
$time = $product->get_meta( '_my_time_field');
$time2 = $product->get_meta( '_my_time_field2');
//Set Date And Time OF The countdown
$todayDate = date('Y-m-d');
$d1 = strtotime("$date 00:00:00");
$d2 = strtotime("$todayDate 00:00:00");
$tsd = $d1-$d2;
$tr = $d1 - time();
$tdays = floor ($tr / 86400);
$tminutes = $tsd /60;
$thours = floor (($tr -$tdays * 86400)/3600);
$mrn = 1440 - 60 * $thours - $tminutes;
$hrm = $mrn / 60;
//time travel
$s = $tsd%60;
$m = floor(($tsd%3600)/60);
$h = floor($tsd / 3600);
if($tr > 0)
{
echo "<h4>Available $title</h4><h5>from $time to $time2</h5> <p> $tdays day(s) $thours hour(s) left </p>";
}
else if ($date == "")
{
echo "";
}
else {
add_filter( 'woocommerce_variation_is_purchasable', 'filter_is_purchasable', 10, 2 );
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable', 10, 2);
function filter_is_purchasable() {
return false;
return is_purchasable;
};
echo "<h4> Product Unvailable</h4>";
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'wcmp_display_custom_field' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment