ulc_automator_codes_backorder_allowed
Filters whether backorders are allowed for products when using the ULX Automator, allowing custom logic to control availability.
add_filter( 'ulc_automator_codes_backorder_allowed', $callback, 10, 1 );
Description
Filters whether backorders are allowed for automator codes. Developers can return `true` to permit backorders or `false` to disallow them. This filter fires when determining the backorder status for products associated with automator codes.
Usage
add_filter( 'ulc_automator_codes_backorder_allowed', 'your_function_name', 10, 1 );
Parameters
-
$this(mixed) - This parameter represents the default value for whether backorders are allowed, which can be modified by the filter.
Return Value
The filtered value.
Examples
/**
* Example: Conditionally allow backorders based on product stock quantity.
*
* This function hooks into 'ulc_automator_codes_backorder_allowed' to
* determine if backorders should be allowed for a product. In this example,
* we allow backorders only if the current stock quantity is less than 5.
*
* @param bool $allow_backorder The current backorder allowance status (default: false).
* @param WC_Product $product The WC_Product object instance.
*
* @return bool True if backorders are allowed, false otherwise.
*/
add_filter( 'ulc_automator_codes_backorder_allowed', function( $allow_backorder, $product ) {
// Check if the product object is valid and has stock data.
if ( ! $product instanceof WC_Product || ! $product->get_manage_stock() ) {
// If not managing stock, defer to the default or previous filter.
return $allow_backorder;
}
$stock_quantity = $product->get_stock_quantity();
// Allow backorders if stock quantity is less than 5.
if ( $stock_quantity < 5 ) {
return true;
}
// Otherwise, disallow backorders (or respect the previous filter).
return false;
}, 10, 2 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/includes/wc_product_automator_codes.php:121
public function backorders_allowed() {
return apply_filters( 'ulc_automator_codes_backorder_allowed', false, $this );
}