如何使用function.php在WooCommerce中实现免费配送

如何使用function.php在WooCommerce中实现免费配送,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,Wordpress 4.5.2 WooCommerce 2.5.5 默认情况下,我在woocommerce中禁用了免费配送选项,并且只启用了固定费率配送 现在,如果购物车总数>=20,那么只有我想启用免费送货选项,否则使用统一费率 这是我的代码: add_action( 'woocommerce_before_cart', 'apply_free_shipping_coupon' ); function apply_free_shipping_coupon($aa) { global

Wordpress 4.5.2 WooCommerce 2.5.5

默认情况下,我在woocommerce中禁用了免费配送选项,并且只启用了固定费率配送

现在,如果购物车总数>=20,那么只有我想启用免费送货选项,否则使用统一费率

这是我的代码:

add_action( 'woocommerce_before_cart', 'apply_free_shipping_coupon' );

function apply_free_shipping_coupon($aa)
{
    global $woocommerce;
    global $cart_total_inc_vat;
    $cart_total_inc_vat =  $woocommerce->cart->get_cart_total();
    //echo $cart_total_inc_vat;
    add_filter( 'woocommerce_shipping_free_shipping_is_available', 'free_shipping_based_on_cart_shipping_class' );
    }
}

function free_shipping_based_on_cart_shipping_class( $is_available ) 
{
    $found = false;
    if($cart_total_inc_vat >=20)
    {
        $cart_items = WC()->cart->get_cart();

      foreach ( $cart_items as $cart_item )
      {
        $product = $cart_item['data'];
        $class   = $product->get_shipping_class();

        if ( 'free_shipping' === $class ) {
            $found = true;
            break;
        }
      }
      $found = true;
    }
    return $is_available && $found;
}

电子商务->设置->配送->免费配送。
您可以更改设置以允许20美元以上的免费送货。

WooCommerce->settings->shipping->free shipping。
您可以更改设置以允许20美元以上的免费送货。

Hi Gertjan,我以前使用过它,但是如果我使用折扣券,购物车<20,它不提供免费送货,这就是为什么我在发布的代码中使用这些API挂钩。Hi Gertjan,我以前使用过它,但是如果我使用折扣券,购物车<20,它不提供免费送货,这就是为什么我在我发布的代码中使用这些API挂钩。