在 WooCommerce 中隐藏运输和付款方式

Hide shipping and payment methods in WooCommerce

我正在建立一个 WooCommerce 电子商店,我需要通过执行以下操作来调整我的结帐页面:

  1. 如果订单总额 > 100 欧元,则隐藏某种送货方式(只有一种)。

  2. 如果选择本地取货,则隐藏货到付款方式。

有人知道怎么做吗?我有代码片段插件,因此我可以轻松添加任何自定义代码。

谢谢!

  1. 有很多插件可以为您做这件事,看看这个 WooCommerce Conditional Shipping and Payments
  2. 您需要参与 "woocommerce_payment_gateways" 行动

大致如下:

function alter_payment_gateways( $gateways ){

    $chosen_rates = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();

    if( in_array( 'local-pickup:6', $chosen_rates ) ) {
        $array_diff = array('cod');
        $list = array_diff( $list, $array_diff );
    }

    return $list;
}

add_action('woocommerce_payment_gateways', 'alter_payment_gateways', 50, 1);

第 4 行 'local-pickup' 末尾的数字将取决于您的 woocommerce 设置。您可以在此处找到需要放入的字符串,方法是将一些东西添加到购物篮,转到结帐,右键单击交付方式中的 "Local Pickup" 选项并查看 value 属性.

要根据购物车总数隐藏特定的送货方式,您可以使用以下代码片段。您需要在代码中更新您的送货方式名称。

根据购物车总数禁用送货方式

将此代码段添加到您主题的 functions.php 文件或自定义插件文件中。

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

function shipping_based_on_price( $rates, $package ) {

    $total = WC()->cart->cart_contents_total;
    //echo $total;
    if ( $total > 100 ) {

        unset( $rates['local_delivery'] ); // Unset your shipping method

    }
    return $rates;

}

禁用特定运输方式的支付网关

使用下面的代码片段。根据您的付款方式和送货方式更新代码。

add_filter( 'woocommerce_available_payment_gateways', 'x34fg_gateway_disable_shipping' );

function x34fg_gateway_disable_shipping( $available_gateways ) {

    global $woocommerce;

    if ( !is_admin() ) {

        $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );

        $chosen_shipping = $chosen_methods[0];

        if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
            unset( $available_gateways['cod'] );
        }

    }

return $available_gateways;

}