WooCommerce:结帐时预先选择并限制在一个州和城市 [英] WooCommerce: Pre-select and restrict to one state and city on checkout

查看:118
本文介绍了WooCommerce:结帐时预先选择并限制在一个州和城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一家只销售至一个城市的商店。我想弄清楚如何将城市选项限制为一个预填字段。

I'm setting up a store that only sells to one city. I'm trying to figure out how I can restrict the city option to a pre-filled field.

我已经限制了国家/地区,并在我的代码中使用了以下代码 functions.php

I've already limited the country and using the following code in my functions.php:

add_filter( 'default_checkout_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_state', 'change_default_checkout_state' );

function change_default_checkout_country() {
  return 'PK'; // country code
}

function change_default_checkout_state() {
  return 'SD'; // state code
}

我已经使用以下方法预选了状态:

And I've pre-selected the state using the following:

add_filter( 'woocommerce_states', 'bbloomer_custom_woocommerce_states' );

function bbloomer_custom_woocommerce_states( $states ) {
$states['PK'] = array(
'SD' => 'Sindh',
);
return $states;
}

但是...现在来了这座城市,我迷路了。我试过使用 $ fields ['billing'] ['billing_city'] 以及 $ fields ['billing'] ['billing_city' ] ['value'] 无济于事。

But... Now comes the city and I am lost. I've tried using $fields['billing']['billing_city'] as well as $fields['billing']['billing_city']['value'] to no avail.

我希望客户仅限于卡拉奇。我对WooCommerce过滤器不是很熟悉,因此在实现这一目标方面需要帮助。

I want the customer to be limited to Karachi. I am not very familiar with WooCommerce filters, so I need help on achieving this.

推荐答案

自woocommerce 3起, <现在已弃用并替换了code> default_checkout_country default_checkout_state 过滤器挂钩。

Since woocommerce 3, default_checkout_country and default_checkout_state filter hooks are now deprecated and replaced.

在最后一个函数中,如果您只想限制一个城市,则不应将您的城市包括在州列表中。相反,您应该只返回一个可能的值。

Also in your last function, if you want to restrict just to one city, you should not include your city in the array of states. Instead you should return only one possible value.

因此,这将是您的代码:

So this will be your code:

// default checkout country
add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_shipping_country', 'change_default_checkout_country' );
function change_default_checkout_country() {
    return 'PK'; // country code
}

// default checkout state
add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );
add_filter( 'default_checkout_shipping_state', 'change_default_checkout_state' );
function change_default_checkout_state() {
    return 'SD'; // state code
}

// Setting one state only
add_filter( 'woocommerce_states', 'custom_woocommerce_state', 10, 1 );
function custom_woocommerce_state( $states ) {
    // Returning a unique state
    return array('PK' => array('SD' => 'Sindh'));
}

// Only one country at checkout
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields', 10, 1 );
function custom_checkout_fields( $fields ) {

    $fields['billing']['billing_city']['type'] = 'select';
    $fields['billing']['billing_city']['options'] = array('Karachi' => 'Karachi');
    $fields['shipping']['shipping_city']['type'] = 'select';
    $fields['shipping']['shipping_city']['options'] = array('Karachi' => 'Karachi');

    return $fields;
}

该代码位于活动子主题的function.php文件中(或主题)或任何插件文件中。

此代码已经过测试,可用于wooCommerce 3+及更高版本

This code is tested and works for wooCommerce versions 3+


添加以上代码并保存后,您将需要 WooCommerce>设置>常规以这种方式设置位置:

Once added the code above and saved, you will need in WooCommerce > Settings > General to set locations this way:

然后您将在结帐时得到类似的东西:

Then you will get something like this on checkout:

两个下拉菜单都只有一个值……因此您可以得到期望的结果。

Both dropdown have only one value… So you get what you are expecting.

这篇关于WooCommerce:结帐时预先选择并限制在一个州和城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