Ask your WordPress questions! Pay money and get answers fast! (more info)

Extra Cart Item Check before Checkout (Woocommerce)


I am having trouble attaching errors to cart items after running a final check on the checkout page. Idea is to do the check once more before final checkout, to see if the items became unavailable while they sat in the customers cart.

Goal: Foreach cart item do overlap check. If fails, attach a class to the tr.cart_table_item.

! Code has been re-editted from original, same problem tho

add_action('woocommerce_proceed_to_checkout', 'second_availability_check');

global $wpdb, $woocommerce;

foreach( $woocommerce->cart->get_cart() as $item_id => $values ) {

if (isset($values['variation_id']) && $values['variation_id'] > 0) :
$_product = new WC_Product_Variation( $values['variation_id'] );
else :
$_product = new WC_Product( $values['product_id'] );

$product_meta = apply_filters( 'woocommerce_get_item_data', array(), $values );

foreach($product_meta as $meta_field )
$meta[$meta_field['name']] = $meta_field['value'];

$Start_Time = strtotime($meta['Start Date'].' '.$meta['Start Time']);
$End_Time = $Start_Time + $meta['Duration']*3600 + (7*24*3600);

$guides_id = $meta['Guide ID'];
$date = $meta['Start Date'];
$hour = date( 'g' , $meta['Start Time'] );
$minute = date( 'i' , $meta['Start Time'] );
$ampm = date( 'a' , $meta['Start Time'] );
$duration = $meta['Duration'];

$args = array( 'author' => $guides_id, 'post_type' => 'guides', 'post_status' => array('draft', 'publish') );
$posts = new WP_Query( $args );
$guide_id = $posts->posts[0]->ID;

$guide = get_post($guide_id);
$buffer = get_post_meta($guide_id, 'wpcf-buffer', true);
$buffer = $buffer?$buffer:0;

$php_startTime = strtotime($date.' '.$hour.':'.$minute.' '.$ampm) - $buffer*60;
$php_endTime = $php_startTime + $duration*3600 + $buffer*60;

if ($php_startTime > time()) {
$startDate = date('Y-m-d H:i:s', $php_startTime);
$endDate = date('Y-m-d H:i:s', $php_endTime);

$overlap = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} a
JOIN {$wpdb->postmeta} b ON a.ID = b.post_id
JOIN {$wpdb->postmeta} c ON a.ID = c.post_id
JOIN {$wpdb->postmeta} d ON a.ID = d.post_id
WHERE a.post_type = 'tribe_events'
AND (a.post_status = 'payment-complete'
OR a.post_status = 'publish'
OR a.post_status = 'personal'
OR a.post_status = 'finalized'
OR a.post_status = 'holding'
OR a.post_status = 'time-block' )
AND d.meta_key = 'wpcf-xguide_id'
AND d.meta_value = {$guide->post_author}
AND b.meta_key = '_EventStartDate'
AND c.meta_key = '_EventEndDate'
AND ((c.meta_value > '{$startDate}' AND c.meta_value < '{$endDate}')
OR (b.meta_value > '{$startDate}' AND b.meta_value < '{$endDate}')
OR (b.meta_value < '{$startDate}' AND c.meta_value > '{$endDate}')
OR (b.meta_value = '{$startDate}')
OR (c.meta_value = '{$endDate}')
OR (b.meta_value = '{$endDate}'))
$ok = ($overlap>0?0:1);
if ($ok ==0){ ///some sort of response with cart item #? }

else $ok = 0;

//check if there were any responses sent if so{ wp_redirect('');



So what I need now is the two holes in the function above fixed.

The second part of this is part of what Gabriel said below. He offered a hook that should help, but I have no idea how to filter it. The idea of the response code above is to be used in junction with the hook he offered, but I do not know how to use it

something like:

add_action('woocommerce_before_cart_contents', 'cart_item_final_check');
function cart_item_final_check(){

foreach cart item if response add_filter('woocommerce_cart_table_item_class');


Answers (1)


Gabriel Reguly answers:

Hi ktrusak,

Seems to me that you are using the wrong action, <strong>woocommerce_before_cart_contents</strong> would be a better option.

Then combine it with filter <strong>woocommerce_cart_table_item_class</strong> to attach you desired class.

Have a look here: [[LINK href=""]][[/LINK]]


Kyle comments:

Hi Gabriel, thank you for the reply

I think that could work, and I can turn the above function into a cart redirect in the event the user tries to skip the cart page and go directly to checkout.

Can you help me with the code for <strong>woocommerce_cart_table_item_class</strong>

Gabriel Reguly comments:

Hi ktrusak,

I am on vacation now, but next thursday I can help you.