首页 文章

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

提问于
浏览
2

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

  • 如果订单总额> 100€,则隐藏某种送货方式(仅限一种) .

  • 如果选择了本地取件,则隐藏货到付款方式 .

有谁知道这是怎么做到的吗?我有Code Snippets插件,所以我可以轻松添加任何自定义代码 .

谢谢!

2 回答

  • 1

    要隐藏基于购物车总额的特定送货方式,您可以使用下面的代码段 . 您需要在代码中更新送货方式名称 .

    Disable shipping method as per cart total

    在主题的 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;
    
    }
    

    Disable Payment Gateway For Specific Shipping Method

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

    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;
    
    }
    
  • 2

    这些方面的东西:

    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 属性来找到您需要放入的字符串 .

相关问题