WooCommerce配送计算器(Shipping Calculator.php)-php覆盖-仅限邮政编码

WooCommerce配送计算器(Shipping Calculator.php)-php覆盖-仅限邮政编码,php,css,woocommerce,calculator,shipping,Php,Css,Woocommerce,Calculator,Shipping,这里需要一些建议 我正在尝试编辑我的发货计算器(不是基于的结帐计算器) 我正在浏览代码,无法复制我试图完成的内容,如图所示: 我只想隐藏State&Country选项,这样您就只能输入邮政编码了。我尝试了一些显而易见的方法,比如将其隐藏在CSS样式表/覆盖中,只是将宽度/长度设置为“0” 但它仍然需要国家来正确计算。例如,我甚至在woocommerce的后端设置了只计算“1个邮政编码(ex:48081)”,并只输入该邮政编码,即使它有一个分配给它的费率,但不幸的是,这并没有解决它 我现在通过Fi

这里需要一些建议

我正在尝试编辑我的发货计算器(不是基于的结帐计算器)

我正在浏览代码,无法复制我试图完成的内容,如图所示:

我只想隐藏State&Country选项,这样您就只能输入邮政编码了。我尝试了一些显而易见的方法,比如将其隐藏在CSS样式表/覆盖中,只是将宽度/长度设置为“0”

但它仍然需要国家来正确计算。例如,我甚至在woocommerce的后端设置了只计算“1个邮政编码(ex:48081)”,并只输入该邮政编码,即使它有一个分配给它的费率,但不幸的是,这并没有解决它

我现在通过FileZilla进入服务器的SFTP,备份shipping-calculator.php文件并进行相应的编辑,我想知道这段代码中是否有一些特定的东西可以帮助我实现这一点:

