Advertisement
palsushobhan

vendor-birthday

Apr 25th, 2024
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.30 KB | None | 0 0
  1. add_filter('wcfm_profile_fields_general', function($fields, $user_id) {
  2.     $birth_month = get_user_meta($user_id, 'user_birth_month', true);
  3.     $birth_date = get_user_meta($user_id, 'user_birth_date', true);
  4.     $month_options = ['Select month', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  5.     $days = ['Select date', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31];
  6.     $birthday = array(
  7.         "birth_month" => array('label' => __('Birthday', 'wc-frontend-manager') , 'type' => 'select', 'options' => $month_options, 'class' => 'wcfm-select wcfm_ele birth_field', 'label_class' => 'wcfm_title wcfm_ele', 'value' => $birth_month ),
  8.         "birth_date" => array('type' => 'select', 'options' => $days, 'class' => 'wcfm-select wcfm_ele birth_field', 'label_class' => 'wcfm_title wcfm_ele', 'value' => $birth_date ),
  9.     );
  10.     if(!$birth_date) {
  11.         $birthday['birth_date']['attributes'] = array( 'disabled' => 'disabled' );
  12.     }
  13.     return array_merge($fields, $birthday);
  14. }, 10, 2);
  15.  
  16. add_action('end_wcfm_user_profile', function() {
  17.     ?>
  18.     <style>
  19.         #wcfm_profile_personal_expander select.birth_field {
  20.             width: 29%;
  21.         }
  22.         #wcfm_profile_personal_expander select.birth_field:first-of-type {
  23.             margin-right: 2%;
  24.         }
  25.     </style>
  26.     <script>
  27.         jQuery(function($) {
  28.             $('#wcfm_profile_personal_expander select#birth_month').change(function() {
  29.                 var monthSelection = $(this).val();
  30.                 var $dateDropdown = $('#wcfm_profile_personal_expander select#birth_date');
  31.                 if(monthSelection > 0) {
  32.                     var month_total_days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  33.                     var monthDays = month_total_days[monthSelection-1];
  34.                     $dateDropdown.find('option').css('display', '');
  35.                     for(i = monthDays+1; i<=31; i++) {
  36.                         $dateDropdown.find('option').eq(i).css('display', 'none');
  37.                     }
  38.                     $dateDropdown.prop('disabled', false);
  39.                 } else {
  40.                     $dateDropdown[0].selectedIndex = 0;
  41.                     $dateDropdown.prop('disabled', true);
  42.                 }
  43.             });
  44.         });
  45.     </script>
  46.     <?php
  47. });
  48.  
  49. add_action('wcfm_profile_update', function($user_id, $wcfm_profile_form) {
  50.     if(!empty($wcfm_profile_form['birth_month']) && !empty($wcfm_profile_form['birth_date'])) {
  51.         update_user_meta($user_id, 'user_birth_month', $wcfm_profile_form['birth_month']);
  52.         update_user_meta($user_id, 'user_birth_date', $wcfm_profile_form['birth_date']);
  53.     } else {
  54.         delete_user_meta($user_id, 'user_birth_month');
  55.         delete_user_meta($user_id, 'user_birth_date');
  56.     }
  57. }, 10, 2);
  58.  
  59. add_shortcode('vendor_birthday', function($attr) {
  60.     global $post;
  61.     $store_id = '';
  62.     if ( isset( $attr['id'] ) && ! empty( $attr['id'] ) ) {
  63.         $store_id = absint( $attr['id'] );
  64.     }
  65.     if ( wcfm_is_store_page() ) {
  66.         $wcfm_store_url = get_option( 'wcfm_store_url', 'store' );
  67.         $store_name = apply_filters( 'wcfmmp_store_query_var', get_query_var( $wcfm_store_url ) );
  68.         $store_id = 0;
  69.         if ( ! empty( $store_name ) ) {
  70.             $store_user = get_user_by( 'slug', $store_name );
  71.             $store_id = $store_user->ID;
  72.         }
  73.     }
  74.     if ( is_product() ) {
  75.         $store_id = $post->post_author;
  76.     }
  77.     if( !$store_id ) return;
  78.     $month_options = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  79.     $birth_month = get_user_meta($store_id, 'user_birth_month', true);
  80.     $birth_date = get_user_meta($store_id, 'user_birth_date', true);
  81.     $birth_date_str = $birth_date;
  82.     if($birth_date % 10 == 0 || $birth_date % 10 > 3 || in_array($birth_date, [11, 12, 13])) $birth_date_str .= 'th ';
  83.     elseif($birth_date % 10 == 1 ) $birth_date_str .= 'st ';
  84.     elseif($birth_date % 10 == 2 ) $birth_date_str .= 'nd ';
  85.     elseif($birth_date % 10 == 3 ) $birth_date_str .= 'rd ';
  86.    
  87.     $birth_date_str .= $month_options[$birth_month - 1];
  88.     return "<p>{$birth_date_str}</p>";
  89. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement