Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'end_wcfm_membership_registration_form', function() {
- $selected_categories = array();
- $product_categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0&parent=0' );
- $p_category_options = array();
- foreach ( $product_categories as $cat ) {
- $p_category_options[$cat->term_id] = array( 'name' => $cat->name, 'parent' => 0 );
- }
- if( !empty( $p_category_options ) ) showRegistrationCategoryHierarchySelector( $p_category_options, $selected_categories );
- ?>
- <script>
- jQuery(document).ready(function($) {
- $('.wcfm_category_hierarchy').each(function() {
- $(this).change(function() {
- $level = parseInt($(this).data('level'));
- $value = $(this).val();
- if( $('.p_category_'+($level+1)).length > 0 ) {
- $('.p_category_'+($level+1)).addClass('wcfm_custom_hide');
- $('.p_category_label_'+($level+1)).addClass('wcfm_custom_hide');
- if( $value ) {
- $.each($value, function( $i, $svalue ) {
- if( $('.p_category_'+($level+1)).find('.cat_parent_'+$svalue).length > 0 ) {
- $('.p_category_'+($level+1)).removeClass('wcfm_custom_hide');
- $('.p_category_label_'+($level+1)).removeClass('wcfm_custom_hide');
- }
- });
- }
- $level_1 = $('.p_category_'+($level+1)).val();
- $('.p_category_'+($level+1)).val('');
- $('.p_category_'+($level+1)).find('option').addClass('wcfm_custom_hide');
- if( $value ) {
- $.each($value, function( $i, $svalue ) {
- $('.p_category_'+($level+1)).find('.cat_parent_'+$svalue).removeClass('wcfm_custom_hide');
- });
- }
- if( $level_1 ) {
- if( !$('.p_category_'+($level+1)).find('option[value="'+$level_1+'"]').hasClass('wcfm_custom_hide') ) {
- $('.p_category_'+($level+1)).val($level_1);
- }
- }
- $('.p_category_'+($level+1)).change();
- }
- }).change();
- });
- });
- </script>
- <?php
- });
- function showRegistrationCategoryHierarchySelector( $p_category_options, $selected_categories, $level = 0 ) {
- global $WCFM, $WCFMch;
- $p_category_child_options = array();
- $ptax_custom_arrtibutes = apply_filters( 'wcfm_taxonomy_custom_attributes', array(), 'product_cat' );
- ?>
- <p class="wcfm_title p_category_label_<?php echo $level; ?>">
- <strong>
- <?php
- echo apply_filters( 'wcfm_taxonomy_custom_label', '', 'product_cat' );
- if( $level ) _e( 'Sub-', 'wcfm-category-hierarchy' );
- _e( 'Categories', 'wc-frontend-manager' );
- ?>
- <span class="required">*</span>
- </strong>
- </p>
- <label class="screen-reader-text" for="product_cats">
- <?php
- if( $level ) echo __( 'Sub-', 'wcfm-category-hierarchy' );
- echo apply_filters( 'wcfm_taxonomy_custom_label', __( 'Categories', 'wc-frontend-manager' ), 'product_cat' );
- ?>
- </label>
- <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 ); ?>>
- <option value=""><?php _e( '-- Select Category --', 'wcfm-category-hierarchy' ); ?></option>
- <?php
- foreach( $p_category_options as $term_id => $term_details ) {
- $cat_group_class = 'cat_parent_' . $term_details['parent'];
- 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>';
- $product_child_taxonomies = get_terms( 'product_cat', 'orderby=name&hide_empty=0&parent=' . absint( $term_id ) );
- if ( $product_child_taxonomies ) {
- foreach ( $product_child_taxonomies as $cat ) {
- $p_category_child_options[$cat->term_id] = array( 'name' => $cat->name, 'parent' => $term_id );
- }
- }
- }
- ?>
- </select>
- <?php
- $level++;
- if( !empty( $p_category_child_options ) ) showRegistrationCategoryHierarchySelector( $p_category_child_options, $selected_categories, $level );
- }
- add_filter('wcfm_taxonomy_custom_attributes', function($attributes, $taxonomy) {
- if( 'product_cat' === $taxonomy ) {
- $attributes[] = 'data-required="1"';
- $attributes[] = 'data-required_message="Please select at least one category."';
- }
- return $attributes;
- }, 10, 2);
- add_action( 'wcfm_membership_registration', function( $member_id, $wcfm_membership_registration_form_data ) {
- if( isset( $wcfm_membership_registration_form_data['reg_category'] ) ) {
- update_user_meta( $member_id, 'wcfm_vendor_reg_category', $wcfm_membership_registration_form_data['reg_category'] );
- }
- }, 50, 2 );
- add_filter('wcfm_allowed_taxonomies', function($is_allowed, $taxonomy, $term_id) {
- global $wp;
- $vendor_id = 0;
- if($taxonomy === 'product_cat') {
- if(wcfm_is_vendor()) {
- $vendor_id = apply_filters( 'wcfm_current_vendor_id', get_current_user_id() );
- } else if(isset( $wp->query_vars['wcfm-products-manage'] ) && !empty( $wp->query_vars['wcfm-products-manage'] ) ) {
- $vendor_id = wcfm_get_vendor_id_by_post($wp->query_vars['wcfm-products-manage']);
- }
- if($vendor_id) {
- $allowed_cat = get_user_meta($vendor_id, 'wcfm_vendor_reg_category', true);
- $allowed_parent_cat = empty($allowed_cat) ? $allowed_cat : reset($allowed_cat);
- if(!empty($allowed_parent_cat)) {
- return in_array($term_id, $allowed_parent_cat);
- }
- }
- }
- return $is_allowed;
- }, 10, 3);
- add_action('end_wcfm_membership_registration_form', function() {
- ?>
- <script>
- jQuery(function($) {
- $( document.body ).on( 'wcfm_form_validate', function(event, form) {
- $form = $(form);
- $elems = $form.find('select.wcfm_category_hierarchy:visible');
- $elems.each( function() {
- $elem = $(this);
- var data_val = [];
- if($elem.length && $elem.data('required')==1) {
- data_val = $elem.val();
- if(data_val.length === 0 || (data_val.length === 1 && data_val[0] === "")) {
- if( $wcfm_is_valid_form ) {
- $('#' + $form.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + $elem.data('required_message') ).addClass('wcfm-error').slideDown();
- } else {
- $('#' + $form.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + $elem.data('required_message') );
- }
- $wcfm_is_valid_form = false;
- $elem.removeClass('wcfm_validation_success').addClass('wcfm_validation_failed');
- }
- }
- });
- });
- });
- </script>
- <?php
- });
Advertisement
Add Comment
Please, Sign In to add comment