简体   繁体   English

在 Woocommerce 购物车页面中的 2 列上自定义结帐字段

[英]Customizing checkout fields on 2 columns in Woocommerce cart page

After the new WooCommerce update checkout fields columns are acting strangely.在新的 WooCommerce 更新结帐字段后,列的行为很奇怪。 That are my checkout fields customizations:这是我的结帐字段自定义:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
unset($fields['billing']['billing_address_2']);
    return $fields;
}

// Checkout placeholder
add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 );
function override_billing_checkout_fields( $fields ) {
    $fields['billing']['billing_phone']['placeholder'] = 'Telefon';
    $fields['billing']['billing_email']['placeholder'] = 'Email';
    $fields['billing']['billing_company']['placeholder'] = 'Firmanavn';
    return $fields;
}

add_filter('woocommerce_default_address_fields', 'override_default_address_checkout_fields', 20, 1);
function override_default_address_checkout_fields( $address_fields ) {
    $address_fields['first_name']['placeholder'] = 'Fornavn';
    $address_fields['last_name']['placeholder'] = 'Efternavn';
    $address_fields['address_1']['placeholder'] = 'Adresse';
    $address_fields['state']['placeholder'] = 'Stat';
    $address_fields['postcode']['placeholder'] = 'Postnummer';
    $address_fields['city']['placeholder'] = 'By';
    return $address_fields;
}

I have set that checkout fields in a custom cart page visible here .我已在此处可见的自定义购物车页面中设置了结帐字段。


My question: With php or with CSS styling, is there a way to have:我的问题:使用 php 或 CSS 样式,有没有办法:

  • firstname and lastname fields in 2 columns next to each other;彼此相邻的 2 列中的名字和姓氏字段;
  • Address 1 field full width at 100% (I have unset address 2 field) ;地址 1 字段全宽为 100% (我未设置地址 2 字段)
  • State field full width at 100%;状态字段全宽为 100%;
  • zip code and city fields in 2 columns next to each other;邮政编码和城市字段在 2 列中彼此相邻;
  • phone and email fields in 2 columns next to each other.电话和电子邮件字段在 2 列中彼此相邻。

I tried this CSS, but it just makes 100% for everything.我试过这个 CSS,但它只是 100% 的一切。 How do I target just these above?我如何定位以上这些?

p.form-row-first, p.form-row-last {
    width: 100%;
    float: left;
}

First the correct php code where you rename your label fields and where you set the classes that are involved in the width styling.首先是正确的 php 代码,您可以在其中重命名标签字段,并在其中设置宽度样式中涉及的类。

Explanations about classes in Woocommerce checkout fields:关于 Woocommerce 结账字段中的类的说明:

  • array('form-row-first') - Field will be displayed in 1st column (so at 50% of width). array('form-row-first') - 字段将显示在第一列中(因此宽度的 50%)。
  • array('form-row-last') - Field will be displayed in 2nd column (so at 50% of width). array('form-row-last') - 字段将显示在第二列中(所以在 50% 的宽度处)。
  • array('form-row-wide') - Field will be displayed in both columns (so at 100% of width). array('form-row-wide') - 字段将显示在两列中(因此宽度为 100%)。

The code:代码:

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

    // Remove billing address 2
    unset($fields['billing']['billing_address_2']);

    if( is_cart()){ // <== On cart page only
        // Change placeholder
        $fields['billing']['billing_phone']['placeholder']   = __( 'Telefon', $domain );
        $fields['billing']['billing_email']['placeholder']   = __( 'Email', $domain );
        $fields['billing']['billing_company']['placeholder'] = __( 'Firmanavn', $domain );
        
        // 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_company']['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'] = __( 'Fornavn', $domain );
        $address_fields['last_name']['placeholder']  = __( 'Efternavn', $domain );
        $address_fields['address_1']['placeholder']  = __( 'Adresse', $domain );
        $address_fields['state']['placeholder']      = __( 'Stat', $domain );
        $address_fields['postcode']['placeholder']   = __( 'Postnummer', $domain );
        $address_fields['city']['placeholder']       = __( 'By', $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-wide');  // 100%
        $address_fields['postcode']['class']   = array('form-row-first'); //  50%
        $address_fields['city']['class']       = array('form-row-last');  //  50%
    }
    return $address_fields;
}

Now the related CSS code should be (by default) something like this:现在相关的 CSS 代码应该(默认情况下)是这样的:
(Try to remove !important to see if it's really necessary) (尝试删除!important看看是否真的有必要)

.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;
    }
}

I don't understand your CSS code.我不明白你的 CSS 代码。 I would give the text input fields a width of 100 % generally, like this:我通常会给文本输入字段的宽度为 100%,如下所示:

 input[type=text] { width: 100%; } input[type=text].half { float: left; width: 50%; }

And those which shall be half size would have a class in the form's HTML code.那些应该是一半大小的将在表单的 HTML 代码中有一个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM