• Resolved clarru

    (@clarru)


    Hello, I want to be able to manually add a shipping link from the order page like I did in this example with google.com and eventually this field be rendered in an href tag in the Email Template.

    I’ve added a custom snippet below which creates the Shipping link input you can see above

    // Add a custom field to the WooCommerce order edit page
    add_action( 'woocommerce_admin_order_data_after_order_details', 'add_shipping_link_metafield' );
    function add_shipping_link_metafield( $order ) {
        $shipping_link = get_post_meta( $order->get_id(), '_shipping_link', true );
        ?>
        <div class="order_data_column">
            <h4><?php _e( 'Shipping Link', 'textdomain' ); ?></h4>
            <input type="text" name="shipping_link" value="<?php echo esc_attr( $shipping_link ); ?>">
        </div>
        <?php
    }
    
    // Save the custom field value
    add_action( 'woocommerce_process_shop_order_meta', 'save_shipping_link_metafield' );
    function save_shipping_link_metafield( $post_id ) {
        $shipping_link = isset( $_POST['shipping_link'] ) ? sanitize_text_field( $_POST['shipping_link'] ) : '';
        update_post_meta( $post_id, '_shipping_link', $shipping_link );
    }

    The problem is the Plugin is not rendering the custom field at all

    Here’s what I added in the Template Editor

    How can I display my custom field here?

    • This topic was modified 6 months, 1 week ago by clarru.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support WebToffee Support

    (@webtoffeesupport)

    Hi @clarru ,

    Greetings from WebToffee Support!

    Thank you for reaching out to us regarding the requirement you are seeking. To ensure that your custom field is displayed in the order emails, you can utilize the provided code snippet. You can copy the code snippet provided below and paste it into your active theme’s functions.php file (WordPress Dashboard > Appearance > Theme Editor > functions.php), or you can use a Code Snippet plugin to do that for you.

    add_filter('wt_woomail_order_body_text','wbte_dec_add_custom_placeholder_to_order_emails',10,5);
    function wbte_dec_add_custom_placeholder_to_order_emails($body_text, $order, $sent_to_admin, $plain_text, $email) {
    	if ( is_a( $email->object, 'WC_Order' ) && !empty($order) ) {
        
    		$shipping_link = $order->get_meta( '_shipping_link', true );
    
            if ( ! empty( $shipping_link ) && strpos( $body_text, '{_shipping_link}' ) !== false ) {
                $body_text = str_replace( '{_shipping_link}', $shipping_link, $body_text );
            }
    	}
    	return $body_text;
    }

    This code snippet will replace the {_shipping_link} placeholder in your order emails with the value of the _shipping_link custom field from your WooCommerce orders.

    Feel free to reach out if you need any further clarification or assistance. We’re here to help!

    Thread Starter clarru

    (@clarru)

    Thank you! Works perfectly!

    Now I have another problem with the email rendering but I’ll create a sepparate ticket!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Render custom fields in the Email Template’ is closed to new replies.