将Woocmmerce的结帐页面中的邮政编码字段更改为下拉菜单

我正在为Woocommerce商店的订单确定基于邮政编码的最低订购量。 另外,一旦用户输入邮政编码(例如,他需要多少结帐),我就会显示基于邮政编码的自定义错误消息。 因此,为了防止用户键入无效的邮政编码(这意味着我们不提供的邮政编码),我在下拉菜单中显示下拉列表以选择其有效的邮政编码。

基于此线程Change postcode shipping field to a dropdown in Woocommerce,我可以使用选项将邮政编码字段下拉至下拉列表。

但是当我从下拉列表中选择任何选项时,我收到Woocommerce的“邮政编码无效”错误。另外,我的自定义消息也不会显示。

下面是我在上述线程中使用的代码。

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' => '000001','option_2' => '000002','option_3' => '000003'
);

return $adresses_fields;

}
iCMS 回答:将Woocmmerce的结帐页面中的邮政编码字段更改为下拉菜单

尝试改用以下内容:

add_filter( 'woocommerce_default_address_fields','customize_postcode_fields' );
function customize_postcode_fields( $adresses_fields ) {
    $adresses_fields['postcode']['type'] = 'select';
    $adresses_fields['postcode']['input_class'] = array('state_select');
    $adresses_fields['postcode']['options'] = array(
        '' => __('Select your postcode','woocommerce'),'000001' => '000001','000002' => '000002','000003' => '000003'
    );

    return $adresses_fields;
}

代码进入您的活动子主题(或活动主题)的functions.php文件中。 经过测试,可在Storefront主题下的最新WooCommerce版本4.5.1上使用。

本文链接:https://www.f2er.com/1582971.html

大家都在问