添加额外的字段woocommerce结帐

时间:2019-05-29 09:21:08

标签: woocommerce checkout

im尝试使用

向woocommerce下订单按钮添加一个额外字段
/**
 * Add PO checkout field 
 */

add_action( 'woocommerce_review_order_before_payment', 'invoice_po' );
function invoice_po($checkout)
    { 
    woocommerce_form_field('invoice_po_field', array(
        'type' => 'text',
        'class' => array(
            'form-row-wide') ,
            'label' => __('Invoice Customer PO') ,
            'placeholder' => __('PO123456789') ,
            'required' => true,
    ) , $checkout->get_value('invoice_po_field'));
}

add_action('woocommerce_checkout_update_order_meta', 'add_po_to_order_meta');
function add_po_to_order_meta($order_id)
{
  if (!empty($_POST['invoice_po_field'])) {
    update_post_meta($order_id, 'Invoice Field', sanitize_text_field($_POST['invoice_po_field']));
  }
}

如果我想在订单注释之前或之后添加此方法,效果很好,但如果尝试在下订单按钮之前添加任何人都知道为什么我不能在此部分中添加字段,则此方法将不起作用

1 个答案:

答案 0 :(得分:0)

我的代码存在错误($ checkout),该变量仅与结帐页面的地址和客户注释部分相关,以允许访问我刚刚删除的所有页面挂钩

/**
 * Add PO checkout field 
 */
add_action( 'woocommerce_review_order_before_payment', 'invoice_po');
function invoice_po()
{
    $user = wp_get_current_user();
    if (in_array('invoice_customer', $user->roles)) {
        woocommerce_form_field('invoice_po_field', array(
            'type' => 'text',
            'class' => array('form-row-wide') ,
            'label' => __('Invoice Customer PO') ,
            'placeholder' => __('PO123456789') ,
            'required' => true
        ));
    }
}

add_action('woocommerce_checkout_update_order_meta', 'add_po_to_order_meta');
function add_po_to_order_meta($order_id)
{
  if (!empty($_POST['invoice_po_field'])) {
    update_post_meta($order_id, 'Invoice Field', sanitize_text_field($_POST['invoice_po_field']));
  }
}