-2

I am looking for the following solution: I would like to display a cart message for products that have a specific shipping class are in cart and when the cart subtotal is below $75:

  • Shipping class: "bezorgservice".
  • Minimum cart amount: 75.
  • cart Message: Order € 20,00 more to deliver your order at home with our delivery service.

Any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Justus
  • 123
  • 2
  • 10
  • It is important that it count all products with the shipping class 'bezorgservice' together. So first product is 50,- second product is 10,-. The message must be: Order € 10,- more to deliver your order at home with our delivery service. – Justus Jun 19 '18 at 07:58
  • Sorry but this question **is NOT too broad** even if the OP didn't provide any code for it. – LoicTheAztec Jul 09 '18 at 22:14

1 Answers1

3

Updated: This can be done easily with this simple custom hooked function (code is commented):

add_action( 'woocommerce_before_calculate_totals', 'cart_items_shipping_class_message', 20, 1 );
function cart_items_shipping_class_message( $cart ){
    if ( is_admin() && ! defined('DOING_AJAX') )
        return;

    ## Your settings below ##
    $shipping_class = 'bezorgservice'; // The targeted shipping class
    $min_amout      = 75; // The minimal amount

    $amout_incl_tax = 0; // Initializing variable

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ){
        // Targeting our defined shipping class only
        if( $cart_item['data']->get_shipping_class() === $shipping_class ){
            // Add each subtotal from our defined shipping class only
            $amout_incl_tax += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
        }
    }

    // Display an error notice when quantity count is below the minimum defined on cart page only
    if( $amout_incl_tax > 0 && $amout_incl_tax < $min_amout && is_cart() ){
        wc_clear_notices(); // Clear all other notices
        // Display our custom notice
        wc_add_notice( sprintf( 'Order <strong>%s</strong> more to deliver your order at home with our delivery service.',
            wc_price($min_amout - $amout_incl_tax) ), 'notice' );
    }
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399