3

I want to allow backorders for specific customer roles. Code i writed is:

// BackOrder Allow Part #1
add_filter( 'woocommerce_product_backorders_allowed', 'woocommerce_product_backorders_allowed', 10, 3 );
function woocommerce_product_backorders_allowed( $backorder_allowed, $product_id, $product ){
    if ( current_user_can('administrator') ) {
        $backorder_allowed = true;
    } else {
        $backorder_allowed = false;
    }

    return $backorder_allowed;
}

// BackOrder Allow Part #2
add_filter( 'woocommerce_product_get_stock_status', 'woocommerce_product_get_stock_status', 10, 2 );
function woocommerce_product_get_stock_status( $stock_status, $product ) {
    if ( current_user_can('administrator') ) {
            return 'onbackorder';
    }
    else {
        return 'outofstock';
    }
    
}

Seems this gived result only for simple product. How i can make work for other product types. I need to work for simple, variable and yith-bundle. Any help will be appreciated! thanks!

LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275

2 Answers2

2

There are some mistakes and missing things in your code. Use the following instead:

// Custom conditional function targeting specific user roles
function is_allowed_user_role() {
    $targeted_roles = array('administrator', 'shop_manager'); // 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;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
0

I did change on second function. If it's outofstock, like qty = 0 or stockstatus be outofstock, to allow backorder

// Custom conditional fuction to target specific user roles
function is_allowed_user_role() {
    $targeted_roles = array('administrator', 'shop_manager'); // 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()  && 'outofstock' === $stock_status ) {
        $stock_status = 'onbackorder';
    }
    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;
}