Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8946439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:30:55+00:00 2026-06-15T12:30:55+00:00

I have a doubt on interpreting a piece of code I got from woocommerce.

  • 0

I have a doubt on interpreting a piece of code I got from woocommerce. The code is working perfectly, BUT I have a problem understanding some part of it.

Below is code:

* Adding the Custom field to the checkout
 **/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

global $woocommerce;
$found = false;
//check if product already in cart

    if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {

            foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {               

                $_product = $values['data'];

                if ( $_product->id == 209) {

                    echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
                    woocommerce_form_field( 'my_field_name', array(
                        'type'          => 'checkbox',
                        'class'         => array('my-field-class form-row-wide'),
                        'label'         => __('Fill in this field'),
                        'placeholder'   => __('Enter a number'),
                        ), $checkout->get_value( 'my_field_name' ));

                    echo '</div>';

                  echo '<div id="my_custom_checkout_field2"><h3>'.__('Keywords').'</h3>';

                    woocommerce_form_field( 'enter_keywords', array(
                    'type'          => 'text',
                    'required'  => true,
                    'class'         => array('my-field-class form-row-wide'),
                    'label'         => __('Enter Keywords'),
                    'placeholder'       => __('Enter something'),
                    ), $checkout->get_value( 'keywords' ));

                    echo '</div>';
                }

            }

        }

}




/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));

        if ($_POST['keywords']) update_post_meta( $order_id, 'Keywords', esc_attr($_POST['keywords']));
}


/**
 * Add the field to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');

function my_custom_checkout_field_order_meta_keys( $keys ) {
    $keys[] = 'My Field';
    $keys[] = 'Keywords';
    return $keys;
}



/**
 * Process the checkout
 **/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    global $woocommerce;

    // Check if set, if its not set add an error.
    if (!$_POST['enter_keywords'])
         $woocommerce->add_error( __('Please enter keywords...') );
}

The above code is related to sending email.

My Doubt is:

Why should I give values for $keys[] as:

$keys[] = 'My Field';
$keys[] = 'Keywords';

Why cannot I give my_field_name , keywords as a values for $keys[]?

Above all these 3 functions have some understanding each other, if i give as ‘My Field’ and ‘Keywords’ for $keys[], then only iam getting an email, otherwise NOT, so why cannot give other values for $keys[] ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:31:00+00:00Added an answer on June 15, 2026 at 12:31 pm

    $key is just a variable.In the above code it just store the value in the $key as an array form to return as result. When you try to print that $key variable it should look like the below:

    Array(
        [0] => My Field
        [1] => Keywords
    )
    

    Or

    Why are you not able to change the value of $key[]='My Field' and $key='Keywords' because they are the methods(means functions) which is define some where in your file.If you change them ,then these function will not get executed that’s why you are not be able to send the email.Try to find these functions in your files then you will have your doubt clear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some doubt on below code #include<stdio.h> int i=6; int main() { int
I have doubt and need some suggestion on following code, NSArray* onscreenwindows = (NSArray*)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly,
I'm very new to IOS programming and i have doubt with NSThread. My problem
is it possible to execute same function concurrently from thread. I have doubt that
I have a doubt with enums if I defined a enums that inherited from
I have doubt in retrieving multiple records from DB. Case 1: Apply join, select
I have doubt about data structure transformation from one form of array to another.
I have done MySQL master1-master2 replication in windows,is work nicely. but i have doubt
This is the text field delegate method but i have doubt about return type
I know how the Singleton pattern works, but I have doubt how it works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.