在Woocommerce中将邮递区号更改为drpdown

时间:2018-12-11 13:02:37

标签: php wordpress woocommerce html-select checkout

我正在尝试使运送中的邮政编码字段成为woocommerce结帐页面中带有我的选项的选择类型:

  1. 未设置运送状态和邮政编码结帐字段

    add_filter( 'woocommerce_checkout_fields' , 'partial_unsetting_checkout_fields' );
    function partial_unsetting_checkout_fields( $fields ) {
         unset($fields['shipping']['woocommerce_checkout_fields']);
         return $fields;
    }  
    
  2. 重新插入自定义运输邮政代码结帐字段,

    add_filter( 'woocommerce_checkout_fields' , 'art_override_default_address_fields' );
    function art_override_default_address_fields( $fields ) {
        $fields['shipping_postcode']['type'] = 'select';
        $fields['shipping_postcode']['class'] = array('form-row-wide');
        $fields['shipping_postcode']['required'] = true;
        $fields['shipping_postcode']['label'] = __('Postcode', 'my_theme_slug');
        $fields['shipping_postcode']['placeholder'] = __('Enter Postcode', 'my_theme_slug');
        $fields['shipping_postcode']['default'] ='Choice 1';
        $fields['shipping_postcode']['options'] = array(
            'option_1' => 'Choice 1',
            'option_2' => 'Choice 2',
            'option_3' => 'Choice 3'
        );
    } 
    

如何使用自定义选项将帐单邮政编码转换为选择字段?

1 个答案:

答案 0 :(得分:1)

有多种方法可以做到:

1)仅用于运送邮政编码字段,您将使用以下内容:

add_filter( 'woocommerce_shipping_fields' , 'customize_shipping_postcode_field' );
function customize_shipping_postcode_field( $shipping_fields ) {

    $shipping_fields['shipping_postcode']['type'] = 'select';
    $shipping_fields['shipping_postcode']['options'] = array(
        ''         => __('Select your postcode', 'woocommerce'),
        'option_1' => 'Choice 1',
        'option_2' => 'Choice 2',
        'option_3' => 'Choice 3'
    );

    return $shipping_fields;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。应该可以。


2)对于帐单和运输邮政编码字段,您可以改用它:

add_filter( 'woocommerce_default_address_fields' , 'customize_postcode_fields' );
function customize_postcode_fields( $adresses_fields ) {

    $adresses_fields['postcode']['type'] = 'select';
    $adresses_fields['postcode']['options'] = array(
        ''         => __('Select your postcode', 'woocommerce'),
        'option_1' => 'Choice 1',
        'option_2' => 'Choice 2',
        'option_3' => 'Choice 3'
    );

    return $adresses_fields;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。应该可以。


enter image description here