1
votes

This is related to : Replace product "on backorder" to a custom field value in Woocommerce

I would like to display the _backorder_text product custom field value in cart items that are backordered.

Based on Admin product pages custom field displayed in Cart and checkout, Here is the code that I have:

// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_item_data, $cart_item ) {
    if( isset( $cart_item['_backorder_text'] ) ) {
        $cart_item_data[] = array( 
            "name" => __( "Backorders text", "woocommerce" ), 
            "value" => $cart_item['_backorder_text'] 
        );
    }
    return $cart_item_data;
}

But it doesn't work.

Any help is appreciated.

1
Original part that this is an addon to is here (I posted over there originally, but told to make a new post): stackoverflow.com/questions/50267393/…Luke
Hi, thanks for that. However for somereason the text "available on backorder" text is appearing below the custom text. Backordered: Ready in 24 - 48 Hours* Available on backorderLuke

1 Answers

0
votes

Continuation of: Replace product "on backorder" to a custom field value in Woocommerce

To display the _backorder_text product custom field value in cart items (and in order items) that are backordered use the following:

// Display in cart items backorder text on cart and checkout pages
add_filter('woocommerce_get_item_data', 'display_custom_cart_item_data', 10, 2);
function display_custom_cart_item_data( $cart_item_data, $cart_item ) {
    if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
        $backorder_text = $cart_item['data']->get_meta('_backorder_text');
    }

    if( isset($backorder_text) && ! empty($backorder_text) ) {
        $cart_item_data[] = array(
            'name'  => __("Backordered", "woocommerce"),
            'value' => $backorder_text,
        );
    }

    return $cart_item_data;
}

// Order items: Save "backorder text" as order item meta data and display it everywhere
add_action('woocommerce_checkout_create_order_line_item', 'save_file_type_as_order_item_meta', 20, 4);
function save_file_type_as_order_item_meta($item, $cart_item_key, $values, $order) {
    if( $values['data']->is_on_backorder( $values['quantity'] ) ) {
        $backorder_text = $values['data']->get_meta('_backorder_text');
    }

    if( isset($backorder_text) && ! empty($backorder_text) ) {
        $item->update_meta_data( __("Backordered", "woocommerce"), $backorder_text );
    }
} 

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

enter image description here