WooCommerce:如何修改特定用户角色预订代码使其支持多产品类型(简单、可变、YITH捆绑)
Fix WooCommerce Backorders for Simple, Variable, and YITH Bundle Products (Admin Only)
Hey there, I see your code works for simple products but misses variable and YITH bundle products—let's fix that! The main issue is your original code doesn't account for the different product structures (like variable product variations or YITH bundle's unique type). Here's the adjusted code that covers all three product types, plus explanations of the key changes:
Updated Code
// Allow backorders for admins on supported product types add_filter( 'woocommerce_product_backorders_allowed', 'custom_allow_backorders_for_admin_roles', 10, 3 ); function custom_allow_backorders_for_admin_roles( $backorder_allowed, $product_id, $product ){ // Only apply to administrators if ( current_user_can('administrator') ) { $product_type = $product->get_type(); // Enable backorders for Simple, Variable, and YITH Bundle products if ( in_array( $product_type, array( 'simple', 'variable', 'yith_bundle' ) ) ) { $backorder_allowed = true; } } else { // Disable backorders for all non-admin users $backorder_allowed = false; } return $backorder_allowed; } // Update stock status to "onbackorder" for admins on supported products add_filter( 'woocommerce_product_get_stock_status', 'custom_set_stock_status_for_admin', 10, 2 ); function custom_set_stock_status_for_admin( $stock_status, $product ) { if ( current_user_can('administrator') ) { $product_type = $product->get_type(); // Handle parent products (Simple, Variable, YITH Bundle) if ( in_array( $product_type, array( 'simple', 'variable', 'yith_bundle' ) ) ) { return 'onbackorder'; } // Handle Variable product variations (child products) elseif ( $product_type === 'variation' ) { $parent_product = wc_get_product( $product->get_parent_id() ); // Inherit backorder status from parent variable product if ( $parent_product ) { return 'onbackorder'; } } } else { // Force "outofstock" for non-admin users return 'outofstock'; } return $stock_status; }
Key Changes Explained
- Renamed Functions: I changed the function names to avoid conflicts with WooCommerce's native functions (your original names matched the filter hooks, which can cause unexpected behavior).
- Product Type Checks: Added explicit checks for
simple,variable, andyith_bundleproduct types to ensure the logic applies to all three. - Variable Product Variations: Variable products rely on child "variation" products, so we added a separate check to ensure those variations also show as backorderable for admins by inheriting the parent product's permission.
- YITH Bundle Support: YITH Bundle products use the
yith_bundleproduct type, so we included it in our allowed list to enable backorders for these products when viewed by admins.
Quick Note for YITH Bundles
If your YITH Bundle products use a stock management logic that depends on the bundled items, you might need to tweak this further—but based on your original requirement (enable backorders for admins regardless), this code should work as expected.
内容的提问来源于stack exchange,提问作者Vladimir Kyatipov
相关产品推荐
相关产品推荐

