16

私はウェブショップに取り組んでおり、このチュートリアルhttp://wcdocs.woothemes.com/snippets/tutorial-customising-checkout-fields-using-hooks-and-filters/に従って、請求書にいくつかのカスタム フィールドを追加しています。

// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
 $fields['billing']['billing_gls_name'] = array(
    'label'     => __('Name for pickup person', 'woocommerce'),
'placeholder'   => _x('Name', 'placeholder', 'woocommerce'),
'required'  => true,
'class'     => array('form-row-wide'),
'clear'     => true
 );

 return $fields;
}

これにより、私のフィールドが追加されます。ここまでは順調ですね。だから私の問題は:

注文ビューでこの新しいフィールドを表示するにはどうすればよいですか? 請求の詳細には、通常の請求フィールドのみが表示されます。

4

3 に答える 3

16

The first answer (Cesar) was CLOSE to being correct. In case anyone ever comes across this old post trying to find out the same thing, below is the code needed to insert into your functions.php file after the code given by the original poster, tailored to his/her variables as provided. Note that they use the field name "billing_gls_name" and that this is referenced in our new function as "_billing_gls_name". The extra "_" at the beginning is necessary. This works on Wordpress 3.5.1 running WooCommerce 2.0.3.

function your_custom_field_function_name($order){
    echo "<p><strong>Name of pickup person:</strong> " . $order->order_custom_fields['_billing_gls_name'][0] . "</p>";
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'your_custom_field_function_name', 10, 1 );
于 2013-03-21T15:06:01.483 に答える
9

カスタム フィールドを定義した後 (上記のコードで行った)、以下のコードを以下に追加します。

  1. フィールドを処理する

  2. 注文メタデータとしてデータベースに保存

  3. Woocommerce->注文セクションの「注文の詳細」に表示します

フィールドを処理します。

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {

if (!$_POST['billing']['billing_gls_name']) {
    wc_add_notice(__('Please tell us the Location Type that we are billing to.'), 'error');
}

}

フィールドを注文メタデータとして DB に保存します。

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 (!empty($_POST['billing']['billing_gls_name'])) {
     update_post_meta($order_id, 'Billing Gls Name', esc_attr($_POST['billing']['billing_gls_name_type']));
     }
 }

最後に、注文の詳細画面に表示します。

  add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_billing_fields_display_admin_order_meta', 10, 1);

  function my_custom_billing_fields_display_admin_order_meta($order) {
echo '<p><strong>' . __('Billing Gls Name') . ':</strong><br> ' . get_post_meta($order->id, '_billing_gls_name', true) . '</p>';
}
于 2014-09-02T13:22:53.903 に答える
3

woocommerce_admin_order_data_after_billing_address請求情報の後にデータを挿入できるアクションを追加します。カスタム フィールドは$order->order_custom_fields配列の下にあります。

function display_rfc_in_order_metabox($order){
    echo "<p><strong>RFC:</strong> {$order->order_custom_fields['_billing_rfc'][0]}</p>";
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'rxm_details_to_order', 10, 1 );
于 2013-01-17T18:03:13.207 に答える