仅在Woocommerce的结帐页面中启用了送货方式 [英] Shipping methods only enabled in checkout page on Woocommerce

查看:36
本文介绍了仅在Woocommerce的结帐页面中启用了送货方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce上,我们需要从购物车"部分中删除送货方式,仅将其添加到结帐"页面.

On Woocommerce, we need to remove the shipping-methods from the Cart section an add it to the Checkout page only.

任何跟踪或帮助都应该受到感激吗?

Any track or help should be really appreciated?

推荐答案

根据为什么?"有多种方法可以做到这一点.并在为什么?"您需要这个:

There will be multiple ways to do it depending on the "why?" and on "for what?" you need this:

1)隐藏购物车中与运输相关的信息-最简单的方法;

add_filter( 'woocommerce_cart_ready_to_calc_shipping', 'disable_shipping_on_cart' );
add_filter( 'woocommerce_cart_needs_shipping', 'disable_shipping_on_cart' );
function disable_shipping_on_cart( $enabled ){
    return is_checkout() ? true : false;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试和工作.

Code goes in function.php file of your active child theme (or active theme). Tested and work.

但是它不会从会话中删除运输方式(或运输包装) ...


2)除结帐页面外,所有地方都删除所有运输方式(和运输包装) :


2) Remove all shipping methods (and shipping packages) everywhere except in checkout page:

// Shipping methods
add_filter( 'woocommerce_package_rates', 'keep_shipping_methods_on_checkout', 100, 2 );
function keep_shipping_methods_on_checkout( $rates, $package ) {
    if ( ! is_checkout() ) {
        // Loop through shipping methods rates
        foreach( $rates as $rate_key => $rate ){
            unset($rates[$rate_key]); // Remove
        }
    }
    return $rates;
}

// Shipping packages
add_filter( 'woocommerce_shipping_packages', 'keep_shipping_packages_on_checkout', 20, 1 );
add_filter( 'woocommerce_cart_shipping_packages', 'keep_shipping_packages_on_checkout', 20, 1 );
function keep_shipping_packages_on_checkout( $packages ) {
    if ( ! is_checkout() ) {
        foreach( $packages as $key => $package ) {
            WC()->session->__unset('shipping_for_package_'.$key); // Remove
            unset($packages[$key]); // Remove
        }
    }
    return $packages;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试和工作.

Code goes in function.php file of your active child theme (or active theme). Tested and work.

它将从购物车和 WC_Session 删除所有运输方式所有运输包装.

相关注册的 WC_Session 数据将类似于:

The related registered WC_Session data will be something like:

WC_Session_Handler Object
(
    [_data:protected] => Array
        (
            [previous_shipping_methods] => a:1:{i:0;a:3:{i:0;s:16:"free_shipping:10";i:1;s:12:"flat_rate:14";i:2;s:15:"local_pickup:13";}}
            [shipping_method_counts] => a:1:{i:0;i:3;}
            [chosen_shipping_methods] => a:1:{i:0;s:16:"free_shipping:10";}
        )
)

没有运输包装...

对于以前已经购买过商品的客户,它将仅保留以前的运输方式和以前选择的运输方式.

It will only keep the previous shipping methods and the previous chosen shipping method for customers that have already purchased something before.

这篇关于仅在Woocommerce的结帐页面中启用了送货方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