【问题标题】:Remove specific states on WooCommerce checkout BUT only in the shipping form删除 WooCommerce 结帐上的特定状态,但仅在运输表格中
【发布时间】:2022-02-09 08:56:10
【问题描述】:

我有这段代码可以从结帐页面的账单和运输表单中删除一些州。它工作正常。

我需要删除状态,但只是在“运输表格”中。然后删除的“状态”应显示在“计费表单”中

我想我需要使用除“woocommerce_states”之外的另一个钩子。但不确定是哪一个。

function remove_my_states ($states) {
   unset ($states ['ES'] ['TF']);// tenerife
   unset ($states ['ES'] ['GC']);// gran canaria
   unset ($states ['ES'] ['CE']);// ceuta
   unset ($states ['ES'] ['ML']); // melilla
   unset ($states ['ES'] ['PM']); // baleares
   return $states;
   }
 
add_filter ('woocommerce_states', 'remove_my_states');

【问题讨论】:

    标签: wordpress woocommerce hook-woocommerce checkout


    【解决方案1】:

    您的问题并不那么明显,因为使用当前使用的钩子,您无法区分字段。作为解决方法,您可以使用woocommerce_form_field_$args['type'] 过滤器挂钩。

    回调函数使用$field,其中包含从<p>到结束</p> HTML标签的所有HTML,因此我们可以通过我们的函数重写整个输出。

    所以你得到:

    function filter_woocommerce_form_field_state( $field, $key, $args, $value ) {
        // Checkout page only
        if ( ! is_checkout() ) return $field;
        
        // Only for shipping
        if ( $key == 'shipping_state' ) {
            // Replace all occurrences of the search string with the replacement string
            $field = str_replace( '<option value="CE" >Ceuta</option>', '', $field );
            $field = str_replace( '<option value="ML" >Melilla</option>', '', $field );
            $field = str_replace( '<option value="PM" >Baleares</option>', '', $field );
            // Etc..
        }
        
        // Do something
        return $field;
    }
    add_filter( 'woocommerce_form_field_state', 'filter_woocommerce_form_field_state', 10, 4 );
    

    可能会派上用场:/woocommerce/i18n/states.php

    更多信息:How should I change the woocommerce_form_field HTML Structure?

    【讨论】:

    • 无法在我的网站上运行。我假设我使用的主题可能会导致此代码失败。我使用 Flatsome 主题。这是我网站的结帐页面t12.d523.dinaserver.com/finalizar-compra
    • @JPashs 您可以随时尝试使用默认主题。我不能考虑影响我答案的插件或主题,因为它们有 1000 多个。你也可以试试How to debug in WooCommerce。问候
    • 我尝试用其他主题,但它不起作用。我认为问题在于您的代码尝试匹配“选项”元素。在“二十二十二”主题中,美国是一个使用 ul->li 元素构建的搜索框(不是 'select->option 元素)这是我的网站:latevabotiga.com/tienda
    猜你喜欢
    • 1970-01-01
    • 2016-06-15
    • 2019-01-20
    • 2020-11-01
    • 2013-12-15
    • 2016-03-20
    • 2020-10-03
    • 1970-01-01
    • 2018-11-03
    相关资源
    最近更新 更多