Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于状态的电子商务限运方法_Php_Wordpress_Woocommerce_States_Usps - Fatal编程技术网

Php 基于状态的电子商务限运方法

Php 基于状态的电子商务限运方法,php,wordpress,woocommerce,states,usps,Php,Wordpress,Woocommerce,States,Usps,在我们的商店里,我们有UPS和USPS运输方式 我们销售的产品形状和尺寸都不同,因此我们必须使用API,而不是表价格选项 我只想在用户输入AK、HI、PR、GU、AS、VI或UM的邮政编码时显示USPS费率。我已经尝试过有条件的运输和付款方式,但似乎不起作用。如果我没有错的话,这只适用于结帐时返回的费率/选项,而不适用于我需要的购物车 我在网上浏览了许多提出这个问题的不同页面,并尝试编辑functions.php文件以匹配回答,但所有答案似乎都过时了 我能找到的最接近的答案是下面链接中的选项#2

在我们的商店里,我们有UPS和USPS运输方式
我们销售的产品形状和尺寸都不同,因此我们必须使用API,而不是表价格选项

我只想在用户输入AK、HI、PR、GU、AS、VI或UM的邮政编码时显示USPS费率。我已经尝试过有条件的运输和付款方式,但似乎不起作用。如果我没有错的话,这只适用于结帐时返回的费率/选项,而不适用于我需要的购物车

我在网上浏览了许多提出这个问题的不同页面,并尝试编辑
functions.php
文件以匹配回答,但所有答案似乎都过时了

我能找到的最接近的答案是下面链接中的选项#2,但当我尝试它时,它不起作用:

以下是我从该链接定制的代码,我已经尝试使其正常工作。请注意,我想排除除阿拉斯加、夏威夷、波多黎各、关岛、美属萨摩亚、维尔京群岛和小岛屿以外的每个州的方法

感谢您的帮助

add_filter('woocommerce_package_rates', 'wf_hide_undesired_service_for_required_states', 10, 2);

function wf_hide_undesired_service_for_required_states($rates, $package)
{
    $exclude = array(
        'wf_shipping_usps:D_PRIORITY_MAIL' => array(
         'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY'
        )
    );
    if (is_array($exclude)) {
        foreach($exclude as $shipping_method => $excluded_states) {
            if (in_array(WC()->customer->shipping_state, $excluded_states)) {
                unset($rates[$shipping_method]);
            }
        }
    }

    return $rates;
}

根据我的理解,只有当客户选择AK、HI、PR、GU、As、VI或UM状态时,您才会显示USPS实时费率

这里详细解释了解决方案。

下面的代码片段将不会对进行任何更改。如果您使用的是任何其他供应商的WooCommerce USPS插件,您可以通过将变量
$qualified\u services\u for_states\u list
的元素更改为与该插件相关的适当值来实现

add_filter( 'woocommerce_package_rates', 'xa_show_shipping_method_based_on_state', 10, 2 );

function xa_show_shipping_method_based_on_state( $available_shipping_methods, $package ) {

    $states_list = array( 'AK', 'HI', 'PR', 'GU', 'AS', 'VI', 'UM' );

    $eligible_services_for_states_list  =   array(
            'wf_shipping_usps:flat_rate_box_priority',
            'wf_shipping_usps:flat_rate_box_express',
            'wf_shipping_usps:D_FIRST_CLASS',
            'wf_shipping_usps:D_EXPRESS_MAIL',
            'wf_shipping_usps:D_STANDARD_POST',
            'wf_shipping_usps:D_MEDIA_MAIL',
            'wf_shipping_usps:D_LIBRARY_MAIL',
            'wf_shipping_usps:D_PRIORITY_MAIL',
            'wf_shipping_usps:I_EXPRESS_MAIL',
            'wf_shipping_usps:I_PRIORITY_MAIL',
            'wf_shipping_usps:I_GLOBAL_EXPRESS',
            'wf_shipping_usps:I_FIRST_CLASS',
            'wf_shipping_usps:I_POSTCARDS',         
        );

    // Basically, below code will reset shipping services if the shipping
    // state is other than defined states_list.
    if ( !in_array(WC()->customer->shipping_state, $states_list) ) {
        foreach ( $eligible_services_for_states_list as &$value ) {
            unset( $available_shipping_methods[$value] );       
        }
    }

    return $available_shipping_methods;
}

希望有帮助。

根据我的理解,您希望仅当客户选择AK、HI、PR、GU、As、VI或UM状态时,才显示USPS实时费率

这里详细解释了解决方案。

下面的代码片段将不会对进行任何更改。如果您使用的是任何其他供应商的WooCommerce USPS插件,您可以通过将变量
$qualified\u services\u for_states\u list
的元素更改为与该插件相关的适当值来实现

add_filter( 'woocommerce_package_rates', 'xa_show_shipping_method_based_on_state', 10, 2 );

function xa_show_shipping_method_based_on_state( $available_shipping_methods, $package ) {

    $states_list = array( 'AK', 'HI', 'PR', 'GU', 'AS', 'VI', 'UM' );

    $eligible_services_for_states_list  =   array(
            'wf_shipping_usps:flat_rate_box_priority',
            'wf_shipping_usps:flat_rate_box_express',
            'wf_shipping_usps:D_FIRST_CLASS',
            'wf_shipping_usps:D_EXPRESS_MAIL',
            'wf_shipping_usps:D_STANDARD_POST',
            'wf_shipping_usps:D_MEDIA_MAIL',
            'wf_shipping_usps:D_LIBRARY_MAIL',
            'wf_shipping_usps:D_PRIORITY_MAIL',
            'wf_shipping_usps:I_EXPRESS_MAIL',
            'wf_shipping_usps:I_PRIORITY_MAIL',
            'wf_shipping_usps:I_GLOBAL_EXPRESS',
            'wf_shipping_usps:I_FIRST_CLASS',
            'wf_shipping_usps:I_POSTCARDS',         
        );

    // Basically, below code will reset shipping services if the shipping
    // state is other than defined states_list.
    if ( !in_array(WC()->customer->shipping_state, $states_list) ) {
        foreach ( $eligible_services_for_states_list as &$value ) {
            unset( $available_shipping_methods[$value] );       
        }
    }

    return $available_shipping_methods;
}
希望能有帮助