• Updating from woocommerce 2.6.* to 3 broke something on cart page as follows:
    I have a custom function checking cart items, as some cannot be shipped to certain destinations. I add a notice with wc_add_notice(‘text’, ‘error’) to prevent checkout until the item is removed. Clicking the red x to remove the item does not trigger an update of the div containing the notice/error/success message, which still appears on screen on top of the “item remove, undo?”. Applying a coupon triggers correctly the div update and the deletion of messages. I assume this is AJAX related, as dequeueing wc_cart solves the issue.
    Function code as follows (mostly copied from the interwebs, works fine with previous version of woocommerce!):

    function blocco_checkout() {
    	
    	// set the slug of the category for which we disallow checkout
    	$cat1 = 'test';
    	
    	// get destination state from shipping calculation form
    	$paese = WC()->customer->get_shipping_country();
    	$paesi_no = array('AU','CA','JP','IT');//da integrare?
    	//unico $stato non accettato al momento è Utah, quindi non importa fare array
    	
    	
    	// get the product category
    	$prod_cat1 = get_term_by( 'slug', $cat1, 'product_cat' );
    	
    	// sanity check to prevent fatals if the term doesn't exist
    	if ( is_wp_error( $prod_cat1 ) ||  is_wp_error( $prod_cat2 ) ||  is_wp_error($prod_cat3) ) {
    		return;
    	}
    	
    	$cat1_name = '<a href="' . get_term_link( $cat1, 'product_cat' ) . '">' . $prod_cat1->name . '</a>';
    	
    	// check if this category is in the cart
    	if ( prod_non_spedibili( $cat1 ) && $paese == 'IT' ) {		
    		// render a notice to explain why checkout is blocked
    		//wc_add_notice( sprintf( 'Hi there! Looks like your cart contains products from the %1$s category &ndash; unfortunately they cannot be shipped to your destination.', $cat1_name ), 'error' );
    		wc_add_notice( sprintf( 'Hi there! Looks like your cart contains products from the %1$s category &ndash; unfortunately they cannot be shipped to your destination.', $cat1_name ), 'error' );
    	}
    	
    }
    add_action( 'woocommerce_check_cart_items', 'blocco_checkout' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter nick76

    (@nick76)

    theme used: storefront. issue persists with twentyseventeen and the custom function

    Thread Starter nick76

    (@nick76)

    apparently changing the action to
    add_action( 'woocommerce_before_cart_table', 'blocco_checkout' );
    and using

    echo '<div class="woocommerce-error">';
    echo '<p>'.'Hi there! Looks like your cart contains products from the '.$cat1_name.' category; unfortunately they cannot be shipped to your destination.'.'</p>';
    echo '</div>';
    		remove_action('woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);

    instead of wc_add_notice solves the problem. Will keep on testing, hope this helps someone.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Dismiss notices on item removal’ is closed to new replies.