2

Firstly, thanks for viewing this question. I've searched and gone through many similar questions however i've not managed to find a perfect fix.

Im setting up a website using wordpress/woocommerce, however most of our products have a set lead time therefore everything is on "back order - allow" status. Instead of showing "on backorder" on each product page, I wanted to see if it was possible to create a custom field in each product and replace the "on backorder" text to show that custom field.

Currently, i've been using the following code that just changes the text for every product however, not all products are on that specific lead time.

add_filter( 'woocommerce_get_availability', 'backorder_text', 10, 2);
function backorder_text($available) {
 return str_replace('Available on backorder', 'Approx lead time: 2-4 working weeks', $available);
}

I appreciate I would need to set up a custom field in each product with the set time, but i'm not entirely sure how to link that specfic custom field per each product to that php code (or rather, whether its actually possible).

Any help would be fantastic - even if its to tell me it can't be done!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Josh Dyas
  • 35
  • 1
  • 5
  • You can use ACF (Advanced Custom Fields) plugin for displaying text field on every product back-end and call that field using get_field('name'); in front-end. you can also modified that field according to your need. https://wordpress.org/plugins/acf-for-woocommerce-product/ , https://businessbloomer.com/woocommerce-display-advanced-custom-fields-single-product/ – Vaibhav Kumar May 10 '18 at 07:24
  • Thank you for your comment, I am installing ACF now. Do you have any idea what the amended php would be going from the one above? (I lack php knowledge, learning slowly) – Josh Dyas May 10 '18 at 08:14
  • I tried the two php codes that you provided but both producted fatal errors. I'll look into it more and see if I can find anything online as i'd love to get this working. Thanks for taking the time to look into it and assist though! – Josh Dyas May 10 '18 at 16:17

3 Answers3

4

This can be done with the following code, that will handle products and product variation too:

// Add a custom field in admin product edit pages - inventory tab
add_action( 'woocommerce_product_options_stock_fields', 'add_product_options_stock_custom_field', 20 );
function add_product_options_stock_custom_field() {
    global $product_object, $post;

    woocommerce_wp_text_input( array(
        'id'          => '_backorder_text',
        'type'        => 'text',
        'label'       => __( 'Backorders text', 'woocommerce' ),
        'description' => __( 'Backorders text. Add a custom backorders text to be displayed when products are on backorders.', 'woocommerce' ),
        'desc_tip'    => true,
    ) );

    // jQuery: HIDE the fied if backorders are not enabled
    ?>
    <script type="text/javascript">
    jQuery( function($){
        var a = 'select#_backorders',
            b = 'p._backorder_text_field';

        if( $(a).val() === 'no' )
            $(b).hide();

        $(a).on('change blur', function(){
            if( $(a).val() === 'no' )
                $(b).hide();
            else
                $(b).show();
        });
    });
    </script>
    <?php
}

// Save the custom field value from admin product edit pages - inventory tab
add_action( 'woocommerce_process_product_meta', 'save_product_options_stock_custom_field', 20, 1 );
function save_product_options_stock_custom_field( $product_id ) {
    if ( isset( $_POST['_backorder_text'] ) )
        update_post_meta( $product_id, '_backorder_text', sanitize_text_field( $_POST['_backorder_text'] ) );
}

// Variations: Add a custom field in admin variation options inventory
add_action( 'woocommerce_variation_options_inventory', 'add_variation_settings_fields', 20, 3 );
function add_variation_settings_fields( $loop, $variation_data, $variation_post ) {

    woocommerce_wp_text_input( array(
        'id'            => '_backorder_text'.$loop,
        'name'          => '_backorder_text['.$loop.']',
        'value'         => get_post_meta( $variation_post->ID, '_backorder_text', true ),
        'type'          => 'text',
        'label'         => __( 'Backorders text', 'woocommerce' ),
        'description'   => __( 'Backorders text. Add a custom backorders text to be displayed when products are on backorders.', 'woocommerce' ),
        'desc_tip'      => true,
        'wrapper_class' => 'form-row form-row-first',
    ) );
}

// Variations: Save a custom field value from admin variation options inventory
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function save_variation_settings_fields( $variation_id, $i ) {
    if( isset( $_POST['_backorder_text'][$i] ) )
        update_post_meta( $variation_id, '_backorder_text', sanitize_text_field( $_POST['_backorder_text'][$i] ) );
}

add_filter( 'woocommerce_get_availability', 'custom_on_backorder_text', 10, 2 );
function custom_on_backorder_text( $availability, $product ) {
    $backorder_text = get_post_meta( $product->get_id(), '_backorder_text', true );

    if( $availability['class'] === 'available-on-backorder' && ! empty( $backorder_text ) )
        $availability['availability'] = $backorder_text;

    return $availability;
}

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

For all products (except variable products, see after) you will get:

enter image description here

enter image description here

For product variations (of a variable product):

enter image description here

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hello there, even though you have already answered my question and I am extremely grateful. I wanted to ask whether its possible to make the above code also valid for the "cart/checkout page" also. – Josh Dyas May 13 '18 at 11:51
  • 1
    I have missed this comment, sorry. If you still need that, just re ask I will give you some answers links, that will allow to make it… You can also make a new question if you like. As you have gain the capability, if you like/want you could please upvote this answer. Thanks. – LoicTheAztec Jul 04 '18 at 15:56
0

The code works perfectly for simple products. But for the variations the back order text is available but it does not display on the product page when a customer makes the selection

  • 1
    So your custom text doesn't display but does the default text still display? – Nat Oct 31 '20 at 14:12
  • Sorry for the delay in communications. So if I set a message on the simple product to say Available for back order. Turn around 1 week. If the user/customer selects that simple product the message appears. But if you set the same massage on a variable product and the customers selects it from a drop down menu. The message does not appear. Hope that makes sense – Nikhil Puthu Nov 02 '20 at 11:32
  • Yes but when they select the variable product do you get the default text? – Nat Nov 04 '20 at 06:12
  • They do not get default text if they select the variable product – Nikhil Puthu Nov 05 '20 at 07:25
  • OK - then remove all your custom code and see if they get the default text when they select the variable product. If not, then that is your problem. It will be some setting in Wordpress – Nat Nov 05 '20 at 13:00
  • Thanks. Will do. Thank you for all your help – Nikhil Puthu Nov 06 '20 at 14:35
0

I modified @LoicTheAztec code slightly to use this for displaying the backorder text on the front end.

add_filter( 'woocommerce_get_availability_text', 'customize_availability_text', 10, 2);
function customize_availability_text( $availability, $product ) {
$backorder_text = get_post_meta( $product->get_id(), '_backorder_text', true );
 if (! empty( $backorder_text )){$availability = str_replace('Available on backorder',  $backorder_text , $availability);}
return $availability;
}

Note that I'm using the

woocommerce_get_availability_text

filter rather than the

woocommerce_get_availability

filter

This worked with my products with variations, not tested on simple products

Nat
  • 621
  • 1
  • 6
  • 17