Prevent WooCommerce checkout if cart only contains "Sold individually" items

Viewed 263

I am using this code, so users can add a product with a custom price (via url) to the cart. Also this makes them "Sold Individually"

add_filter( 'woocommerce_add_cart_item' , 'set_woo_prices');
add_filter( 'woocommerce_get_cart_item_from_session',  'set_session_prices', 20 , 3 );
function set_woo_prices( $woo_data ) {
  if ( ! isset( $_GET['agcs'] ) || empty ( $_GET['agcs'] ) ) { return $woo_data; }
  $woo_data['data']->set_price( $_GET['agcs'] );
  $woo_data['data']->set_sold_individually('true');
  $woo_data['my_price'] = $_GET['agcs'];
  
  return $woo_data;
}

function  set_session_prices ( $woo_data , $values , $key ) {
    if ( ! isset( $woo_data['my_price'] ) || empty ( $woo_data['my_price'] ) ) { return $woo_data; }
    $woo_data['data']->set_price( $woo_data['my_price'] );
     $woo_data['data']->set_sold_individually('true');
    return $woo_data;
}

Because I am using this for upsells. I need a check that would dissalow checkout if cart contained only "Sold seperatly" items.

I used to do it with the check "If cart is less than 10$ then dissallow checkout"

add_action( 'woocommerce_check_cart_items', 'pokazimiga' );
function pokazimiga() {

    if( is_cart() || is_checkout() ) {
        global $woocommerce;
        $minimum_cart_total = 9;
        $total = WC()->cart->subtotal;
        
        
        
        if( $total <= $minimum_cart_total  ) {
            wc_add_notice( sprintf( '<strong>Nakup, manjši od %s %s ni mogoč</strong>'
                .'<br />Trenutna vrednost košarice: %s %s',
                $minimum_cart_total,
                get_option( 'woocommerce_currency'),
                $total,
                get_option( 'woocommerce_currency') ),
            'error' );
        }
    }
}

It would suffice if all items in cart are set_sold_individually('true'), then do not allow checkout.

1 Answers

The following code prevents checkout, if the cart contains only "Sold seperatly" items.

Explanation via comment tags added in the code:

function action_woocommerce_check_cart_items() {
    // Flag
    $flag = false;
    
    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {
        // Cart contains an item that is not sold individually
        if ( ! $cart_item['data']->get_sold_individually() ) {
            // Flag becomes true
            $flag = true;
            
            // Break loop
            break;
        }
    }
    
    // When the flag is still false & the cart is NOT empty
    if ( ! $flag && ! WC()->cart->is_empty() ) {
        // Clear all other notices          
        wc_clear_notices();

        // Avoid checkout displaying an error notice
        wc_add_notice( __( 'My custom error message', 'woocommerce' ), 'error' );
        
        // Optional: remove proceed to checkout button
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );   
    }
}   
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );
Related