Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Ajax Add To Cart Item Meta Check WordPress

  • REFUNDED

Before items are added to the cart, I have a check the compares the submitted item meta again a certain cpt. I would like to have the same concept behind the check compare the items before adding them to the cart against the other items in the cart.

Current function:

Script: [[LINK href="http://pastie.org/private/314gzzsvjj7wabl8cjpkcw"]]http://pastie.org/private/314gzzsvjj7wabl8cjpkcw[[/LINK]]
Function: [[LINK href="http://pastie.org/private/wiohffdssy8xt8tdpgilg"]]http://pastie.org/private/wiohffdssy8xt8tdpgilg[[/LINK]]

This is what I tried to start out getting the cart meta.


add_action( 'wp_ajax_availability', 'cart_check' );
add_action( 'wp_ajax_nopriv_availability', 'cart_check' );
function cart_check($order_id) {

global $woocommerce;

$woocommerce->cart->get_item_data( $values );

foreach ($values as $item) {
$meta = array();
foreach($item['item_meta'] as $meta_field)
{
$meta[$meta_field['meta_name']]=$meta_field['meta_value'];
}

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

$php_startTime = strtotime($meta['Start Date'].' '.$meta['Start Time']);
$php_endTime = $php_startTime + $meta['Duration']*3600;

$event['EventStartDate'] = date("Y-m-d",$php_startTime);
$event['EventStartHour'] = date("h",$php_startTime);
$event['EventStartMinute'] = date("i",$php_startTime);
$event['EventStartMeridian'] = date("A",$php_startTime);

$event['EventEndDate'] = date("Y-m-d",$php_endTime);
$event['EventEndHour'] = date("h",$php_endTime);
$event['EventEndMinute'] = date("i",$php_endTime);
$event['EventEndMeridian'] = date("A",$php_endTime);


I'm not really sure how to replace the existing SQL cpt overlap check with the an item meta check. Help is much appreciated here.


Answers (2)

2013-01-10

Arnav Joy answers:

Hi ,

Can you explain what you want to check ?


Kyle comments:

Hi thanks for the reply. Yes, I am trying to check for overlap in time like in the function seen in the link above. Each item has some meta pertaining to Start Time and Duration (End Time = Start Time + Duration). None of those times should overlap with one another.

In the function above, it is checking to see if the item being added to the cart overlaps with a specific CPT, I need to do the same check, but for items already in the cart.


Arnav Joy comments:

so you want to check , if new item which is being added to the cart should not start and end at same time as any of the already added items on the cart?


Kyle comments:

Yes, plus Items should not 'overlap' so know end times need to be later than a start time and vice versa.

That is this chunk:

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}'))


2013-01-10

John Cotton answers:

Can you show us what's in the item meta? An example...?


Kyle comments:

Sure if you go here: https://hookahi.com/guides/hucks-charters/ you can see a reservation form. From there on the cart page you can see the meta (give me a minute to strip the appearance filters). Is that what you were asking about?


John Cotton comments:

Are you saying that you want to ensure that the cart doesn't contain two bookings with times that overlap?


Kyle comments:

Yes, exactly


John Cotton comments:

What code do you use to store the date/time when an item is added to the cart?


Kyle comments:

That is done through the Woocommerce Gravity Forms add-on.

Do you need to see the core php file from that? It would probably be best to email that if so


John Cotton comments:

Comparing time clashes is always a pain, but assuming you store the start time in the item meta then something like this will work:

<blockquote>That is done through the Woocommerce Gravity Forms add-on.</blockquote>
So you'll need to mod the following to



add_action( 'wp_ajax_availability', 'cart_check' );
add_action( 'wp_ajax_nopriv_availability', 'cart_check' );
function cart_check($order_id) {

global $woocommerce;

$Start_Time = strtotime($_POST['date'].' '.$_POST['hour'].':'.$_POST['minute']);
$End_Time = $Start_Time + $$_POST['Duration']*3600;

foreach($woocommerce->cart->cart_contents as $item) {
$item_start = $item['add_on']['start_time']; // modify to reflect where you actually store this value...
$item_end = $item_start + $item['add_on']['duration']; // modify to reflect where you actually store this value...

if(
( $item_start >= $Start_Time && $item_start <= $End_time)
|| ( $item_end >= $Start_Time && item_end <= $End_time)
|| ( $item_start < $Start_Time && item_end >= $Start_Time)
|| ( $item_start > $Start_Time && item_end >= $End_time)
) {
// do what you need to do when they clash
}
}
}


Kyle comments:

I am not sure what to do with "do what you need to do when they clash", or how to then tie that to the ajax in the script.

Also "modify to reflect where you actually store this value..." I was hoping that was in the code above. Is it not?


John Cotton comments:

<blockquote>I am not sure what to do with "do what you need to do when they clash", or how to then tie that to the ajax in the script</blockquote>
Well, it's up to you - return an error, show a message, whatever you want to happen if they have something else in their cart! For example you could return a message and display that:

echo 'You already a booking at that time!'; exit();


<blockquote>Also "modify to reflect where you actually store this value..." I was hoping that was in the code above. Is it not?</blockquote>
It's not and without the seeing your code I can't know where it's being put.....