【问题标题】:Woocommerce checkout page changes not workingWoocommerce 结帐页面更改不起作用
【发布时间】:2023-04-04 08:17:01
【问题描述】:

我现在尝试了几种方法来更改帐单和运输的表单字段。我正在尝试执行以下操作:

在结算部分

  • 更改地址2占位符内容
  • 更改地址 2 标签
  • 地址 1 更改为 50%
  • 地址 2 将等级更改为 50%
  • 城市变化等级为 50%
  • 状态更改等级为 50%
  • 邮政编码更改等级为 50%
  • 电话更改等级为 50%
  • 地址 2 删除阅读器类

在送货地址部分

  • 地址2更改占位符地址2更改标签
  • 地址 1 更改类别 50%
  • 地址 2 更改类别 50%
  • 状态更改等级为 50%
  • 邮政编码更改等级为 50%
  • 地址 2 删除文本阅读器类

他们需要不同的布局,所以我不能使用“默认”挂钩(有效)。到目前为止,这是我的代码。

//customize woocommerce checkout fields
add_filter( 'woocommerce_checkout_fields' , 'jm_checkout_billing_fields', 20, 1 );
function jm_checkout_billing_fields( $billing_fields ){


       // Change placeholder
       $billing_fields['billing']['billing_address_2']['placeholder'] = __('Apt #, Floor, etc', $domain);

       // Change label
       $billing_fields['billing']['billing_address_2']['label'] = __('Apt #, Floor, etc', $domain);
       
       // Change class
       $billing_fields['billing']['billing_address_1']['class']   = array('form-row-first'); //  50%
       $billing_fields['billing']['billing_address_2']['class']   = array('form-row-last');  //  50%
       $billing_fields['billing']['billing_city']['class']   = array('form-row-first'); //  50%
       $billing_fields['billing']['billing_state']['class']   = array('form-row-last');  //  50%
       $billing_fields['billing']['billing_postcode']['class']   = array('form-row-first'); //  50%
       $billing_fields['billing']['billing_phone']['class']   = array('form-row-last');  //  50%
       $billing_fields['billing']['billing_address_2']['label_class'] = array(); // No label class

   return $billing_fields;
}

add_filter('woocommerce_address_fields', 'jm_address_fields', 20, 1);
function jm_address_fields( $address_fields ){
   
   if(  is_checkout()){ // On checkout page only
       // Change placeholder
       $address_fields['address_2']['placeholder'] = __( 'Apt #, Floor, etc', $domain );

       //change label
       $address_fields['address_2']['label'] = __( 'Apt #, Floor, etc', $domain );



       // Change class
       $address_fields['address_1']['class'] = array('form-row-first'); //  50%
       $address_fields['address_2']['class']  = array('form-row-last');  //  50%
       $address_fields['state']['class']  = array('form-row-first');  //  50%
       $address_fields['postcode']['class']  = array('form-row-last');  //  50%
       $address_fields['address_2']['label_class'] = array(); // No label class


   }
   return $address_fields;
}

如果我对所有内容都使用“woocommerce_default_address_fields”,它可以工作,但同样,我需要帐单地址与我的地址字段的布局不同。

我可以将这些设置为“默认值”,但需要以“城市”开头的不同

  • 更改地址2占位符内容
  • 更改地址 2 标签
  • 地址 1 更改为 50%
  • 地址 2 将等级更改为 50%
  • 地址 2 删除文本阅读器类

我按照此处的答案进行了更改,以尝试使其单独工作:https://stackoverflow.com/a/48801880/14664702

【问题讨论】:

  • 您没有在代码中提到您要实现的目标。你只提到了什么是行不通的,如果我们不知道你在做什么,这也是没有用的。说明您要解决的具体问题是什么。
  • @bhanu 我更新了我的帖子以包含更多详细信息。谢谢
  • 你使用什么主题 @jmasked ?我可以在店面试试这个,因为可能有很多自定义 CSS 编写使答案无效。
  • 谢谢@bhanu 我正在使用你好主题
  • 请分享它的链接??

标签: wordpress woocommerce checkout


【解决方案1】:

更改地址字段的方法是使用woocommerce_default_address_fields 过滤器。使用以下代码进行更改。

add_filter( 'woocommerce_default_address_fields' , 'bks_override_default_address_fields' );

