I have some products without a woocommerce-price. Using code below removes [add-to-cart]-button if price is not above 0:-. But I would still like Agents to be able to [add-to-cart] and input their own price on the cart-page instead.
So I would like to have the code below execute ONLY if customer is logged in and not if Agent is logged in as customer. Is it possible to somehow configure that?
I have some products without a woocommerce-price. Using code below removes [add-to-cart]-button if price is not above 0:-. But I would still like Agents to be able to [add-to-cart] and input their own price on the cart-page instead.
So I would like to have the code below execute ONLY if customer is logged in and not if Agent is logged in as customer. Is it possible to somehow configure that?
if(current_user_can('customer')) {
function remove_add_to_cart_on_0 ( $purchasable, $product ){
if( $product->get_price() == 0 )
$purchasable = false;
return $purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'remove_add_to_cart_on_0', 99, 2 );
}
Hi Mervin,
I believe the following code should do it:
if(current_user_can('customer')) { function remove_add_to_cart_on_0 ( $purchasable, $product ){ if( $product->get_price() == 0 ) $purchasable = false; return $purchasable; } function check_user_is_agent_with_access(){ // 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; } if (!check_user_is_agent_with_access()){ add_filter( 'woocommerce_is_purchasable', 'remove_add_to_cart_on_0', 99, 2 ); } }Please check and let me know.
Kind regards,
Stefan