首页 文章

如何在Woocommerce的结帐字段之间添加 Headers

提问于
浏览
4

我正在自定义WooCommerce结帐页面字段 . 我想在字段之间添加 Headers (文本) . 我重新排序了这样的字段

add_filter('woocommerce_checkout_fields', 'ac_checkout_reorder_fields');

function ac_checkout_reorder_fields($fields) {

    $order = array(
        "billing_first_name", 
        "billing_last_name", 
        "billing_company", 
        "billing_email", 
        "billing_phone",
        "billing_address_1", 
        "billing_address_2", 
        "billing_postcode", 
        "billing_country" 


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

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

}

然后我添加了一个这样的新 Headers

add_action( 'woocommerce_after_checkout_billing_form', 'add_custom_heading' );

function add_custom_heading( $checkout ) {

    echo '<div id="add_custom_heading"><h2>' . __('Custom Heading Here') . '</h2></div>' ;


}

现在重新排列字段并显示自定义 Headers . 但是我希望 the heading showing just below the name & company fields. 使用我的代码,该字段将在下面添加 . 我如何在我想要的地方重新定位 .

PS:我还尝试使用此挂钩添加自定义整个字段部分 woocommerce_checkout_fields 添加占位符并删除标签 . 以下是代码,但这也无法帮助我解决问题 .

function add_wc_custom_fields($fields) {
global $woocommerce;
    $countries_obj   = new WC_Countries();
    $countries   = $countries_obj->__get('countries');

     $fields['billing']['billing_first_name'] = array(
            'label' => '',
            'placeholder' => _x('First Name*', 'placeholder', 'woocommerce'),
            'required' => true,
           'class'    => array( 'form-row-first' ),
        );
        $fields['billing']['billing_last_name'] = array(
            'label' => '',
            'placeholder' => _x('last Name*', 'placeholder', 'woocommerce'),
            'required' => true,
           'class'    => array( 'form-row-last' ),
        );
        $fields['billing']['billing_company'] = array(
            'label' => '',
            'placeholder' => _x('Company Name', 'placeholder', 'woocommerce'),
            'required' => false,
            'class' => array('checkout-billing-company')
        );
        $fields['billing']['billing_address_1'] = array(
            'label' => '',
            'placeholder' => _x('Address(Line 1)*', 'placeholder', 'woocommerce'),
            'required' => true,
            'class' => array('checkout-billing-addressL1')
        );
         $fields['billing']['billing_address_2'] = array(
            'label' => '',
            'placeholder' => _x('Address(Line 2)*', 'placeholder', 'woocommerce'),
            'required' => false,
            'class' => array('checkout-billing-addressL2')
        );
         $fields['billing']['billing_email'] = array(
            'label' => '',
            'placeholder' => _x('Email Address*', 'placeholder', 'woocommerce'),
            'required' => true,
            'class' => array('form-row-first')
        );
        $fields['billing']['billing_phone'] = array(
            'label' => '',
            'placeholder' => _x('Phone Number', 'placeholder', 'woocommerce'),
            'required' => false,
            'class' => array('form-row-last')
        );
        $fields['billing']['billing_city'] = array(
            'label' => '',
            'placeholder' => _x('Town/City', 'placeholder', 'woocommerce'),
            'required' => false,
            'class' => array('form-row-first')
        );

$fields['billing']['billing_state'] = array(
            'label' => '',
            'placeholder' => _x('State/County', 'placeholder', 'woocommerce'),
            'required' => false,
            'class' => array('form-row-first')
        );
    $fields['billing']['billing_postcode'] = array(
            'label' => '',
            'placeholder' => _x('Zip/Postcode', 'placeholder', 'woocommerce'),
            'required' => false,
            'class' => array('form-row-first')
        );
        $fields['billing']['billing_country'] = array(
            'label' => '',
            'type' => 'select',
            'placeholder' => _x('Country', 'placeholder', 'woocommerce'),
            'required' => false,
            'class' => array('form-row-last'),
              'options'    => $countries
        );
        return $fields;
    }

add_filter('woocommerce_checkout_fields', 'add_wc_custom_fields');

3 回答

  • 0

    我们可以使用过滤器 'woocommerce_form_field_' . $type ...其中 $type 是输入的类型...在我们的例子中 billing_company 是文本类型...这个过滤器返回字段的html,在我们的案例结算字段中, billing_company .. filter有4个参数被传递,这些是 $field$key$args$value ......我们只需要其中两个......

    add_action( 'woocommerce_form_field_text','reigel_custom_heading', 10, 2 );
    function reigel_custom_heading( $field, $key ){
        // will only execute if the field is billing_company and we are on the checkout page...
        if ( is_checkout() && ( $key == 'billing_company') ) {
            $field .= '<div id="add_custom_heading"><h2>' . __('Custom Heading Here') . '</h2></div>';
        }
        return $field;
    }
    
  • 10

    我宁愿使用 woocommerce_form_field_<field_type> 过滤器来创建新的字段类型,而不是挂钩到现有的 woocommerce_form_field_<field_type> 过滤器,例如 woocommerce_form_field_text . 这允许我们为其他字段类型添加HTML输出(因此我们可以使用HTML输出作为 Headers ),并且我们可以在使用 woocommerce_checkout_fields 过滤器修改结帐字段时使用此新 Headers "field type" .

    // Add field type to WooCommerce form field 
    add_filter( 'woocommerce_form_field_heading','sc_woocommerce_form_field_heading', 10, 4 );
    function sc_woocommerce_form_field_heading($field, $key, $args, $value) {
        $output = '<h3 class="form-row form-row-wide">'.__( $args['label'], 'woocommerce' ).'</h3>';
        echo $output;
    }
    
    // Modify checkout fields
    add_filter( 'woocommerce_checkout_fields','custom_override_checkout_fields' );
    function custom_override_checkout_fields( $fields ) {
        $fields['billing']['billing_heading_name'] = array(
            'type'      => 'heading',
            'label'     => 'Heading here',
        );
    

    使用上述方法,原始问题的解决方案将是:

    add_filter('woocommerce_checkout_fields', 'ac_checkout_reorder_fields');
    function ac_checkout_reorder_fields($fields) {
        $fields['billing']['billing_heading_name'] = array(
            'type'      => 'heading',
            'label'     => 'Heading here',
        );
        $order = array(
            "billing_first_name", 
            "billing_last_name", 
            "billing_heading_name",
            "billing_company", 
            "billing_email", 
            "billing_phone",
            "billing_address_1", 
            "billing_address_2", 
            "billing_postcode", 
            "billing_country" 
        );
        foreach($order as $field) {
            $ordered_fields[$field] = $fields["billing"][$field];
        }
        $fields["billing"] = $ordered_fields;
        return $fields;
    }
    
  • 1
    add_filter('woocommerce_form_field', 'mmodify_wc_checkout_pge', 10, 4);
    
    function mmodify_wc_checkout_pge($field, $key, $args, $value = null) {
    
    
        switch ($key) {
            case 'billing_email':
    
    
                $a = '<p class="form-row form-row-wide" id="main_heading" data-priority="110"><label for="" class="main-address" style="font-weight:bold; font-size:22px;">Main address&nbsp;</label></p>';
    
                return $a . $field;
                break;
            default:
                return $field;
                break;
        }
    }
    

相关问题