将 WooCommerce 虚拟产品排除在免费送货之外

eMi*_*sen 2 php wordpress woocommerce

在我的 WordPress/WooCommerce 网站上,我们为所有超过 500 丹麦克朗的订单提供免费送货服务。这工作得很好。我们制作了礼品卡作为虚拟产品,因为我们以数字方式发送所有产品。问题是由于某种原因,虚拟产品计入 500 丹麦克朗的免费送货限额。

我们试图做的是将所有虚拟产品排除在免费送货限制之外。

我不知道该怎么做。我们的网站是: http: //detitalienskekoekken.dk.linux99.unoeuro-server.com

它的设置方式是我们有一个提供各种交付方法的插件。其中一种方法(送货到商店)的费用为 49 丹麦克朗,但达到 500 丹麦克朗后,您可以通过此特定选项免费送货。

我无法在网上找到此代码,因此很抱歉没有提供此代码。

Loi*_*tec 6

更新您的特定插件:

add_filter('woocommerce_package_rates', 'custom_free_shipping_option', 15, 2 );
function custom_free_shipping_option($rates, $package){

    // HERE set the "minimum order amount" for free shipping
    $limit = 500;

    $free_total = 0;

    // Get the cart content total excluding virtual products
    foreach( WC()->cart->get_cart() as $cart_item )
        if( ! $cart_item['data']->is_virtual( ) )
            $free_total += $cart_item['line_total'];

    // Set the cost to 0 based on specific cart content total
    if( $free_total > $limit )
        foreach ( $rates as $rate_key => $rate )
            if( 'pakkelabels_shipping_gls' === $rate->method_id )
                $rates[$rate->id]->cost = 0;

    return $rates;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。

它应该适用于您的特定运输方式。


原始答案 (对于经典的“免费送货”方法)

以下是替换送货方式免费送货“最低订单金额”选项(不包括虚拟产品)的必要代码:

add_filter('woocommerce_package_rates', 'custom_free_shipping_option', 10, 2 );
function custom_free_shipping_option($rates, $package){

    // HERE set the "minimum order amount" for free shipping
    $limit = 500;

    $free_total = 0;

    // Get the cart content total excluding virtual products
    foreach( WC()->cart->get_cart() as $cart_item )
        if( ! $cart_item['data']->is_virtual( ) )
            $free_total += $cart_item['line_total'];

    // Disabling free shipping method based on specific cart content total
    if( $free_total < $limit )
        foreach ( $rates as $rate_key => $rate )
            if( 'free_shipping' == $rate->method_id )
                unset( $rates[ $rate_key ] );

    return $rates;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。

已测试并工作

因此,您甚至可以在免费送货设置中删除此选项。