php - WooCommerce product categories custom fields - Setting time purchasing restrictions -
i have wordpress/woocommerce site displays categorized products. i'm trying create custom field in admin individual categories pages (backend), can set start , end time related categorized products purchase availability.
how should work:
customers purchase products category "open" (between start , end time set category) products still visible if not purchasable.
i wondering if has idea best way begin , achieve that.
for moment here related code have:
function custom_product_taxonomy_add_new_meta_field() { //add term page //this add custom meta field add new term page ?> <div class="form-field"> <label for="term_meta[custom_term_meta]"><?php _e( 'post code(s)' ); ?></label> <input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value=""> <p class="description"><?php _e( 'enter delivery post codes available store.','my-text-domain' ); ?></p> </div> <?php } add_action( 'product_cat_add_form_fields', 'custom_product_taxonomy_add_new_meta_field', 10, 2 ); //edit term page function custom_product_taxonomy_edit_meta_field($term) { //put term id variable $t_id = $term->term_id; //retrieve existing value(s) meta field. returns array $term_meta = get_option( "taxonomy_$t_id" ); ?> <tr class="form-field"> <th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'post code(s)' ); ?></label></th> <td> <input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>"> <p class="description"><?php _e( 'enter delivery post codes available store.' ); ?></p> </td> </tr> <?php } add_action( 'product_cat_edit_form_fields', 'custom_product_taxonomy_edit_meta_field', 10, 2 ); //save taxonomy fields callback function. function save_taxonomy_custom_meta( $term_id ) { if ( isset( $_post['term_meta'] ) ) { $t_id = $term_id; $term_meta = get_option( "taxonomy_$t_id" ); $cat_keys = array_keys( $_post['term_meta'] ); foreach ( $cat_keys $key ) { if ( isset ( $_post['term_meta'][$key] ) ) { $term_meta[$key] = $_post['term_meta'][$key]; } } update_option( "taxonomy_$t_id", $term_meta ); //save option array. } } add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 ); add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
this code adds custom field in admin categories pages, have said before, looking custom field can set starting , ending time.
please suggestions welcome, i've been on weeks , looking solutions.
thanks in advance.
concerning time availability of product categories, have chose add 3 custom selectors:
- one enabling functionality,
- one start time
- one duration (period):
in code snippet bellow, create 3 editable custom fields (the code compact since use for
loop generate hours selector options, instead of displaying 24 hours options…):
add_action( 'product_cat_edit_form_fields', 'availiability_product_taxonomy_edit_meta_field', 10, 2 ); add_action( 'product_cat_add_form_fields', 'availiability_product_taxonomy_edit_meta_field', 10, 2 ); function availiability_product_taxonomy_edit_meta_field($term) { //put term id variable $term_id = $term->term_id; //retrieve start , ends values $enable_time = get_term_meta( $term_id, 'enable_time', true ) == '1' ? '1' : '0'; $start_time = get_term_meta( $term_id, 'start_time', true ); $lenght_time = get_term_meta( $term_id, 'lenght_time', true ); ?> <tr class="form-field"> <th scope="row" valign="top"> <label for="time-availiability"><?php _e( 'time availiability' ,'my-theme-slug' ); ?></label> </th> <td> <span style="padding-right:8px"> <label style="padding-right:8px" for="enable_time"><b><?php _e( 'hours: ' ,'my-theme-slug' ); ?></b></label> <select name="enable_time" id="enable_time" class="postform"> <option <?php if ( $enable_time == '0') echo 'selected'; ?> value='0'><?php _e( 'disabled' ,'my-theme-slug' ); ?></option> <option <?php if ( $enable_time == '1') echo 'selected'; ?> value='1'><?php _e( 'enabled' ,'my-theme-slug' ); ?></option> </select> </span> <span style="padding-right:10px"> <label for="start_time"><?php _e( "start" ,"my-theme-slug" ); ?></label> <select name="start_time" id="start_time" class="postform"> <?php // genereting 24 options tags ($i = 0; $i < 24; $i++) { $j = $i < 10 ? '0'.$i : ''.$i ; // values on 2 digits $selected = $start_time == $j ? "selected" : ""; ?> <option <?php echo $selected; ?> value="<?php echo $j; ?>"><?php echo $j; ?></option> <?php } ?> </select> </span> <span> <label for="lenght_time"><?php _e( "length" ,"my-theme-slug" ); ?></label> <select name="lenght_time" id="lenght_time" class="postform"> <?php // genereting 24 options tags ($i = 0; $i < 25; $i++) { $j = $i < 10 ? '0'.$i : ''.$i ; // values on 2 digits $selected = $lenght_time == $j ? "selected" : ""; ?> <option <?php echo $selected; ?> value="<?php echo $j; ?>"><?php echo $j; ?></option> <?php } ?> </select> </span> </td> </tr> <?php }
then in code snippet below save custom fields data when submitted.
add_action( 'create_product_cat', 'availiability_taxonomy_custom_meta', 10, 2 ); add_action( 'edited_product_cat', 'availiability_taxonomy_custom_meta', 10, 2 ); function availiability_taxonomy_custom_meta( $term_id ) { if ( isset( $_post['enable_time'] ) ) { update_term_meta ($term_id, 'enable_time', $_post['enable_time'] ); } if ( isset( $_post['start_time'] ) ) { update_term_meta ($term_id, 'start_time', $_post['start_time'] ); } if ( isset( $_post['lenght_time'] ) ) { update_term_meta ($term_id, 'lenght_time', $_post['lenght_time'] ); } }
and in database if in wp_termmeta
table see category id (here id 11
when testing) 3 custom fields corresponding values:
this code using more recent functions included since wordpress 4.4+ (much better) found on other related threads: get_term_meta() , update_term_meta()
all code tested , functional. goes in function.php file located in active child theme or theme.
a reference (using new core functions): add custom field category
Comments
Post a Comment