2

woocommerce から入手したコードの解釈に疑問があります。コードは完全に機能していますが、一部を理解するのに問題があります。

以下はコードです:

* 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...') );
}


上記のコードは、電子メールの送信に関連しています。

私の疑問は次のとおりです。

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

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

の値として my_field_name , キーワードを指定できないのはなぜ$keys[]ですか?

何よりも、これら 3 つの関数は相互にある程度理解しています。$keys[] に「My Field」と「Keywords」を指定すると、電子メールのみが送信されます。それ以外の場合は、$keys[] に他の値を指定できないのはなぜですか?

4

1 に答える 1

1

$keyは単なる変数です。上記のコードでは、値を$key配列形式として格納し、結果として返します。その$key変数を印刷しようとすると、次のようになります。

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

または

ファイル内のどこかを定義するメソッド(手段)であるため、値$key[]='My Field'を変更できないのはなぜですか。それらを変更すると、これらの関数は実行されないため、送信できませんファイル内でこれらの関数を見つけてみてください。そうすれば、疑問が解消されます。$key='Keywords'functions

于 2012-12-04T08:35:16.467 に答える