• Resolved jamesscaife

    (@jamesscaife)


    Hi Guys

    DESPERATLEY need advice, I’m tearing my hair out on this one!

    I want to change the ‘available to back order’ text that displays for products on back order on the product pages to display my ‘Lead Time’ custom Field.

    I’ve done this:

    function backorder_text($available) {
    
    foreach($available as $i) {
    
    $available = str_replace('Available on backorder', '<div class="prod-code pc-leadtime">Lead Time: ', $available);
    
    }
    
    return $available;
    }
    add_filter('woocommerce_get_availability', 'backorder_text');
    
    But unfortunatley it only displays the custom fields title and not the content, which in this case is “Made for you in just eight weeks”

    Look forward to hearing your thoughts ??

    KR

    James

    https://www.ads-software.com/plugins/woocommerce/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hey James,

    The below should work fine – you just need to replace your_custom_meta_key with whatever the name of your custom field is. I’d guess something like lead_time in your case.

    Hope that makes sense,
    Cheers

    function custom_backorder_text( $available_array, $product_object ) {
    
    	if( $available_array["availability"] == "Available on backorder" ){
    
    		$custom_meta_value = get_post_meta( $product_object->id, 'your_custom_meta_key', true );
    
    		$available_array["availability"] = "Lead Time: ".$custom_meta_value;
    
    		$available_array["class"] = "prod-code pc-leadtime";
    
    		return $available_array;
    
    	}
    
    }
    
    add_filter( 'woocommerce_get_availability', 'custom_backorder_text', 99, 2);
    Thread Starter jamesscaife

    (@jamesscaife)

    Thank you so much Steve for the response

    Unfortunatley this only displays the text Lead Time, it doesn’t display the custom field data, i.e.

    ‘Lead Time: Made For You In Just Eight Weeks’

    instead of…

    ‘Lead Time:’

    What do you think?

    BTW I posted the your code in functions.php file, this is correct right?

    robingb

    (@robingb)

    Hi James,

    Did you solve this problem, I am trying to do the same thing.

    Your help please.

    robingb

    (@robingb)

    To help others, I wanted to vary the text displayed at product level, some products take longer than others to produce. So in the woocommerce product page I created a custom field called “Out_of_stock_message” and in it I put like “Lead time is 2 weeks”.

    Then in my child themes functions.php file I added this…

    add_filter( 'woocommerce_get_availability' , 'revised_woocommerce_get_availability' , 10, 2 );
    
    function revised_woocommerce_get_availability( $available_array , $product) {
    
    if ( $product->managing_stock() ) {
    
      if ( !($product->is_in_stock() && $product->get_stock_quantity() > get_option( 'woocommerce_notify_no_stock_amount' ))  && ($product->backorders_allowed() && $product->backorders_require_notification())  ) {
    
      $custom_meta_value = get_post_meta( $product->id, 'Out_of_stock_message', true );
    	$available_array["availability"] = $custom_meta_value;
       }
    }
    
        return $available_array;
    }

    This only affects the product page, not the cart/basket page. Hope that helps someone.

    Thread Starter jamesscaife

    (@jamesscaife)

    Hi robingb

    Sorry just seen this, great!

    Does it also display as ‘in stock’ when stocked?

    I solved this a while ago but when the product is in stock it doesn’t display as ‘in stock’ – instead nothing appears.

    Check this out…

    https://olsonbaker.com/product/tools-frying-pan/

    The 28cm size is in stock and the 24 out of stock.

    Thanks for your help so far, really appreciate it!
    ??

    KR

    James

    Hi James,

    Yes it displays “In Stock”, see https://www.hart-expert.co.uk/online-shop, currently everything is in stock.

    Regards,

    Robin

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘PLEASE HELP!!! Adding Custom Field to 'available on back order' message…’ is closed to new replies.