Woocommerce重新安排结帐领域

时间:2016-08-23 17:31:43

标签: php wordpress woocommerce

我已使用此功能重新排列了Woocommerce结帐页面上的订单字段:

    //Reorder checkout
    add_filter( 'woocommerce_checkout_fields', 'reorder_woo_fields' ); 
    function reorder_woo_fields( $fields ) {
    $fields2['billing']['billing_first_name'] = $fields['billing']  ['billing_first_name'];
    $fields2['billing']['billing_last_name']  = $fields['billing']['billing_last_name'];
    $fields2['billing']['billing_company'] =  $fields['billing']['billing_company'];
    $fields2['billing']['billing_address_1']  = $fields['billing']['billing_address_1'];
    $fields2['billing']['billing_city']       = $fields['billing']['billing_city'];
    $fields2['billing']['billing_postcode']   = $fields['billing']['billing_postcode'];
    $fields2['billing']['billing_country']    = $fields['billing']['billing_country'];
    $fields2['billing']['billing_email']      = $fields['billing']['billing_email'];
    $fields2['billing']['billing_phone']      = $fields['billing']['billing_phone'];

    $fields2['shipping']['shipping_first_name'] = $fields['shipping']['shipping_first_name'];
    $fields2['shipping']['shipping_last_name'] = $fields['shipping']['shipping_last_name'];
    $fields2['shipping']['shipping_company'] =  $fields['shipping']['shipping_company'];
    $fields2['shipping']['shipping_address_1'] = $fields['shipping']['shipping_address_1'];
    $fields2['shipping']['shipping_city'] = $fields['shipping']['shipping_city'];
    $fields2['shipping']['shipping_postcode'] = $fields['shipping']['shipping_postcode'];
    $fields2['shipping']['shipping_country'] = $fields['shipping']['shipping_country'];

    // Add full width Classes and Clears to Adjustments    
    $fields2['billing']['billing_first_name'] = array(
        'label' => __('First Name', 'woocommerce'),
    'class'     => array('form-row-wide'),
    'clear'     => true,
    'required' => true
     );
     $fields2['billing']['billing_last_name'] = array(
    'label' => __('Last Name', 'woocommerce'),
    'class'     => array('form-row-wide'),
    'clear'     => true,
        'required' => true
     );
     $fields2['shipping']['shipping_first_name'] = array(
        'label' => __('First Name', 'woocommerce'),
    'class'     => array('form-row-wide'),
    'clear'     => true,
    'required' => true
     );
     $fields2['shipping']['shipping_last_name'] = array(
    'label' => __('Last Name', 'woocommerce'),
    'class'     => array('form-row-wide'),
    'clear'     => true,
        'required' => true
     );
     return $fields2;
     }

这是有效的,但是当启用调试模式时,我在chekcout页面上收到错误: 注意:未定义的索引:第58行的/html/wordpress/wp-content/plugins/woocommerce/templates/checkout/form-shipping.php中的订单

警告:第58行/html/wordpress/wp-content/plugins/woocommerce/templates/checkout/form-shipping.php为foreach()提供的参数无效

aynone可以理解这个吗?

2 个答案:

答案 0 :(得分:5)

您可以尝试以下代码重新排序结帐页面字段。您可以选择数组中字段的顺序。

// for billing fields

add_filter("woocommerce_checkout_fields", "new_order_fields");

function new_order_fields($fields) {

    $order = array(
        "billing_company", 
        "billing_first_name", 
        "billing_last_name", 
        "billing_address_1", 
        "billing_address_2", 
        "billing_postcode", 
        "billing_country", 
        "billing_email", 
        "billing_phone"
    );
    foreach( $order as $field ) {
        $ordered_fields[$field] = $fields["billing"][$field];
    }

    $fields["billing"] = $ordered_fields;
    return $fields;    
}

// for shipping fields
add_filter("woocommerce_checkout_fields", "new_shiping_order_fields");

function new_shiping_order_fields($fields) {

    $order = array(   
        "shipping_city", 
        "shipping_postcode", 
        "shipping_country",    
        "shipping_first_name", 
        "shipping_last_name", 
        "shipping_company", 
        "shipping_address_1", 
        "shipping_address_2"

    );
    foreach( $order as $field ) {
        $ordered_fields[$field] = $fields["shipping"][$field];
    }

    $fields["shipping"] = $ordered_fields;
    return $fields;
}

答案 1 :(得分:0)

You can find a complete guide about that in the WooCommerce docs.

You are defining a new array $fields2 and this array doesn't have all the fields required in woocommerce_checkout_fields. You only need to override $fields["billing"] and $fields["shipping"] just like @pallavi showed you.

To remove a field (ex. order_comments) you can put this line in your function

unset($fields['order']['order_comments']);