Search code examples
phpwordpresswoocommercemetadataemail-notifications

Display order item estimated delivery date if product has specific custom field in WooCommerce


I'm adding programmatically a product custom field (the estimated delivery date) as custom cart item data if the product is ordered from backorder.

I want to display that information also on order items so it will be displayed on orders and email notifications sent to customer.

Here's what i tried so far :

add_filter( 'woocommerce_get_item_data', 'display_acf_on_cart_and_checkout', 10, 2 );
function display_acf_on_cart_and_checkout( $cart_data, $cart_item ) {
    if ( ($cart_item['variation_id'] > 0 && $cart_item['data']->get_stock_quantity() <= 0
    && $cart_item['data']->get_meta('_ab_preorder_checkbox') === 'yes') || isset( $cart_item['is_preorder_field'] ) ) {
        if ( $estimated_delivery_date = $cart_item['data']->get_meta('_ab_preorder_estimated_date') ) {
            $custom_items[] = array( "name" => __("Date de livraison estimée (précommande) ", "woocommerce"),  "value" => $estimated_delivery_date );
        }
    }
    return $custom_items;
}

The message is showing on the cart and on the checkout page :

enter image description here

enter image description here

But not on the invoice/emails sent to the client :

enter image description here

I would like it to show up on like on the screenshot below (in the red rectangle). Is it possible?

Any help is appreciated.


Solution

  • You can use the following to save the estimated delivery date for preordered items as custom order item meta data and display it on orders and email notifications:

    // Save as custom order item meta data and display on orders and email notifications
    add_action('woocommerce_checkout_create_order_line_item', 'action_checkout_create_order_line_item', 10, 4 );
    function action_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
        if ( ($values['variation_id'] > 0 && $values['data']->get_stock_quantity() <= 0
        && $values['data']->get_meta('_ab_preorder_checkbox') === 'yes') || isset( $values['is_preorder_field'] ) ) {
            if ( $estimated_delivery_date = $values['data']->get_meta('_ab_preorder_estimated_date') ) {
                $item->update_meta_data( 'is_preorder', $estimated_delivery_date );
            }
        }
    }
    
    // Change required displayed meta key label to something readable
    add_filter('woocommerce_order_item_display_meta_key', 'filter_order_item_displayed_meta_key', 20, 3 );
    function filter_order_item_displayed_meta_key( $displayed_key, $meta, $item ) {
        // Change displayed meta key label for specific order item meta key
        if( $item->get_type() === 'line_item' && $meta->key === 'is_preorder' ) {
            $displayed_key = __("Date de livraison estimée (précommande) ", "woocommerce");
        }
        return $displayed_key;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should work.