Search code examples
wordpressemailwoocommercehook-woocommerceshortcode

Add custom field to the custom Woocommerce Emails


I want to add delivery date in woocommerce emails, the hook I am using is

add_action( 'woocommerce_order_details_after_order_table', 'action_woocommerce_order_details_after_order_table', 10, 1 );

function action_woocommerce_order_details_after_order_table( $order ) {
...
$order_expected_delivery_date = get_post_meta($order_id, 'order_expected_delivery_date_'.$order_id, true);

if(!$order_expected_delivery_date) {
    $delivery_date = getExpectedDeliveryDate($order);
    update_post_meta($order_id, 'order_expected_delivery_date_'.$order_id, $delivery_date);
    echo "<header><h2>Expected Delivery Date</h2></header><p style='font-size: 20px;'>".$delivery_date."<p>";
} else {
    echo "<header><h2>Expected Delivery Date</h2></header><p style='font-size: 20px;'>".$order_expected_delivery_date."<p>";
}
}

but this hook is only called when the woocommerce order emails are triggered. I have made a custom email for sending to the warehouse as per requirement. This hook is not working for the custom emails.

I have tried to add the shortcode to the custom emails but that short code is also not working.

function delivery_date_shortcode( $atts, $content = null ) {
if( is_numeric($content) ) {
    $order = wc_get_order( $content );

    $order_data = $order->get_data();

    return '<span class="caption">' . $order_data . '</span>';
}
}
add_shortcode( 'delivery', 'delivery_date_shortcode' );

Solution

  • I forgot to tell you that this issue is resolved.

    // shortcode to display expected delivery date in warehouse email
    function delivery_date_shortcode( $atts, $content = null ) {
    
    $order = new \WC_Order($content);
    $delivery_date = getExpectedDeliveryDate($order);
    
    return "<header><h2 style='margin: 40px 0 18px;'>Expected Delivery Date</h2></header><p style='font-size: 20px;'>".$delivery_date."<p>";
    
    }
    add_shortcode( 'delivery', 'delivery_date_shortcode' );
    

    The function getExpectedDeliveryDate($order) takes the order id and returns the expected delivery date. Thank You.