首页 文章

在woocommerce中订购自定义字段

提问于
浏览
13

我在网上商店工作,然后按照本教程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;
}

这增加了我的领域 . 到现在为止还挺好 . 所以我的问题是:

如何在订单视图中查看此新字段?计费详细信息仅显示常用的结算字段 .

3 回答

  • 12

    第一个答案(塞萨尔)关闭是正确的 . 如果有人遇到这个老帖子试图找出相同的东西,下面是在原始海报给出的代码之后插入到functions.php文件中所需的代码,根据他/她提供的变量量身定制 . 请注意,它们使用字段名称“billing_gls_name”,并在我们的新函数中将其引用为“billing_gls_name” . 开头的额外“”是必要的 . 这适用于运行WooCommerce 2.0.3的Wordpress 3.5.1 .

    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 );
    
  • 7

    在定义自定义字段(您在上面提到的代码中所做的)后,将下面提到的代码添加到:

    • 处理你的领域

    • 将其作为订单元数据保存在数据库中

    • 在Woocommerce-> Orders部分的“订单详细信息”中显示

    Process your field:

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

    Save Field in the DB as Order Meta Data:

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

    And finally display it in the Order details screen:

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

相关问题