palsushobhan

mandatory-product-categories-field-on-vendor-registration-form

Aug 1st, 2025
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.94 KB | None | 0 0
  1. add_action( 'end_wcfm_membership_registration_form', function() {
  2.     $selected_categories = array();
  3.    
  4.     $product_categories   = get_terms( 'product_cat', 'orderby=name&hide_empty=0&parent=0' );
  5.     $p_category_options   = array();
  6.     foreach ( $product_categories as $cat ) {
  7.         $p_category_options[$cat->term_id] = array( 'name' => $cat->name, 'parent' => 0 );
  8.     }
  9.    
  10.     if( !empty( $p_category_options ) ) showRegistrationCategoryHierarchySelector( $p_category_options, $selected_categories );
  11.     ?>
  12.     <script>
  13.     jQuery(document).ready(function($) {
  14.         $('.wcfm_category_hierarchy').each(function() {
  15.             $(this).change(function() {
  16.                 $level = parseInt($(this).data('level'));
  17.                 $value = $(this).val();
  18.                 if( $('.p_category_'+($level+1)).length > 0 ) {
  19.                     $('.p_category_'+($level+1)).addClass('wcfm_custom_hide');
  20.                     $('.p_category_label_'+($level+1)).addClass('wcfm_custom_hide');
  21.                     if( $value ) {
  22.                         $.each($value, function( $i, $svalue ) {
  23.                             if( $('.p_category_'+($level+1)).find('.cat_parent_'+$svalue).length > 0 ) {
  24.                                 $('.p_category_'+($level+1)).removeClass('wcfm_custom_hide');
  25.                                 $('.p_category_label_'+($level+1)).removeClass('wcfm_custom_hide');
  26.                             }
  27.                         });
  28.                     }
  29.                     $level_1 = $('.p_category_'+($level+1)).val();
  30.                     $('.p_category_'+($level+1)).val('');
  31.                     $('.p_category_'+($level+1)).find('option').addClass('wcfm_custom_hide');
  32.                     if( $value ) {
  33.                         $.each($value, function( $i, $svalue ) {
  34.                             $('.p_category_'+($level+1)).find('.cat_parent_'+$svalue).removeClass('wcfm_custom_hide');
  35.                         });
  36.                     }
  37.                     if( $level_1 ) {
  38.                         if( !$('.p_category_'+($level+1)).find('option[value="'+$level_1+'"]').hasClass('wcfm_custom_hide') ) {
  39.                             $('.p_category_'+($level+1)).val($level_1);
  40.                         }
  41.                     }
  42.                     $('.p_category_'+($level+1)).change();
  43.                 }
  44.             }).change();
  45.            
  46.         });
  47.     });
  48.     </script>
  49.     <?php
  50. });
  51.  
  52. function showRegistrationCategoryHierarchySelector( $p_category_options, $selected_categories, $level = 0 ) {
  53.     global $WCFM, $WCFMch;
  54.    
  55.     $p_category_child_options = array();
  56.     $ptax_custom_arrtibutes = apply_filters( 'wcfm_taxonomy_custom_attributes', array(), 'product_cat' );
  57.     ?>
  58.     <p class="wcfm_title p_category_label_<?php echo $level; ?>">
  59.         <strong>
  60.             <?php
  61.             echo apply_filters( 'wcfm_taxonomy_custom_label', '', 'product_cat' );
  62.             if( $level ) _e( 'Sub-', 'wcfm-category-hierarchy' );
  63.             _e( 'Categories', 'wc-frontend-manager' );
  64.             ?>
  65.             <span class="required">*</span>
  66.         </strong>
  67.     </p>
  68.     <label class="screen-reader-text" for="product_cats">
  69.         <?php
  70.         if( $level ) echo __( 'Sub-', 'wcfm-category-hierarchy' );
  71.         echo apply_filters( 'wcfm_taxonomy_custom_label', __( 'Categories', 'wc-frontend-manager' ), 'product_cat' );
  72.         ?>
  73.     </label>
  74.     <select id="p_category_<?php echo $level; ?>" style="height: auto;" multiple name="reg_category[<?php echo $level; ?>][]" class="wcfm-select wcfm_category_hierarchy p_category_<?php echo $level; ?>" data-level="<?php echo $level; ?>" data-catlimit="1" <?php echo implode( ' ', $ptax_custom_arrtibutes ); ?>>
  75.         <option value=""><?php _e( '-- Select Category --', 'wcfm-category-hierarchy' ); ?></option>
  76.         <?php
  77.         foreach( $p_category_options as $term_id => $term_details ) {
  78.             $cat_group_class = 'cat_parent_' . $term_details['parent'];
  79.             echo '<option class="' . $cat_group_class . '" value="' . esc_attr( $term_id ) . '"' . selected( in_array( $term_id, $selected_categories ), true, false ) . '>' . __( esc_html( $term_details['name'] ), 'wcfm-category-hierarchy' ) . '</option>';
  80.            
  81.             $product_child_taxonomies   = get_terms( 'product_cat', 'orderby=name&hide_empty=0&parent=' . absint( $term_id ) );
  82.             if ( $product_child_taxonomies ) {
  83.                 foreach ( $product_child_taxonomies as $cat ) {
  84.                     $p_category_child_options[$cat->term_id] = array( 'name' => $cat->name, 'parent' => $term_id );
  85.                 }
  86.             }
  87.         }
  88.         ?>
  89.     </select>
  90.     <?php
  91.     $level++;
  92.     if( !empty( $p_category_child_options ) ) showRegistrationCategoryHierarchySelector( $p_category_child_options, $selected_categories, $level );
  93. }
  94.  
  95. add_filter('wcfm_taxonomy_custom_attributes', function($attributes, $taxonomy) {
  96.     if( 'product_cat' === $taxonomy ) {
  97.         $attributes[] = 'data-required="1"';
  98.         $attributes[] = 'data-required_message="Please select at least one category."';
  99.     }
  100.     return $attributes;
  101. }, 10, 2);
  102.  
  103. add_action( 'wcfm_membership_registration', function( $member_id, $wcfm_membership_registration_form_data ) {
  104.     if( isset( $wcfm_membership_registration_form_data['reg_category'] ) ) {
  105.         update_user_meta( $member_id, 'wcfm_vendor_reg_category', $wcfm_membership_registration_form_data['reg_category'] );
  106.     }
  107. }, 50, 2 );
  108.  
  109. add_filter('wcfm_allowed_taxonomies', function($is_allowed, $taxonomy, $term_id) {
  110.     global $wp;
  111.     $vendor_id = 0;
  112.     if($taxonomy === 'product_cat') {
  113.         if(wcfm_is_vendor()) {
  114.             $vendor_id = apply_filters( 'wcfm_current_vendor_id', get_current_user_id() );
  115.         } else if(isset( $wp->query_vars['wcfm-products-manage'] ) && !empty( $wp->query_vars['wcfm-products-manage'] ) ) {
  116.             $vendor_id = wcfm_get_vendor_id_by_post($wp->query_vars['wcfm-products-manage']);
  117.         }
  118.         if($vendor_id) {
  119.             $allowed_cat = get_user_meta($vendor_id, 'wcfm_vendor_reg_category', true);
  120.             $allowed_parent_cat = empty($allowed_cat) ? $allowed_cat : reset($allowed_cat);
  121.             if(!empty($allowed_parent_cat)) {
  122.                 return in_array($term_id, $allowed_parent_cat);
  123.             }
  124.         }
  125.     }
  126.     return $is_allowed;
  127. }, 10, 3);
  128.  
  129. add_action('end_wcfm_membership_registration_form', function() {
  130.     ?>
  131.     <script>
  132.         jQuery(function($) {
  133.             $( document.body ).on( 'wcfm_form_validate', function(event, form) {
  134.                 $form = $(form);
  135.                 $elems = $form.find('select.wcfm_category_hierarchy:visible');
  136.                 $elems.each( function() {
  137.                     $elem = $(this);
  138.                     var data_val = [];
  139.                     if($elem.length && $elem.data('required')==1) {
  140.                         data_val = $elem.val();
  141.                         if(data_val.length === 0 || (data_val.length === 1 && data_val[0] === "")) {
  142.                             if( $wcfm_is_valid_form ) {
  143.                                 $('#' + $form.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + $elem.data('required_message') ).addClass('wcfm-error').slideDown();
  144.                             } else {
  145.                                 $('#' + $form.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + $elem.data('required_message') );
  146.                             }
  147.                             $wcfm_is_valid_form = false;
  148.                             $elem.removeClass('wcfm_validation_success').addClass('wcfm_validation_failed');
  149.                         }
  150.                     }
  151.                 });
  152.             });
  153.         });
  154.     </script>
  155.     <?php
  156. });
Advertisement
Add Comment
Please, Sign In to add comment