Home > database >  Custom stock for variable products WooCommerce
Custom stock for variable products WooCommerce

Time:01-26

I tried this function to make a custom stock status for variable products in WooCommerce but doesn`t work

add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);
function custom_get_availability( $availability, $product ) {
    // Only for variable products
    if ( $product->is_type( 'variable' ) ) {
        // Get the stock status of the product
        $stock_status = $product->get_stock_status();
 
        // Custom stock status
        if( $stock_status == 'outofstock' ) {
            $availability['availability'] = __('Custom Out of Stock Message', 'woocommerce');
        }
    }
    return $availability;
}

CodePudding user response:

add_filter('woocommerce_out_of_stock_message', 'change_variable_out_of_stock_availability', 10, 1);

function change_variable_out_of_stock_availability($out_of_stock_message) {
    return __('Custom Out of Stock Message', 'woocommerce');
}

WooCommerce has the template for variable product single-product/add-to-cart/variable.php.This template can be overridden by copying it to yourtheme/woocommerce/single-product/add-to-cart/variable.php. Or this message can be altered with the dedicated hook (woocommerce_out_of_stock_message) for the variable product out-of-stock message

CodePudding user response:

you can try this :

add_filter( 'woocommerce_variation_is_in_stock', 'custom_variation_stock_status', 10, 2 );
function custom_variation_stock_status( $is_in_stock, $variation ) {
    if ( ! $is_in_stock ) {
        // Custom stock status
        return 'Custom Out of Stock Message';
    }
    return $is_in_stock;
}
  • Related