Deprecated: Return type of HM\BackUpWordPress\CleanUpIterator::accept() should either be compatible with FilterIterator::accept(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/charlott/ourfragileplanet.com/site/wp-content/plugins/backupwordpress/classes/class-path.php on line 455
Woocommerce: Alert at checkout when specific category is in basket - Our Fragile Planet

Woocommerce: Alert at checkout when specific category is in basket

I needed an alert at the checkout to warn people that an item in their basket had to be collected. To set the conditional alert at the checkout when specific category is in basket I added the following code to my theme’s functions.php I could only get this to work for a single category though.

At the end it says return has_term( 30,’product_cat’, get_post( $product_id ) ); in that the number 30 is the category ID.


/**
 * Add the field to the checkout
 **/
add_action( 'woocommerce_before_order_notes', 'sdc_custom_checkout_field' );



function sdc_custom_checkout_field( $checkout ) {

	if( check_product_category() ){

		echo '<div id="my_custom_checkout_field"><h3>' . __( '<span class="red">Message e.g. This order contains the category that I am alerting you about</span>' . '</h3><br>');

				echo '</div>';

	}

}



function check_product_category(){

	foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

		$product_id   = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );	

		if( is_category_in_cart( $product_id ) ){

			return  true;		

		}

	}

    return false;

}

	

function is_category_in_cart( $product_id ){

	return has_term( 30,'product_cat', get_post( $product_id ) );

}
Scroll to Top