Okay
  Public Ticket #3201288
Back Orders for sales Agents Only
Closed

Comments

  •  15
    Darren Robinson started the conversation

    Hi Stefan,

    At the moment I have some code that you helped me with a while ago that only allows user role 'reps' to see back order products and be able to place backorders. 


    Is it possible to add some code so that only sales agents can see back order products and be able to place back orders when shopping as a customer?

    Thank you for your time

    Darren

  •  2,218
    WebWizards replied

    Hi Darren,

    Hope you've you had a great holiday!


    I wrote this snippet which should apply a piece of code only if the current user is an agent OR is an agent shopping  as a customer.

    We then need to add the code for backorders where it says 'CODE HERE'.

    if (check_user_is_agent() or check_is_agent()){
        // CODE HERE
    }
    function check_is_agent(){
        $user_id = get_current_user_id();
        $agent_group = get_user_meta($user_id,'salesking_group',true);
        if ($agent_group !== 'none' && !empty($agent_group)){
            return true;
        } else {
            return false;
        }
    }
    function check_user_is_agent(){
        // 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;
    }
    


    I checked our ticket history but I can't seem to find the code which allows reps to see back orders. Do you have that anywhere and could send it to me?

    It would be easier than writing that again. If you don't have it, no problem.


    Kind regards,

    Stefan


  •  15
    Darren Robinson replied

    Hi Stefan, the code Ihave for only User role 'reps' to see and place back orders is,

    // Custom conditional function targeting specific user roles
    function is_allowed_user_role() {
        $targeted_roles = array('administrator', 'reps'); // Here define your targeted user roles
        return (bool) array_intersect( wp_get_current_user()->roles, $targeted_roles );
    }

    add_filter( 'woocommerce_product_get_stock_status', 'filter_product_stock_status' );
    add_filter( 'woocommerce_product_variation_get_stock_status', 'filter_product_stock_status' );
    function filter_product_stock_status( $stock_status ) {
        if ( ! is_allowed_user_role() && 'onbackorder' === $stock_status ) {
            $stock_status = 'outofstock';
        }
        return $stock_status;
    }

    add_filter( 'woocommerce_product_get_backorders', 'filter_product_get_backorders' );
    add_filter( 'woocommerce_product_variation_get_backorders', 'filter_product_get_backorders' );
    function filter_product_get_backorders( $backorders ){
        return is_allowed_user_role() ? $backorders : 'no';
    }

    add_filter( 'woocommerce_product_backorders_allowed', 'filter_product_backorders_allowed', 10, 3 );
    function filter_product_backorders_allowed( $allowed, $product_id, $product ){
        return is_allowed_user_role() ? $allowed : false;
    }

    Thanks

    Darren


  •  2,218
    WebWizards replied

    Hi Darren,

    I think you can this code to your site to achieve that:

    // Custom conditional function targeting specific user roles
    function is_allowed_user_role2() {
        if (check_user_is_agent() or check_is_agent()){
            return true;
        } else {
            return false;
        }
    }
    add_filter( 'woocommerce_product_get_stock_status', 'filter_product_stock_status2' );
    add_filter( 'woocommerce_product_variation_get_stock_status', 'filter_product_stock_status2' );
    function filter_product_stock_status2( $stock_status ) {
        if ( ! is_allowed_user_role2() && 'onbackorder' === $stock_status ) {
            $stock_status = 'outofstock';
        }
        return $stock_status;
    }
    add_filter( 'woocommerce_product_get_backorders', 'filter_product_get_backorders2' );
    add_filter( 'woocommerce_product_variation_get_backorders', 'filter_product_get_backorders2' );
    function filter_product_get_backorders2( $backorders ){
        return is_allowed_user_role2() ? $backorders : 'no';
    }
    add_filter( 'woocommerce_product_backorders_allowed', 'filter_product_backorders_allowed2', 10, 3 );
    function filter_product_backorders_allowed2( $allowed, $product_id, $product ){
        return is_allowed_user_role2() ? $allowed : false;
    }
    function check_is_agent(){
        $user_id = get_current_user_id();
        $agent_group = get_user_meta($user_id,'salesking_group',true);
        if ($agent_group !== 'none' && !empty($agent_group)){
            return true;
        } else {
            return false;
        }
    }
    function check_user_is_agent(){
        // 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;
    }
    

    It should work for agents or agents shopping as a customer

  •  15
    Darren Robinson replied

    Hi Stefan, 

    This works great, thank you.

    Can i ask a question about B2BKing that we use? We are still using version 3 as you told me how to make the Tiered pricing table show on the shop page but when i updated to the new plugin version with B2Bking core and B2BKing pro the Tiered pricing table no longer shows on the shop and category pages?

    Thanks

    Darren

  •  2,218
    WebWizards replied

    Do you also use the other code for 'reps' as well?

    If you have both pieces of code active I'm thinking they may be conflicting with each other. 

    The older code is working but this newer one is not?

  •  15
    Darren Robinson replied

    Hi Stefan<

    The code is working fine now afteri turned off the other code snippet and the backorder products are only showing for an agent shopping as a customer, thank you. 


    Did you get the message about the B2BKing?


    Thanks

    Darren

  •  15
    Darren Robinson replied

    Hi Stefan, 

    Can i ask a question about B2BKing that we use? We are still using version 3 as you told me how to make the Tiered pricing table show on the shop page but when i updated to the new plugin version with B2Bking core and B2BKing pro the Tiered pricing table no longer shows on the shop and category pages?

    Thanks

    Darren

  •  2,218
    WebWizards replied

    Hi Darren,

    Glad to hear that worked,


    when i updated to the new plugin version with B2Bking core and B2BKing pro the Tiered pricing table no longer shows on the shop and category pages?

    I'm not really sure why that would be I'm afraid. There have been many changes since v3 and I can't precisely remember what may have changed.

    However I'd be happy to check the issue on your site directly if you'd want. Would you be able to share a backend login to the site or to a staging clone site so I can look into it?



  •   Darren Robinson replied privately
  •  2,218
    WebWizards replied

    Hi Darren,

    I'm not sure why, at the moment any link I access on https://huggablesltd.co.uk shows the following message: (I tried /staging /staging/my-account and others)

    1494835590.png


    I thought it might be my IP but I also tried with a VPN and it's the same message.

    Can you check please?

  •  15
    Darren Robinson replied

    HI Stefan, What country are you in as it may be blocked i will allow it?


    ThanksDarren

  •  2,218
    WebWizards replied

    Ah, I figured it out, had to set the VPN to use a UK address.

    I logged in to the site and I updated the B2BKing Pro plugin to 4.4.55

    Then I activated B2BKing Core + B2BKing Pro and now I can see the table when going here:

    http://huggablesltd.co.uk/staging/product/squeezy-12cm-spaceman-cdu-x-12/


    Starting a few versions ago, it's now needed to have 2 plugins active at the same time: B2BKing Core (free plugin) and B2BKing Pro (paid). The 2 now work together.

  •   WebWizards replied privately
  •  15
    Darren Robinson replied

    hi Stefan,

    The tiered pricing table is fine on the single product pages but it needs to show on the shop and category pages? it works if you turn off the core and pro plugin and just have the old version 3 active, 

    You gave me some code to add to the plugin files before that made it show on the shop page as well, but this doesn’t work when I activate core & pro plugins. 


    Thanks
    Darren 

  •   WebWizards replied privately
  •  15
    Darren Robinson replied

    Thanks, Stefan, that worked great. Will it still work next time I update the core or pro plugins?


    Thanks

    Darren

  •   WebWizards replied privately
  •  15
    Darren Robinson replied

    Thank you Stefan, I will leave a review


    Darren