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[] ?
$keyis just a variable.In the above code it just store the value in the$keyas an array form to return as result. When you try to print that$keyvariable it should look like the below:Or
Why are you not able to change the value of
$key[]='My Field'and$key='Keywords'because they are the methods(meansfunctions) 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.