WooCommerce

Set Minimum total purchase amount

add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {
    // HERE Set minimum cart total amount
    $minimum_amount = 200;
    // Total (before taxes and shipping charges)
    $cart_subtotal = WC()->cart->subtotal;
    // Add an error notice is cart total is less than the minimum required
    if( $cart_subtotal < $minimum_amount  ) {
        // Display an error message
        wc_add_notice( '<strong>' . sprintf( __("A minimum total purchase amount of %s is required to checkout."), wc_price($minimum_amount) ) . '</strong>', 'error' );
    }
}

Update Cart automatically on quantity change

add_action( 'wp_head', function() {
	?><style>
	.woocommerce button[name="update_cart"],
	.woocommerce input[name="update_cart"] {
		display: none !important;
	}</style><?php
	
} );
add_action( 'wp_footer', function() {
	
	?><script>
	jQuery( function( $ ) {
		let timeout;
		$('.woocommerce').on('change', 'input.qty', function(){
			if ( timeout !== undefined ) {
				clearTimeout( timeout );
			}
			timeout = setTimeout(function() {
				$("[name='update_cart']").trigger("click"); // trigger cart update
			}, 1000 ); // 1 second delay, half a second (500) seems comfortable too
		});
	} );
	</script><?php
	
} );

Add “Continue Shopping” button on Cart page

add_action( 'woocommerce_before_cart_table', 'woo_add_continue_shopping_button_to_cart' );
function woo_add_continue_shopping_button_to_cart() {
 $shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) );
 
 echo '<div class="woocommerce-message">';
 echo ' <a href="'.$shop_page_url.'" class="button">Continue Shopping →</a> Would you like some more goods?';
 echo '</div>';
}

Remove Order Notes in checkout page

add_filter( 'woocommerce_enable_order_notes_field', '__return_false', 9999 );
add_filter( 'woocommerce_checkout_fields' , 'remove_order_notes' );
function remove_order_notes( $fields ) {
unset($fields['order']['order_comments']);
return $fields;
}

Change “$0.00” to “Free”

add_filter( 'woocommerce_get_price_html', 'ecommercehints_change_zero_price_display', 10, 2 );
function ecommercehints_change_zero_price_display( $price, $product ) {
if (empty($product->get_price()) || $product->get_price() == 0) { // If price is not entered or set to 0
$price = __( 'Free', 'woocommerce' );
}
return $price;
}

Apply a coupon depending on cart weight 

add_action( 'woocommerce_before_cart', 'snipjar_apply_coupon_cart_weight' );

function snipjar_apply_coupon_cart_weight() {
	$coupon_code = 'snipdeal';
	// if cart content weight is more than 15kg
	if( 15 < WC()->cart->get_cart_contents_weight() ) {
		if( ! WC()->cart->has_discount( $coupon_code ) ) {
			WC()->cart->apply_coupon( $coupon_code );
		}
	} else {
		WC()->cart->remove_coupon( $coupon_code );
		WC()->cart->calculate_totals();
	}
}

Apply a coupon depending on cart subtotal 

add_action( 'woocommerce_before_cart', 'snipjar_apply_coupon_cart_subtotal' );

function snipjar_apply_coupon_cart_subtotal() {
	$coupon_code = 'snipdeal';
	// if cart subtotal is more than 10,000
	if( 10000 < WC()->cart->get_subtotal() ) {
		if( ! WC()->cart->has_discount( $coupon_code ) ) {
			WC()->cart->apply_coupon( $coupon_code );
		}
	} else {
		WC()->cart->remove_coupon( $coupon_code );
		WC()->cart->calculate_totals();
	}
}

Apply a coupon depending on a number of products in the cart

add_action( 'woocommerce_before_cart', 'shipjar_apply_coupon_number_of_products' );

function shipjar_apply_coupon_number_of_products() {
	$coupon_code = 'snipdeal';
	// if there are more than 5 different products in the cart
	if( 5 < count( WC()->cart->get_cart() ) ) {
		if( ! WC()->cart->has_discount( $coupon_code ) ) {
			WC()->cart->apply_coupon( $coupon_code );
		}
	} else {
		WC()->cart->remove_coupon( $coupon_code );
		WC()->cart->calculate_totals();
	}
}

Apply a coupon if there is a specific product in cart 

When a customer purchases a specific product you may want to provide a specific discount to all the products in the cart automatically.

add_action( 'woocommerce_before_cart', 'snipjar_apply_coupon_specific_product' );

function snipjar_apply_coupon_specific_product() {
	$coupon_code = 'snipdeal';
	$product_id = 678;
	// if product in the cart
	if( in_array( $product_id, array_column( WC()->cart->get_cart(), 'product_id' ) ) ) {
		if( ! WC()->cart->has_discount( $coupon_code ) ) {
			WC()->cart->apply_coupon( $coupon_code );
		}
	} else { // if product removed from cart we remove the coupon
		WC()->cart->remove_coupon( $coupon_code );
		WC()->cart->calculate_totals();
	}
}