Untitled
unknown
plain_text
a year ago
3.0 kB
5
Indexable
Never
function is_agent_shopping_as_customer(){ // check if switch cookie is set if (isset($_COOKIE['salesking_switch_cookie'])){ $switch_to = sanitize_text_field($_COOKIE['salesking_switch_cookie']); $current_id = get_current_user_id(); if (!empty($switch_to) && is_user_logged_in()){ // show bar $udata = get_userdata( get_current_user_id() ); $name = $udata->first_name.' '.$udata->last_name; // get agent details $agent = explode('_',$switch_to); $customer_id = intval($agent[0]); $agent_id = intval($agent[1]); $agent_registration = $agent[2]; // check real registration in database $udataagent = get_userdata( $agent_id ); $registered_date = $udataagent->user_registered; // if current logged in user is the one in the cookie + agent cookie checks out if ($current_id === $customer_id && $agent_registration === $registered_date){ return true; } } } return false; } add_action( 'woocommerce_edit_account_form', 'cssigniter_add_account_details' ); function cssigniter_add_account_details() { $user = wp_get_current_user(); if (is_agent_shopping_as_customer()){ ?> <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide"> <select name="b2bking_customergroup" id="b2bking_customergroup" class="b2bking_user_settings_select"> <?php $user_is_b2b = get_user_meta( $user->ID, 'b2bking_b2buser', true ); if ($user_is_b2b === 'yes'){ // do nothing } else { $user_is_b2b = 'no'; } ?> <optgroup label="<?php esc_html_e('B2C Group', 'b2bking'); ?>"> <?php echo '<option value="b2cuser" '.selected('no', $user_is_b2b, false).'>'.esc_html__('B2C Users', 'b2bking').'</option>'; ?> </optgroup> <optgroup label="<?php esc_html_e('B2B Groups', 'b2bking'); ?>"> <?php $posts = get_posts([ 'post_type' => 'b2bking_group', 'post_status' => 'publish', 'numberposts' => -1 ]); foreach ($posts as $post){ if ($user_is_b2b === 'yes'){ // if user is b2b, select the b2b group echo '<option value="'.esc_attr($post->ID).'" '.selected($post->ID, b2bking()->get_user_group($user->ID),false).'>'.esc_html($post->post_title).'</option>'; } else { // if user is b2c, dont select the b2b group echo '<option value="'.esc_attr($post->ID).'" >'.esc_html($post->post_title).'</option>'; } } ?> </optgroup> </select> </p> <?php } } add_action( 'woocommerce_save_account_details', 'cssigniter_save_account_details' ); function cssigniter_save_account_details( $user_id ) { if ( isset( $_POST['b2bking_customergroup'] ) ) { update_user_meta( $user_id, 'b2bking_customergroup', sanitize_text_field( $_POST['b2bking_customergroup'] ) ); if ($_POST['b2bking_customergroup'] !== 'b2cuser'){ update_user_meta( $user_id, 'b2bking_b2buser', 'yes' ); } else { update_user_meta( $user_id, 'b2bking_b2buser', 'no' ); } } }