dongshan6870 2018-08-26 17:31
浏览 38
已采纳

结帐字段在Woocommerce购物车页面中有2列

I have found the solution to have the following fields in-line with each other on the woocommerce checkout page - orignal question here: Customizing checkout fields on 2 columns in Woocommerce cart page

Most fields with this code become inline, however the following fields have no affect:

  • Phone
  • Email
  • Address line 2

I think it's an error within the billing section in the code. This is the full code:

add_filter( 'woocommerce_checkout_fields' , 'custom_checkout_billing_fields', 20, 1 );
function custom_checkout_billing_fields( $fields ){

if( is_cart()){ // <== On cart page only
    // Change placeholder
    $fields['billing_phone']['placeholder']     = __( 'Phone', $domain );
    $fields['billing_email']['placeholder']     = __( 'Email', $domain );
    $fields['billing_address_2']['placeholder'] = __( 'Address line 2', $domain );

    // Change class
    $fields['billing_phone']['class']     = array('form-row-last'); //  50%
    $fields['billing_email']['class']     = array('form-row-first');  //  50%
    $fields['billing_address_2']['class'] = array('form-row-wide');  // 100%
}
return $fields;
}

add_filter('woocommerce_default_address_fields', 'custom_default_address_fields', 20, 1);
function custom_default_address_fields( $address_fields ){

if( ! is_cart()){ // <== On cart page only
    // Change placeholder
    $address_fields['first_name']['placeholder'] = __( 'First name', $domain );
    $address_fields['last_name']['placeholder']  = __( 'Last name', $domain );
    $address_fields['address_1']['placeholder']  = __( 'Address line 1', $domain );
    $address_fields['state']['placeholder']      = __( 'County', $domain );
    $address_fields['postcode']['placeholder']   = __( 'Post Code', $domain );
    $address_fields['city']['placeholder']       = __( 'Town/City', $domain );

    // Change class
    $address_fields['first_name']['class'] = array('form-row-first'); //  50%
    $address_fields['last_name']['class']  = array('form-row-last');  //  50%
    $address_fields['address_1']['class']  = array('form-row-wide');  // 100%
    $address_fields['state']['class']      = array('form-row-last');  // 50%
    $address_fields['postcode']['class']   = array('form-row-first'); //  50%
    $address_fields['city']['class']       = array('form-row-first');  //  50%
}
return $address_fields;
}

Here is the css code:

.form-row-wide,
.form-row-first,
.form-row-last {
    clear: both !important;
    float: none !important;
    width: 100% !important;
    margin-right: 0 !important;
}

@media (min-width: 768px){
    .form-row-first {
       width: 47% !important;
       float: left !important;
       margin-right: 5.8% !important;
       clear: both !important;
    }
    .form-row-last {
       width: 47% !important;
       float: right !important;
       margin-right: 0 !important;
       clear: none !important;
    }
}
  • 写回答

1条回答 默认 最新

  • dongluan2612 2018-08-26 19:01
    关注

    you have some mistake in your code and i believe the answer which you took those code from is for similar solution and not for your case.

    So if you want to display the checkout filed inline you need only the following:

     add_filter('woocommerce_checkout_fields', 'custom_checkout_billing_fields', 20, 1);
     function custom_checkout_billing_fields($fields)
     {
    $domain = 'woocommerce';
    // Remove billing address 2
    unset($fields['billing']['billing_address_2']);
    
    // Change class
    $fields['billing']['billing_phone']['class'] = array('form-row-first'); //  50%
    $fields['billing']['billing_email']['class'] = array('form-row-last'); //  50%
    $fields['billing']['billing_address_1']['class'] = array('form-row-first'); //  50%
    $fields['billing']['billing_postcode']['class'] = array('form-row-last'); //  50%
    $fields['billing']['billing_company']['class'] = array('form-row-wide'); // 100%
    
    // Change placeholder this below is just if you wanto to change the place holder  you can remove theme if you don't want to change that
    $fields['billing']['billing_phone']['placeholder'] = __('Telefon', $domain);
    $fields['billing']['billing_email']['placeholder'] = __('Email', $domain);
    $fields['billing']['billing_company']['placeholder'] = __('Firmanavn', $domain);
    
    return $fields;
    }
    

    in above code only five filed which we have change the classes if you want the rest let me know or you can add them by yourself by looking for each filed name and change the class in the way you want

    Remember there is block of code in this function that change the placeholder if you don't want them you can simple delete them

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行