Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
1.4 kB
2
Indexable
Never
function hide_woocommerce_stock_html( $html, $availability, $product ) {

    if (!is_agent_shopping()){
        // hide stock for non agents
        return '';
    }
    return $html;
}
add_filter( 'woocommerce_stock_html', 'hide_woocommerce_stock_html', 10, 3 );

function is_agent_shopping(){
    // 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;
}
Leave a Comment