简体   繁体   English

在woocommerce中将“统一费率”运输方式设置为默认值

[英]Set “flat rate” shipping method as default in woocommerce

I have a woocommerce website and I have set 2 shipping methods:我有一个 woocommerce 网站,我设置了 2 种运输方式:
- Flat Rate - 统一费率
- Local pickup - 当地接送

I would like to set the "Flat rate" shipping method as default (selected) in the cart or checkout page.我想在购物车或结帐页面中将“统一费率”运输方式设置为默认(已选择)。

Any help should be appreciated.任何帮助都应该受到赞赏。

1) You can use the following code (to set "flat rate" shipping method as default) In cart page: 1)您可以在购物车页面中使用以下代码(将“统一费率”运输方式设置为默认值):

add_action( 'woocommerce_before_cart', 'set_default_chosen_shipping_method', 5 );
function set_default_chosen_shipping_method(){
    //
    if( count( WC()->session->get('shipping_for_package_0')['rates'] ) > 0 ){
        foreach( WC()->session->get('shipping_for_package_0')['rates'] as $rate_id =>$rate)
            if($rate->method_id == 'flat_rate'){
                $default_rate_id = array( $rate_id );
                break;
            }

        WC()->session->set('chosen_shipping_methods', $default_rate_id );
    }
}

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

Tested and Works in WooCommerce 3+在 WooCommerce 3+ 中测试和工作


2) You can also reorder the shipping rates in your shipping zones settings (but it doesn't really works as the last chosen shipping method take the hand) . 2) 您还可以在您的运输区域设置中重新排列运输费率(但它并不能真正起作用,因为最后选择的运输方式是自动的)

You could use the following code to set 'any' shipping method as default.您可以使用以下代码将“任何”运输方式设置为默认值。

function reset_default_shipping_method( $method, $available_methods ) {
    $default_method = 'wf_fedex_woocommerce_shipping:FEDEX_GROUND'; //provide the service name here
    if( array_key_exists($method, $available_methods ) )
        return $default_method;
    else
        return $method;
}

Let's say, you're using a Carrier shipping plugin like WooCommerce FedEx Shipping Plugin .假设您使用的是承运人运输插件,例如WooCommerce FedEx Shipping Plugin You can fetch the value Id (shown below) and paste it under the '$default_method' in the above code.您可以获取值 Id(如下所示)并将其粘贴到上述代码中的“$default_method”下。

You will have to copy and paste the code in WordPress Dashboard->Appearance–>Editor–>functions.php of your theme.您必须将代码复制并粘贴到主题的WordPress Dashboard->Appearance->Editor->functions.php中。

Hope that helped.希望有所帮助。 :) :)

copy the value Id from here从这里复制值 Id

暂无
暂无

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

相关问题 在 WooCommerce 中为“统一费率”运输方式添加工具提示 - Add tooltip to “flat rate” shipping method in WooCommerce WooCommerce:如何设置默认的表价运输方式? - WooCommerce: How to set a default table-rate shipping method? 基于 Woocommerce 中项目数量的渐进式统一运费方法 - Progressive Flat rate shipping method based on item quantity in Woocommerce 如果在 Woocommerce 中应用了特定的优惠券,则更改统一费率运输方式成本 - Change Flat rate shipping method cost if a specific coupon is applied in Woocommerce 删除WooCommerce 2.6和3+中特定类别的运输统一费率方法 - Remove shipping Flat Rate method for particular Category in WooCommerce 2.6 and 3+ 在 Woocommerce 中为所选产品禁用特定的统一费率运输方式 - Disable a specific of flat rate shipping method for a selected product in Woocommerce 当 Woocommerce 提供免费送货时,仅禁用统一费率送货方式 - Disable only flat rate shipping method when free shipping is available in Woocommerce 当 WooCommerce 中提供免费送货时,仅禁用特定的统一费率送货方式 - Disable only specific flat rate shipping method when free shipping is available in WooCommerce 在 WooCommerce 中将“免费送货”方式设置为选定的默认送货选项 - Set "free shipping" method as selected default shipping option in WooCommerce 当 Woocommerce 提供免费送货服务时,隐藏运费统一费率” - Hide shipping Flat rate" when free shipping is available in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM