I’ve made a customized theme the place individuals can reserve an occasion. 95% of the individuals bought no issues. the 5% bought issues once they add it to thier cart.
After they click on on reserve, an ajax name is made the place a brand new product is created and added to thier cart. After that is carried out, the ajax name will get a response of success: true. the 5% will get the popup, however the cart is empty.
What have I attempted:
- Init the woocommerce session when the person is just not registered
- Debug on a number of locations inside the code, no success.
- Cookies are enabled on the prospects
On the debug, the product is created and added to the cart, I am getting a hash response from the add_to_cart operate.
That is my add to cart operate:
public operate add_to_cart_virtuagym() {
error_log("Making an attempt so as to add product with ID: " . $_POST['id'] . ' dienst: ' . $_POST['dienst']);
if(isset($_POST['id']) && isset($_POST['dienst'])) :
$occasions = get_transient('virtuagym');
$title = "Reservering";
$title_to_check = "";
$max = 1;
$total_max_places = 1;
$attendees = 0;
$begin = "";
$finish = "";
foreach($occasions as $occasion) {
if($event->event_id == $_POST['id']) {
$total_max_places = $event->max_places;
$attendees = $event->attendees;
$max = $event->max_places - $event->attendees;
$begin = $event->begin;
$finish = $event->finish;
$title = $event->title . ' ' . date("d-m-Y H:i", strtotime($begin));
$title_to_check = $event->title;
}
}
if(!$this->is_event_reservated($_POST['id'], $total_max_places, $attendees) && $begin != "") {
$product = new WC_Product_Simple();
$product->set_name( $title );
$product->set_slug( 'reservering' );
$product->set_catalog_visibility("hidden");
$value = 0;
if(get_field( 'virtuagym_price', $_POST['dienst'] )) {
$value = get_field( 'virtuagym_price', $_POST['dienst'] );
}
if ( have_rows( 'virtuagym_evenementen', $_POST['dienst'] ) ) :
whereas ( have_rows( 'virtuagym_evenementen', $_POST['dienst'] ) ) : the_row();
if($title_to_check == get_sub_field( 'titel' )) {
$value = get_sub_field( 'prijs' );
}
endwhile;
endif;
error_log("Merchandise value: " . $value);
if($value == 0) {
wp_send_json_error(["message" => "Er is iets mis gegaan met het ophalen van de prijs. Probeer het nog een keer."]);
}
$product->set_regular_price( floatval($value) );
$product->set_manage_stock(true);
$product->set_stock_quantity(1);
$product->save();
update_post_meta($product->get_id(), 'virtuagym_id', $_POST['id']);
update_post_meta($product->get_id(), 'expiration', date("Y-m-d H:i", strtotime("+10 minutes")));
update_post_meta($product->get_id(), 'begin', $begin);
update_post_meta($product->get_id(), 'finish', $finish);
update_post_meta($product->get_id(), 'max_places', $max);
error_log("Creating product: " . $begin . ' Title: ' . $title . ' And ID: ' . $_POST['id']);
$consequence = WC()->cart->add_to_cart( $product->get_id(), 1 );
error_log("Cart consequence: " . $consequence);
error_log("Cart rely: " . WC()->cart->get_cart_contents_count());
if(get_transient($_POST['id'])) {
$mixed = array_merge(get_transient($_POST['id']), [$product->get_id()]);
set_transient($_POST['id'], $mixed, 600);
} else {
set_transient($_POST['id'], [$product->get_id()], 600);
}
wp_send_json_success();
} else {
wp_send_json_error(["message" => "Alle plekken zijn helaas al gereserveerd."]);
}
endif;
wp_send_json_error(["message" => "No ID provided."]);
die;
}
My JS code:
$(doc).on('click on', '.reserve_event', operate(e) {
var button = $(this);
var allbuttons = $('.reserve_event');
if($('.reserve_event').attr('data-clicked') === 'false') {
$('.reserve_event').attr('data-clicked', 'true');
var information = {
'motion': 'add_to_cart_virtuagym',
'id': $(this).information('id'),
'dienst': params.page_id
};
button.html('Laden...');
$.ajax({ // you may as well use $.put up right here
url : params.ajax_url, // AJAX handler
information : information,
kind : 'POST',
success : operate( information ) {
console.log(information);
if(information.success) {
renew_cart();
}
if(!information.success) {
button.dad or mum().discover('.occupied').html(information.information.message);
button.html('reserveren');
}
allbuttons.attr('data-clicked', 'false');
}
});
}
});
Hope somebody acknowledge this downside and bought an answer!