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.