<?php
/**
 * Shipping Calculator
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/cart/shipping-calculator.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.0.8
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

if ( 'no' === get_option( 'woocommerce_enable_shipping_calc' ) || ! WC()->cart->needs_shipping() ) {
    return;
}

?>

<?php do_action( 'woocommerce_before_shipping_calculator' ); ?>

<form class="woocommerce-shipping-calculator" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">

    <p><a href="#" class="shipping-calculator-button"><?php _e( 'Calculate Shipping', 'woocommerce' ); ?></a></p>

    <section class="shipping-calculator-form" style="display:none;">

        <p class="form-row form-row-wide" id="calc_shipping_country_field">
            <select name="calc_shipping_country" id="calc_shipping_country" class="country_to_state" rel="calc_shipping_state">
                <option value=""><?php _e( 'Select a country&hellip;', 'woocommerce' ); ?></option>
                <?php
                    foreach( WC()->countries->get_shipping_countries() as $key => $value )
                        echo '<option value="' . esc_attr( $key ) . '"' . selected( WC()->customer->get_shipping_country(), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
                ?>
            </select>
        </p>

        <p class="form-row form-row-wide" id="calc_shipping_state_field">
            <?php
                $current_cc = WC()->customer->get_shipping_country();
                $current_r  = WC()->customer->get_shipping_state();
                $states     = WC()->countries->get_states( $current_cc );

                // Hidden Input
                if ( is_array( $states ) && empty( $states ) ) {

                    ?><input type="hidden" name="calc_shipping_state" id="calc_shipping_state" placeholder="<?php esc_attr_e( 'State / county', 'woocommerce' ); ?>" /><?php

                // Dropdown Input
                } elseif ( is_array( $states ) ) {

                    ?><span>
                        <select name="calc_shipping_state" id="calc_shipping_state" placeholder="<?php esc_attr_e( 'State / county', 'woocommerce' ); ?>">
                            <option value=""><?php _e( 'Select a state&hellip;', 'woocommerce' ); ?></option>
                            <?php
                                foreach ( $states as $ckey => $cvalue )
                                    echo '<option value="' . esc_attr( $ckey ) . '" ' . selected( $current_r, $ckey, false ) . '>' . __( esc_html( $cvalue ), 'woocommerce' ) .'</option>';
                            ?>
                        </select>
                    </span><?php

                // Standard Input
                } else {

                    ?><input type="text" class="input-text" value="<?php echo esc_attr( $current_r ); ?>" placeholder="<?php esc_attr_e( 'State / county', 'woocommerce' ); ?>" name="calc_shipping_state" id="calc_shipping_state" /><?php

                }
            ?>
        </p>

        <?php if ( apply_filters( 'woocommerce_shipping_calculator_enable_city', false ) ) : ?>

            <p class="form-row form-row-wide" id="calc_shipping_city_field">
                <input type="text" class="input-text" value="<?php echo esc_attr( WC()->customer->get_shipping_city() ); ?>" placeholder="<?php esc_attr_e( 'City', 'woocommerce' ); ?>" name="calc_shipping_city" id="calc_shipping_city" />
            </p>

        <?php endif; ?>

        <?php if ( apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true ) ) : ?>

            <p class="form-row form-row-wide" id="calc_shipping_postcode_field">
                <input type="text" class="input-text" value="<?php echo esc_attr( WC()->customer->get_shipping_postcode() ); ?>" placeholder="<?php esc_attr_e( 'Postcode / ZIP', 'woocommerce' ); ?>" name="calc_shipping_postcode" id="calc_shipping_postcode" />
            </p>

        <?php endif; ?>

        <p><button type="submit" name="calc_shipping" value="1" class="button"><?php _e( 'Update Totals', 'woocommerce' ); ?></button></p>

        <?php wp_nonce_field( 'woocommerce-cart' ); ?>
    </section>
</form>

<?php do_action( 'woocommerce_after_shipping_calculator' ); ?>

代码更新

如果您仅在问题更新确认时在美国销售:

您可以尝试替换装运计算器模板上的第54行:
如果(是数组($states)和空($states)){

通过
if(空($current_r)){

和第59行替换:
}elseif(is_数组($states)){

通过
}elseif(false){

这样:
1) 如果客户已登录并且已定义发货状态,则您将获得预填充的标准输入 2) 如果客户未登录,则会得到隐藏的输入字段(空)


尝试一下,因为我无法真正测试它,因为它也很大程度上取决于woocommerce设置。

我能够通过将初始国家选择更新为仅美国来解决问题,因此请注意,这只适用于单个国家,适用于那些可能通过编辑以下PHP代码而偶然发现此问题的人:

    <p class="form-row form-row-wide" id="calc_shipping_country_field">
        <select name="calc_shipping_country" id="calc_shipping_country" class="country_to_state" rel="calc_shipping_state">
            <option value=""><?php _e( 'Select a country&hellip;', 'woocommerce' ); ?></option>
            <?php
                foreach( WC()->countries->get_shipping_countries() as $key => $value )
                    echo '<option value="' . esc_attr( $key ) . '"' . selected( WC()->customer->get_shipping_country(), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
            ?>
        </select>
    </p>
完成所有这些之后,您需要进入后端并调整配送区域设置,以反映每个州的邮政编码范围,如下所示:(删除之后的州,这些州仅用于参考。)


另一个非常感谢@LoicTheAztec的帮助,他的方式最初是我希望通过PHP实现的答案,但我变懒了,调整了CSS样式表lol。

在你发布之前的3分钟左右,我更新了我的帖子,最后一个代码文本框是我在上实现的强制更改,而不是允许下拉。慢慢地但肯定地在这个lolAs上取得进展。你的答案对你的问题是好的,你应该接受它(在你的答案旁边有一个小的灰色复选标记)…我已经投票支持了你的答案。谢谢。
    <p class="form-row form-row-wide" id="calc_shipping_country_field">
        <select name="calc_shipping_country" id="calc_shipping_country" class="country_to_state" rel="calc_shipping_state">
            <option value="US" selected="selected">United States (US)</option>
            <?php
                foreach( WC()->countries->get_shipping_countries() as $key => $value )
                    echo '<option value="' . esc_attr( $key ) . '"' . selected( WC()->customer->get_shipping_country(), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
            ?>
        </select>
    </p>
    <p class="form-row form-row-wide" id="calc_shipping_country_field">
        <select name="calc_shipping_country" id="calc_shipping_country" class="country_to_state" rel="calc_shipping_state">
            <option value=""><?php _e( 'Select a country&hellip;', 'woocommerce' ); ?></option>
            <?php
                foreach( WC()->countries->get_shipping_countries() as $key => $value )
                    echo '<option value="' . esc_attr( $key ) . '"' . selected( WC()->customer->get_shipping_country(), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
            ?>
        </select>
    </p>
<p class="form-row form-row-wide" id="calc_shipping_country_field">
    <select name="calc_shipping_country" id="calc_shipping_country" class="country_to_state" rel="calc_shipping_state">
        <option value="US" selected="selected">United States (US)</option>
        <?php
            foreach( WC()->countries->get_shipping_countries() as $key => $value )
                echo '<option value="' . esc_attr( $key ) . '"' . selected( WC()->customer->get_shipping_country(), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
        ?>
    </select>
</p>
#add_payment_method .cart-collaterals .cart_totals table select, .woocommerce-cart .cart-collaterals .cart_totals table select, .woocommerce-checkout .cart-collaterals .cart_totals table select {
width: 0 height: 0;}