Allow Free shipping If there is only a specific product type or if subtotal reaches a minimal amout

I have a script that create a progress bar to show “Spend another XX € for free shipping!” based on a minimal amount to be spent, but I would like to avoid this text if there are ONLY products in cart from a specific product type. This because people could also buy an “Appointment” product, that are not physical products, but are under the custom product type “Appointment”.

Here is my current code:

add_filter( 'woocommerce_package_rates', 'ecommercehints_free_shipping_for_specific_products', 10, 2 );
function ecommercehints_free_shipping_for_specific_products($rates, $package, $order) {
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Get an instance of the WC_Product Object from the WC_Order_Item_Product
        $product = $item->get_product();
        $cart_total = WC()->cart->get_subtotal();

        if( $product->is_type('Appointment')  ){
            echo 'You've unlocked free shipping!';
        } else if ( $cart_total < 50 ) {
            unset( $rates['free_shipping:21'] ); // The shipping method radio button value
        }
        break; // We stop the loop and keep the first item
    }
    return $rates;
}   

// Show 'Spend another X amount' on checkout.
add_filter( 'woocommerce_checkout_before_order_review', 'ecommercehints_checkout_page_progress_bar', 10 );
function ecommercehints_checkout_page_progress_bar() {
    $cart_total = WC()->cart->get_subtotal();
    $cart_remaining = 50 - $cart_total;
    $product = $item->get_product();

    if ($cart_total < 50 ) {
        echo 'Spend another ' . get_woocommerce_currency_symbol() . $cart_remaining . ' for free shipping!<br><progress id="freeshippingprogress" max="50" value="'.$cart_total.'"></progress>';
    } else if ( $product->is_type('Appointment') {
        echo 'You've unlocked free shipping!';
    }      
}

There are some mistakes in your code like:

  • the $order variable doesn’t exist for woocommerce_package_rates filter hook.
  • Here you need to check cart items, but not order items.

Also, you need to be sure that “Appointment” is a valid product type. In general, product types are always in lowercase, so I used “appointment” (in lowercase) below.

Try the following revised code:

// Conditional function that checks if there are only "appointment" product types in cart
function has_only_appointments() {
    $others_found = false; // Initializing

    // Loop through cat items
    foreach ( WC()->cart->get_cart() as $item ) {
        if ( ! $item['data']->is_type('appointment') ) {
            $others_found = true;
            break;
        }
    }
    return !$others_found;
}

// Filter shipping  methods
add_filter( 'woocommerce_package_rates', 'ecommercehints_free_shipping_for_specific_products', 10, 2 );
function ecommercehints_free_shipping_for_specific_products( $rates, $package ) {
    // check if there are only "appointment" product types in cart
    if( has_only_appointments()  ){
        return $rates;
    }
    
    // If there is shippable products remove free shipping if cart subtotal is under specific amount 
    if ( WC()->cart->get_subtotal() < 50 ) {
        unset( $rates['free_shipping:21'] ); // The targeted Free shipping method rate ID
    }
    return $rates;
}

// Show 'Spend another X amount' on checkout.
add_filter( 'woocommerce_checkout_before_order_review', 'ecommercehints_checkout_page_progress_bar', 10 );
function ecommercehints_checkout_page_progress_bar() {
    $subtotal = WC()->cart->get_subtotal();

    // check if there are only "appointment" product types in cart or If cart subtotal is up to 50
    if( has_only_appointments( WC()->cart->get_cart() ) || $subtotal >= 50 ){
        echo __("You've unlocked free shipping!", "woocommerce");
    } elseif  ( $subtotal < 50 ) {
        printf( __('Spend another %s for free shipping!', 'woocommerce'), wc_price( 50 - $subtotal ) );
        printf('<br><progress id="freeshippingprogress" max="50" value="%d"></progress>', intval($subtotal) );
    }    
}

It should work…

Important: Empty your cart to refresh the shipping methods cached data.


Addition (for virtual products):

Now as your custom product type “Appointments” is not a “Physical” product, it should be “virtual” by default…

So you can try the following instead:

// Conditional function that checks if there are only virtual products in cart
function has_only_virtual_products() {
    $physical_found = false; // Initializing

    // Loop through cat items
    foreach ( WC()->cart->get_cart() as $item ) {
        if ( ! $item['data']->is_virtual() ) {
            $others_found = true;
            break;
        }
    }
    return !$others_found;
}

// Filter shipping  methods
add_filter( 'woocommerce_package_rates', 'ecommercehints_free_shipping_for_specific_products', 10, 2 );
function ecommercehints_free_shipping_for_specific_products( $rates, $package ) {
    // check if there are only virtual products in cart
    if( has_only_virtual_products()  ){
        return $rates;
    }
    
    // If there is shippable products remove free shipping if cart subtotal is under specific amount 
    if ( WC()->cart->get_subtotal() < 50 ) {
        unset( $rates['free_shipping:21'] ); // The targeted Free shipping method rate ID
    }
    return $rates;
}

// Show 'Spend another X amount' on checkout.
add_filter( 'woocommerce_checkout_before_order_review', 'ecommercehints_checkout_page_progress_bar', 10 );
function ecommercehints_checkout_page_progress_bar() {
    $subtotal = WC()->cart->get_subtotal();

    // check if there are only virtual products in cart or If cart subtotal is up to 50
    if( has_only_virtual_products() || $subtotal >= 50 ){
        echo __("You've unlocked free shipping!", "woocommerce");
    } elseif  ( $subtotal < 50 ) {
        printf( __('Spend another %s for free shipping!', 'woocommerce'), wc_price( 50 - $subtotal ) );
        printf('<br><progress id="freeshippingprogress" max="50" value="%d"></progress>', intval($subtotal) );
    }    
}

It could work…

Important: Empty your cart to refresh the shipping methods cached data.

2

You should go through your code line and fix few thigs like ” $order variable doesn’t exist for woocommerce_package_rates filter hook.”
that should be on a label line

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật