Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.0 kB
1
Indexable

add_action('wp_loaded', function(){

	if (is_object( WC()->cart )){

		$total = 0;
		foreach(WC()->cart->get_cart() as $cart_item){
			$total+=$cart_item['line_total'];
		}

		if ($total > 1000){

			// If go to checkout page, redirect to cart
			add_action( 'template_redirect', 'b2bking_checkout_redirect_to_cart', 100 );

			// Hide proceed to checkout button
			remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 ); 

			// Hide cart totals entirely
			remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cart_totals', 10 );
			add_action('woocommerce_before_cart_totals', function(){
				ob_start();
			});
			add_action('woocommerce_after_cart_totals', function(){
				$discard = ob_get_clean();
			});
		}
		
	}
});

function b2bking_checkout_redirect_to_cart(){
	// only for checkout
    if ( ! is_checkout() ) return; 

    if (is_wc_endpoint_url( 'order-received' )) return;

    wp_redirect( get_permalink( wc_get_page_id( 'cart' ) ) ); // redirect to cart.
    
    exit();
}
Leave a Comment