Add a custom dynamic placeholder to email subject in Woocommerce












1















In Woocommerce, I have added a custom field to the checkout page using the following code:



add_action( 'woocommerce_checkout_fields', 'woo_add_conditional_checkout_fields' );
function woo_add_conditional_checkout_fields( $fields ) {
foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];
$fields['billing']['billing_field_newfield'] = array(
'label' => __('New Field', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => false
);
}
return $fields;
}


Now, I would like to include the value that user enters in this field in the 'New Order' woocommerce email subject using {billing_field_newfield} custom placeholder.



However when I go to Woocommerce > Settings > Emails > New Order and put {billing_field_newfield} in the subject, I just get {billing_field_newfield} in the email and not its actual value.



How to add a custom dynamic placeholder to email subject in Woocommerce?










share|improve this question





























    1















    In Woocommerce, I have added a custom field to the checkout page using the following code:



    add_action( 'woocommerce_checkout_fields', 'woo_add_conditional_checkout_fields' );
    function woo_add_conditional_checkout_fields( $fields ) {
    foreach( WC()->cart->get_cart() as $cart_item ){
    $product_id = $cart_item['product_id'];
    $fields['billing']['billing_field_newfield'] = array(
    'label' => __('New Field', 'woocommerce'),
    'placeholder' => _x('', 'placeholder', 'woocommerce'),
    'required' => true,
    'class' => array('form-row-wide'),
    'clear' => false
    );
    }
    return $fields;
    }


    Now, I would like to include the value that user enters in this field in the 'New Order' woocommerce email subject using {billing_field_newfield} custom placeholder.



    However when I go to Woocommerce > Settings > Emails > New Order and put {billing_field_newfield} in the subject, I just get {billing_field_newfield} in the email and not its actual value.



    How to add a custom dynamic placeholder to email subject in Woocommerce?










    share|improve this question



























      1












      1








      1








      In Woocommerce, I have added a custom field to the checkout page using the following code:



      add_action( 'woocommerce_checkout_fields', 'woo_add_conditional_checkout_fields' );
      function woo_add_conditional_checkout_fields( $fields ) {
      foreach( WC()->cart->get_cart() as $cart_item ){
      $product_id = $cart_item['product_id'];
      $fields['billing']['billing_field_newfield'] = array(
      'label' => __('New Field', 'woocommerce'),
      'placeholder' => _x('', 'placeholder', 'woocommerce'),
      'required' => true,
      'class' => array('form-row-wide'),
      'clear' => false
      );
      }
      return $fields;
      }


      Now, I would like to include the value that user enters in this field in the 'New Order' woocommerce email subject using {billing_field_newfield} custom placeholder.



      However when I go to Woocommerce > Settings > Emails > New Order and put {billing_field_newfield} in the subject, I just get {billing_field_newfield} in the email and not its actual value.



      How to add a custom dynamic placeholder to email subject in Woocommerce?










      share|improve this question
















      In Woocommerce, I have added a custom field to the checkout page using the following code:



      add_action( 'woocommerce_checkout_fields', 'woo_add_conditional_checkout_fields' );
      function woo_add_conditional_checkout_fields( $fields ) {
      foreach( WC()->cart->get_cart() as $cart_item ){
      $product_id = $cart_item['product_id'];
      $fields['billing']['billing_field_newfield'] = array(
      'label' => __('New Field', 'woocommerce'),
      'placeholder' => _x('', 'placeholder', 'woocommerce'),
      'required' => true,
      'class' => array('form-row-wide'),
      'clear' => false
      );
      }
      return $fields;
      }


      Now, I would like to include the value that user enters in this field in the 'New Order' woocommerce email subject using {billing_field_newfield} custom placeholder.



      However when I go to Woocommerce > Settings > Emails > New Order and put {billing_field_newfield} in the subject, I just get {billing_field_newfield} in the email and not its actual value.



      How to add a custom dynamic placeholder to email subject in Woocommerce?







      php wordpress woocommerce placeholder email-notifications






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 10:29









      LoicTheAztec

      96.2k1371113




      96.2k1371113










      asked Jan 3 at 1:36









      Bradley JoeBradley Joe

      152




      152
























          1 Answer
          1






          active

          oldest

          votes


















          0














          To add a custom active placeholder {billing_field_newfield} in woocommerce email notifications, you will use the following hooked function.



          You will check before that _billing_field_newfield is the correct post meta key used to save the checkout field value to the order (check in wp_postmeta database table for the order post_id).



          The code:



          add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 20, 2 );
          function add_custom_email_format_string( $string, $email ) {
          // The post meta key used to save the value in the order post meta data
          $meta_key = '_billing_field_newfield';

          // Get the instance of the WC_Order object
          $order = $email->object;

          // Get the value
          $value = $order->get_meta($meta_key) ? $order->get_meta($meta_key) : '';

          // Additional subject placeholder
          $new_placeholders = array( '{billing_field_newfield}' => $value );

          // Return the clean replacement value string for "{billing_field_newfield}" placeholder
          return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
          }


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



          Then in Woocommerce > Settings > Emails > "New Order" notification, you will be able to use the dynamic placeholder {billing_field_newfield}






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015310%2fadd-a-custom-dynamic-placeholder-to-email-subject-in-woocommerce%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            To add a custom active placeholder {billing_field_newfield} in woocommerce email notifications, you will use the following hooked function.



            You will check before that _billing_field_newfield is the correct post meta key used to save the checkout field value to the order (check in wp_postmeta database table for the order post_id).



            The code:



            add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 20, 2 );
            function add_custom_email_format_string( $string, $email ) {
            // The post meta key used to save the value in the order post meta data
            $meta_key = '_billing_field_newfield';

            // Get the instance of the WC_Order object
            $order = $email->object;

            // Get the value
            $value = $order->get_meta($meta_key) ? $order->get_meta($meta_key) : '';

            // Additional subject placeholder
            $new_placeholders = array( '{billing_field_newfield}' => $value );

            // Return the clean replacement value string for "{billing_field_newfield}" placeholder
            return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
            }


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



            Then in Woocommerce > Settings > Emails > "New Order" notification, you will be able to use the dynamic placeholder {billing_field_newfield}






            share|improve this answer




























              0














              To add a custom active placeholder {billing_field_newfield} in woocommerce email notifications, you will use the following hooked function.



              You will check before that _billing_field_newfield is the correct post meta key used to save the checkout field value to the order (check in wp_postmeta database table for the order post_id).



              The code:



              add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 20, 2 );
              function add_custom_email_format_string( $string, $email ) {
              // The post meta key used to save the value in the order post meta data
              $meta_key = '_billing_field_newfield';

              // Get the instance of the WC_Order object
              $order = $email->object;

              // Get the value
              $value = $order->get_meta($meta_key) ? $order->get_meta($meta_key) : '';

              // Additional subject placeholder
              $new_placeholders = array( '{billing_field_newfield}' => $value );

              // Return the clean replacement value string for "{billing_field_newfield}" placeholder
              return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
              }


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



              Then in Woocommerce > Settings > Emails > "New Order" notification, you will be able to use the dynamic placeholder {billing_field_newfield}






              share|improve this answer


























                0












                0








                0







                To add a custom active placeholder {billing_field_newfield} in woocommerce email notifications, you will use the following hooked function.



                You will check before that _billing_field_newfield is the correct post meta key used to save the checkout field value to the order (check in wp_postmeta database table for the order post_id).



                The code:



                add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 20, 2 );
                function add_custom_email_format_string( $string, $email ) {
                // The post meta key used to save the value in the order post meta data
                $meta_key = '_billing_field_newfield';

                // Get the instance of the WC_Order object
                $order = $email->object;

                // Get the value
                $value = $order->get_meta($meta_key) ? $order->get_meta($meta_key) : '';

                // Additional subject placeholder
                $new_placeholders = array( '{billing_field_newfield}' => $value );

                // Return the clean replacement value string for "{billing_field_newfield}" placeholder
                return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
                }


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



                Then in Woocommerce > Settings > Emails > "New Order" notification, you will be able to use the dynamic placeholder {billing_field_newfield}






                share|improve this answer













                To add a custom active placeholder {billing_field_newfield} in woocommerce email notifications, you will use the following hooked function.



                You will check before that _billing_field_newfield is the correct post meta key used to save the checkout field value to the order (check in wp_postmeta database table for the order post_id).



                The code:



                add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 20, 2 );
                function add_custom_email_format_string( $string, $email ) {
                // The post meta key used to save the value in the order post meta data
                $meta_key = '_billing_field_newfield';

                // Get the instance of the WC_Order object
                $order = $email->object;

                // Get the value
                $value = $order->get_meta($meta_key) ? $order->get_meta($meta_key) : '';

                // Additional subject placeholder
                $new_placeholders = array( '{billing_field_newfield}' => $value );

                // Return the clean replacement value string for "{billing_field_newfield}" placeholder
                return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
                }


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



                Then in Woocommerce > Settings > Emails > "New Order" notification, you will be able to use the dynamic placeholder {billing_field_newfield}







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 10:19









                LoicTheAztecLoicTheAztec

                96.2k1371113




                96.2k1371113
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015310%2fadd-a-custom-dynamic-placeholder-to-email-subject-in-woocommerce%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                    Npm cannot find a required file even through it is in the searched directory