• wael_elsaid

    (@wael_elsaid)


    Hi there,
    My website has many roles for users, and I allow the currently registered user with any role to apply to become a vendor on the site. The issue is when I approve any new vendor, his old roles are gone, for example if the current user is subscriber and applied to become a vendor, when I approve him, his role will be vendoe and the subscriber role will be removed. I’d like to keep the user old roles and add him the new vendor role, is this possible?

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • I also have that issue on my site. I was a bit confused at first as for why some users did not have the basic user role of subscriber. But later I found that this happened to all approved vendors. Would be nice to get that fixed.

    Thread Starter wael_elsaid

    (@wael_elsaid)

    Hey @zokkoz I’ve figured out a solution for this issue. Here is the code snippet:

    add_action(‘wp_ajax_wcfmvm_vendor_approval_response_update’, ‘wcfmvm_vendor_approval_response_update’);

    function wcfmvm_vendor_approval_response_update() {
    global $WCFM, $WCFMvm, $_POST, $wpdb;

    if ( ! check_ajax_referer( 'wcfm_ajax_nonce', 'wcfm_ajax_nonce', false ) ) {
        wp_send_json_error( esc_html__( 'Invalid nonce! Refresh your page and try again.', 'wc-frontend-manager' ) );
        wp_die();
    }
    
    if ( !current_user_can( 'manage_woocommerce' ) && !current_user_can( 'shop_staff' ) ) {
        wp_send_json_error( esc_html__( 'You don’t have permission to do this.', 'woocommerce' ) );
        wp_die();
    }
    
    $wcfm_vendor_approval_response_form_data = array();
    parse_str($_POST['wcfm_vendor_approval_response_form'], $wcfm_vendor_approval_response_form_data);
    $wcfm_vendor_approval_response_form_data = wc_clean( $wcfm_vendor_approval_response_form_data );
    
    if( isset( $wcfm_vendor_approval_response_form_data['wcfm_vendor_approval_message_id'] ) && isset($wcfm_vendor_approval_response_form_data['wcfm_vendor_approval_member_id']) ) {
        $message_id = absint( $wcfm_vendor_approval_response_form_data['wcfm_vendor_approval_message_id'] );
        $member_id  = absint( $wcfm_vendor_approval_response_form_data['wcfm_vendor_approval_member_id'] );
    
        if( $member_id && $message_id ) {
            $member_user = new WP_User(absint($member_id));
            $approval_status = $wcfm_vendor_approval_response_form_data['wcfm_vendor_approval_response_status'];
    
            // delete_user_meta( $member_id, 'wcfm_membership_application_status' );
    
    
            // Continue with your existing logic...
            if( $approval_status == 'approve' ) {
    
                // Check existing roles
                $existing_roles = $member_user->roles;
    
                // If the vendor role is not already assigned, add it
                if ( ! in_array( 'wcfm_vendor', $existing_roles ) ) {
                    $member_user->add_role( 'wcfm_vendor' );
                }
    
    
                // Add your approval logic here (register vendor, store subscription data, etc.)
                $paymode    = get_user_meta( $member_id, 'wcfm_membership_paymode', true );
                if( !$paymode ) $paymode = 'bank_transfer';
    
                // $has_error = $WCFMvm->register_vendor( $member_id );
    
                $membership_id = get_user_meta( $member_id, 'wcfm_membership', true );
                if( $membership_id ) {
                  $subscription = (array) get_post_meta( $membership_id, 'subscription', true );
                  $subscription_type = isset( $subscription['subscription_type'] ) ? $subscription['subscription_type'] : 'one_time';
                  $subscription_pay_mode = isset( $subscription['subscription_pay_mode'] ) ? $subscription['subscription_pay_mode'] : 'by_wcfm';
    
                  if( ($paymode != 'paypal') && ($paymode != 'stripe') ) {
                    $WCFMvm->store_subscription_data( $member_id, $paymode, '', $paymode.'_subscription', 'Completed', '' );
                    if( ( $subscription_type == 'recurring' ) && ( $subscription_pay_mode == 'by_wcfm' ) ) {
                      $WCFMvm->store_subscription_data( $member_id, $paymode.'_subs', '', $paymode.'_reccuring_subscription', 'Completed', '' );
                    }
                  }
                }
            }  else {
    
              $wcfm_membership_options = get_option( 'wcfm_membership_options', array() );
              $membership_reject_rules = array();
              if( isset( $wcfm_membership_options['membership_reject_rules'] ) ) $membership_reject_rules = $wcfm_membership_options['membership_reject_rules'];
              $vendor_reject_rule = isset( $membership_reject_rules['vendor_reject_rule'] ) ? $membership_reject_rules['vendor_reject_rule'] : 'same';
              $send_notification = isset( $membership_reject_rules['send_notification'] ) ? $membership_reject_rules['send_notification'] : 'yes';
    
              if( $send_notification == 'yes' ) {
                if( !defined( 'DOING_WCFM_EMAIL' ) ) 
                    define( 'DOING_WCFM_EMAIL', true );
    
                // Switch language context…
                if( apply_filters( 'wcfm_allow_wpml_email_translation', true ) ) {
                  do_action('wpml_switch_language_for_email', $member_user->user_email);
                }
    
                $rejection_reason = wcfm_stripe_newline( $wcfm_vendor_approval_response_form_data['wcfm_vendor_rejection_reason'] );
                $rejection_reason = esc_sql( $rejection_reason );
    
                $reject_notication_subject = wcfm_get_option( 'wcfm_membership_reject_notication_subject', '{site_name}: Vendor Application Rejected' );
                $reject_notication_content = wcfm_get_option( 'wcfm_membership_reject_notication_content', '' );
                if( !$reject_notication_content ) {
                  $reject_notication_content = "Hi {first_name},
                                                <br /><br />
                                                Sorry to inform you that, your vendor application has been rejected.&nbsp;
                                                <br /><br />
                                                <strong><i>{rejection_reason}</i></strong>
                                                <br /><br />
                                                Thank You";
                }
    
                $subject = str_replace( '{site_name}', get_bloginfo( 'name' ), $reject_notication_subject );
                $subject = apply_filters( 'wcfm_email_subject_wrapper', $subject );
                $message = str_replace( '{first_name}', $member_user->first_name, $reject_notication_content );
                $message = str_replace( '{rejection_reason}', $rejection_reason, $message );
                $message = apply_filters( 'wcfm_email_content_wrapper', $message, __( 'Vendor Application Rejected', 'wc-multivendor-membership' ) );
    
                wp_mail( $member_user->user_email, $subject, $message );
    
                // switch language back
                if( apply_filters( 'wcfm_allow_wpml_email_translation', true ) ) {
                  do_action('wpml_restore_language_from_email');
                }
    
              }
    
              if( apply_filters( 'wcfm_is_allow_delete_vendor_reject_user', true ) ) {
                $membership_id = get_user_meta( $member_id, 'temp_wcfm_membership', true );
                if( ( $membership_id != -1 ) && ( $membership_id != '-1' ) ) {
                  $vendor_reject_rule = get_post_meta( $membership_id, 'vendor_reject_rule', true ) ? get_post_meta( $membership_id, 'vendor_reject_rule', true ) : $vendor_reject_rule;
                }
                delete_user_meta( $member_id, 'temp_wcfm_membership' );
                delete_user_meta( $member_id, 'wcfm_membership_application_status' );
                if( $vendor_reject_rule == 'delete' ) {
                  wp_delete_user( $member_id );
                }
              }
            }
    
    
            // Vendor Approval message mark read
                $author_id = apply_filters( 'wcfm_message_author', get_current_user_id() );
                $todate = date('Y-m-d H:i:s');
    
                /*$wcfm_read_message     = "INSERT into {$wpdb->prefix}wcfm_messages_modifier 
                                                                        (message, is_read, read_by, read_on)
                                                                        VALUES
                                                                        ({$message_id}, 1, {$author_id}, '{$todate}')";
                $wpdb->query($wcfm_read_message);*/
    
            // Vendor Approval message mark read
            $wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->prefix}wcfm_messages WHERE ID =%d", $message_id ) );
    
            echo '{"status": true, "message": "' . esc_html( __( 'Vendor Approval status successfully updated.', 'wc-multivendor-membership' ) ) . '"}';
            die;
        }
    }
    echo '{"status": false, "message": "' . esc_html( __( 'Vendor Approval status update failed.', 'wc-multivendor-membership' ) ) . '"}';
    die;

    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.