简体   繁体   English

将结帐邮政编码字段自定义为自定义下拉菜单

[英]Customizing checkout Postcode field into a custom drop-down menu

I am building a WooCommerce based store.我正在建立一个基于 WooCommerce 的商店。 I have a list of postcodes , each one has a different shipping cost attached through Shipping Zones (some provide free shipping, some have a flat rate).我有一个邮政编码列表,每个邮政编码都有不同的运费(有些提供免费送货,有些提供统一费率)。

When the customer goes to the checkout page, he needs to type his postcode number in the input field.当客户进入结账页面时,他需要在输入字段中输入他的邮政编码。 Depending on postcode, an order preview will show different shipping total (free or flat rate).根据邮政编码,订单预览将显示不同的运费总额(免费或统一费率)。

Here's how the input field looks like in class-wc-countries.php :以下是class-wc-countries.php输入字段的样子:

public function get_default_address_fields() {
    $fields = array(
    'postcode' => array(
            'label'        => __( 'Postcode/ZIP', 'woocommerce' ),
            'required'     => true,
            'class'        => array( 'form-row-first', 'address-field' ),
            'clear'        => true,
            'validate'     => array( 'postcode' ),
            'autocomplete' => 'postal-code',
        ),
    );

However, what I want to do is to turn this field into a drop-down menu, so the customer could just select his postcode option rather than type it.但是,我想要做的是将这个字段变成一个下拉菜单,这样客户就可以选择他的邮政编码选项而不是输入它。

I managed to make it drop-down, but whenever I choose any option it doesn't seem to change shipping total as it would with input field.我设法将其下拉,但是每当我选择任何选项时,它似乎都不会像输入字段那样更改运输总额。

Here's what I did:这是我所做的:

public function get_default_address_fields() {
    $fields = array(
        'postcode' => array(
            'label'        => __( 'Postcode/ZIP', 'woocommerce' ),
            'required'     => true,
            'class'        => array( 'form-row-first', 'address-field' ),
            'clear'        => true,
            'validate'     => array( 'postcode' ),
            'autocomplete' => 'postal-code',
            'type' => 'select',
            'options' => array(
                              'opt1' => "001122", "112200", "334400")
        ),
    );

But this don't work.但这不起作用。

Am I missing something?我错过了什么吗?
How do I make these drop-down options change shipping total?如何让这些下拉选项更改运费总额?

Thanks谢谢

This will answer very partially to your question, and just show you the way to customize checkout fields.这将部分回答您的问题,并只向您展示自定义结帐字段的方法。

Overriding core files is not really something to do, as you will loose everithing each time Woocommerce is going to be updated and is not recommended.覆盖核心文件并不是真正的事情,因为每次更新 Woocommerce 时您都会丢失所有内容,因此不建议这样做。

To override checkout fields in a clean way, first you need to use a custom function hooked in one of that 2 filter hooks:要以干净的方式覆盖结帐字段,首先您需要使用挂接在这两个过滤器钩子之一中的自定义函数:

  • woocommerce_default_address_fields (when customizing billing and shipping address default fields) woocommerce_default_address_fields (自定义帐单和送货地址默认字段时)
  • woocommerce_checkout_fields (when customizing billing or shipping address fields and also others fields). woocommerce_checkout_fields (自定义帐单或送货地址字段以及其他字段时)。

Related official documentation: Customizing checkout fields using actions and filters相关官方文档: 使用操作和过滤器自定义结帐字段

So here I have chose the first hook, and I have corrected your post codes array.所以在这里我选择了第一个钩子,我更正了你的邮政编码数组。 You will get that:你会得到:

在此处输入图片说明

Here is that functional and tested code:这是功能和经过测试的代码:

add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_postcode_field' );
function custom_override_default_postcode_field( $address_fields ) {
    // Your postcodes array
    $postcode_array = array(
        'opt1' => "001122",
        'opt2' => "112200",
        'opt3' => "334400"
    );
    $address_fields['postcode']['type'] = 'select';
    $address_fields['postcode']['options'] = $postcode_array;

    return $address_fields;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

As selecting a post-code is a live front-end event, you will need to use a complex javascript/ajax script with some remote php function to achieve what you want to do, and this is a real development... It also depends on your settings and is complex to handle as there is already some woocommerce ajax scripts operating in that checkout page.由于选择一个post-code是一个实时的前端事件,你需要使用一个复杂的javascript/ajax脚本和一些远程php函数来实现你想要做的事情,这是一个真正的开发......这也取决于在您的设置上并且处理起来很复杂,因为在该结帐页面中已经有一些 woocommerce ajax 脚本在运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM