Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter truongdang

    (@truongdang)

    What I’ve found so far in
    assets/js/features/event-area/ui.js

    Is the EATicketSelection which can be used to print out the while ticket-selectform, it automatically uses the event/post ID for construction and it creates all event-listeners for adding/updating and removing events on the form.

    What I really would like to know is which kind of ajax call can be used to push an event to the cart with JS/PHP or ajax when I have an event-id.

    Plugin Author loushou

    (@loushou)

    Hey @truongdang ,

    As you have already figured out, adding a ticket programmatically is a two part process. One part is adding the ticket product to the cart, and another part is making that ticket associated to an event. The easiest way to do this is to leverage the ‘qsot-zoner-reserve-current-user’ filter, which has two functions that run on it. You can used it by doing something like this:

    $success = apply_filters( 'qsot-zoner-reserve-current-user', false, $event_id, $ticket_product_id, $quantity );

    The two functions that run on this filter are:

    // file: inc/core/zoner.class.php
    // purpose: adds the record to the seat associations table
    qsot_zoner::reserve_current_user()

    // file: inc/venues/event-area/pricing.class.php
    // purpose: adds the ticket item to the cart, with the appropriate event association
    qsot_seat_pricing::reserve_current_user()

    If you need to accomplish something more advanced, then take a look at what those functions do, and model any custom thing you do after those functions.

    Hopefully this helps,
    Loushou

    Thread Starter truongdang

    (@truongdang)

    Thanks for the help. This was exactly the lead I needed to accomplish this.

    What I did, I created a new function based on reserve_current_user.

    I searched with my own wp sql query which event+productid combination I want to add to the cart. The function is placed an ajax/php file, which I can call from jquery/javascript.

    Plugin Author loushou

    (@loushou)

    @truongdang – Sweet! Glad I could point you in the right direction!

    Loushou

    Thread Starter truongdang

    (@truongdang)

    For posterity’s sake. These are the functions I created and used:
    This function searches the corresponding Woo Commerce Product ID which is attached to the event-postID (connected to the event-area, which is connectet to the woocommerce product).

    function getProductID($eventID){
    	global $wpdb;
    	$result1 = $wpdb->get_results('SELECT meta_value FROM wp_2_postmeta WHERE meta_key = "_event_area_id" AND post_ID = '.$eventID);
    	$eventereaID = $result1[0]->meta_value;
    
    	$result2 = $wpdb->get_results('SELECT meta_value FROM wp_2_postmeta WHERE meta_key = "_pricing_options" AND post_ID = '.$eventereaID);
    	$productID = $result2[0]->meta_value;
    
    	if($productID>0){
    		return $productID;
    	}
    	return false;
    }

    And this function adds an event programmatically

    function add_ticket_to_cart($eventid, $ticket_type_id, $count ){
    	global $woocommerce;
    	$data = array(
    		'event_id' => $eventid,
    	);
    
    	if ( is_object( $woocommerce->cart ) ) {
    		// Generate a ID based on product ID, variation ID, variation data, and other cart item data
    		$cart_id = $woocommerce->cart->generate_cart_id( $ticket_type_id, '', '', $data );
    
    		// See if this product and its options is already in the cart
    		$cart_item_key = $woocommerce->cart->find_product_in_cart( $cart_id );
    		if ($count == 0) {
    			if ($cart_item_key) {
    				$woocommerce->cart->set_quantity( $cart_item_key, 0 );
    			}
    		} else {
    			if ($cart_item_key) {
    				$woocommerce->cart->set_quantity( $cart_item_key, $count );
    			} else {
    				$woocommerce->cart->add_to_cart( $ticket_type_id, $count, '', '', $data );
    			}
    		}
    	}
    	return $cart_id;
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How To: programatically add a ticket with PHP?’ is closed to new replies.