// Our hooked in function - $address_fields is passed via the filter!
function bks_override_default_address_fields( $address_fields ) {
    $address_fields['address_1']['placeholder'] = 'Apt #, Floor, etc';
    $address_fields['address_2']['label'] = 'Apt #, Floor, etc';
    $address_fields['address_1']['class']   = array('form-row-first', 'address-field');
    $address_fields['address_2']['class']   = array('form-row-last', 'address-field');
    $address_fields['city']['class']   = array('form-row-first'); //  50%
    $address_fields['state']['class']   = array('form-row-last');  //  50%
    $address_fields['postcode']['class']   = array('form-row-first'); //  50%
    $address_fields['address_2']['label_class'] = array(); // No label class

    return $address_fields;
}

这些只是我从您的代码中快速获得的一些更改。我没有查看您的完整更改列表,因为我认为您可以从上面的代码中进行更改。

我在这里添加默认字段。这些是实际的默认字段。这些都是从这里捡来的。 https://github.com/woocommerce/woocommerce/blob/trunk/includes/class-wc-countries.php#L683-L755

$fields = array(
    'first_name' => array(
        'label'        => __( 'First name', 'woocommerce' ),
        'required'     => true,
        'class'        => array( 'form-row-first' ),
        'autocomplete' => 'given-name',
        'priority'     => 10,
    ),
    'last_name'  => array(
        'label'        => __( 'Last name', 'woocommerce' ),
        'required'     => true,
        'class'        => array( 'form-row-last' ),
        'autocomplete' => 'family-name',
        'priority'     => 20,
    ),
    'company'    => array(
        'label'        => __( 'Company name', 'woocommerce' ),
        'class'        => array( 'form-row-wide' ),
        'autocomplete' => 'organization',
        'priority'     => 30,
        'required'     => 'required' === get_option( 'woocommerce_checkout_company_field', 'optional' ),
    ),
    'country'    => array(
        'type'         => 'country',
        'label'        => __( 'Country / Region', 'woocommerce' ),
        'required'     => true,
        'class'        => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
        'autocomplete' => 'country',
        'priority'     => 40,
    ),
    'address_1'  => array(
        'label'        => __( 'Street address', 'woocommerce' ),
        /* translators: use local order of street name and house number. */
        'placeholder'  => esc_attr__( 'House number and street name', 'woocommerce' ),
        'required'     => true,
        'class'        => array( 'form-row-wide', 'address-field' ),
        'autocomplete' => 'address-line1',
        'priority'     => 50,
    ),
    'address_2'  => array(
        'label'        => $address_2_label,
        'label_class'  => array( 'screen-reader-text' ),
        'placeholder'  => esc_attr( $address_2_placeholder ),
        'class'        => array( 'form-row-wide', 'address-field' ),
        'autocomplete' => 'address-line2',
        'priority'     => 60,
        'required'     => 'required' === get_option( 'woocommerce_checkout_address_2_field', 'optional' ),
    ),
    'city'       => array(
        'label'        => __( 'Town / City', 'woocommerce' ),
        'required'     => true,
        'class'        => array( 'form-row-wide', 'address-field' ),
        'autocomplete' => 'address-level2',
        'priority'     => 70,
    ),
    'state'      => array(
        'type'         => 'state',
        'label'        => __( 'State / County', 'woocommerce' ),
        'required'     => true,
        'class'        => array( 'form-row-wide', 'address-field' ),
        'validate'     => array( 'state' ),
        'autocomplete' => 'address-level1',
        'priority'     => 80,
    ),
    'postcode'   => array(
        'label'        => __( 'Postcode / ZIP', 'woocommerce' ),
        'required'     => true,
        'class'        => array( 'form-row-wide', 'address-field' ),
        'validate'     => array( 'postcode' ),
        'autocomplete' => 'postal-code',
        'priority'     => 90,
    ),
);

您可以更改上述数组中的任何值以获得所需的值。

输出

经过测试并且有效

PS:上面的数组没有电话字段。您可以将您的原始代码用于此特定字段。 $billing_fields['billing']['billing_phone']['class'] = array('form-row-last');。它应该可以正常工作。

【讨论】:

  • 非常感谢您的回复。我最初使用了您为过滤器“woocommerce_default_address_fields”建议的确切格式,但它也更改了帐单地址默认值,无论我是否指定了地址字段。这就是为什么我遇到了我遇到的问题。帐单地址和送货地址需要有两种不同的格式,从“城市”开始。在计费城市需要有“form-row-first”类,而在运输中需要“form-row-wide”。
猜你喜欢
  • 2016-07-20
  • 2015-11-29
  • 2016-10-08
  • 2017-11-09
  • 2017-07-30
  • 2019-11-13
  • 2018-03-27
  • 2015-01-17
  • 1970-01-01
相关资源
最近更新 更多