• Resolved mutagennix

    (@mutagennix)


    Hello,

    I have added my custom “checkbox” fields to “mt-fields-api.php” and left everything else default. My custom fields display below the tickets on event page and the form seems to send the correct JSON data (i.e. tickets and custom fields that are selected).

    The issue is that when the “Add to cart” button is pressed and I “Go to cart”, then go back to the event page and update my selection, the data gets added together when I press “Add to cart” again and “Go to cart” again. It seems that the cookie/user meta is not clearing and just gets accumulated.

    I tried clicking “Empty cart” link but it only removes tickets but not custom field meta and hidden input fields on cart/purchase page.

    How can I force all cookie/user meta to firstly be removed and only then send JSON data when “Add to cart” pressed?

    Thank you in advance.

    This is my custom fields code just in case (Not sure if all is correctly setup):

    add_filter( 'mt_custom_fields', 'create_custom_fields', 10, 2 );
    function create_custom_fields( $array ) {
    	$array['test_event_data1'] = array( 
    		'title'=>"Extra Option 1", 
    		'sanitize_callback'=>'sanitize_callback', 
    		'display_callback'=>'display_callback',
    		'input_type'=>'checkbox',
    		'input_values'=>'Extra Option 1',
    		'context'=>'global'
    	);
    	$array['test_event_data2'] = array( 
    		'title'=>"Extra Option 2", 
    		'sanitize_callback'=>'sanitize_callback', 
    		'display_callback'=>'display_callback',
    		'input_type'=>'checkbox',
    		'input_values'=>'Extra Option 2',
    		'context'=>'global'
    	);
    	return $array;
    }
    function display_callback( $data, $context ) {
    	return urldecode( $data );
    }
    function sanitize_callback( $data ) {
    	return esc_sql( $data );
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Joe Dolson

    (@joedolson)

    I’m not sure I follow exactly what the issue is. Can you describe what is happening using an example?

    Thread Starter mutagennix

    (@mutagennix)

    Hello Joe,

    Thank you for getting back to me.

    Here is a link to my test website “https://printnetpartners.com.au/phsanz/phsanz-asm/”. The tickets and custom fields are at the bottom of the event page.

    Quirk: For some reason all the custom field check boxes are ticked on first page load. When page is refreshed, the check boxes are not ticked.

    Not sure if this section in “mt-fields.api.php” is the reason since checkboxes have “input_values”:

    case "checkbox":
    case "radio":
    	if ( isset( $field['input_values'] ) ) {
    		$value = $field['input_values'];
    		if ( $user_value != '' ) {
    			$checked = ' checked="checked"';
    		} else {
    			$checked = '';
    		}
    		$output = "<input type='" . $field['input_type'] . "' name='$name' id='$name' value='" . esc_attr( stripslashes( $value ) ) . "'$checked />";
    	}
    	break;

    In “to mt_ajax.php”, I added the “mt_delete_data” function in front to clear the cart first since I only want one ticket to be available for purchase at a time:

    function mt_ajax_handler() {
    	mt_delete_data( 'cart' );
    
    	$options = array_merge( mt_default_settings(), get_option( 'mt_settings' ) );

    If you select a ticket and session(s) and click “Register and Pay”, you should be redirected to purchase/cart page (jQuery used for redirect). Now if you go back to event page and change your selections, then click “Register and Pay”, you will notice that the ticket in cart changes correctly but session(s) don’t update correctly. I tried in different browsers with same result.

    Are the custom fields saved to a separate cookie, if so then how do I clear cookie first and then update it when “Add to cart” or in my case “Register and Pay” button is pressed?

    Thank you in advance for your assistance.

    • This reply was modified 7 years, 4 months ago by mutagennix.
    Plugin Author Joe Dolson

    (@joedolson)

    When I visited the page, the checkboxes were not checked on first load.

    If you currently have tickets in your cart, they will be checked, because the cookie is aware that you have them in your cart, however.

    Having mt_delete_cart() in the AJAX function could cause problems; I’m not sure exactly what effect that would have – I obviously haven’t tested that, since it isn’t my code. It certainly does mean that if somebody adds a different event it would remove the first event they have.

    The cart isn’t saved as a cookie at all; it’s saved either as user meta (if a user is logged in) or as a transient option if the user is not logged in. The only cookie saved is an ID reference to locate the cart data.

    Your issue is because the five fields are individual checkboxes, rather than a checkbox group. When an individual checkbox that is un-checked is submitted, it doesn’t send an empty value; it sends nothing at all, so it isn’t being picked up as something to be removed from the data.

    What I should probably do about this is allow the creation of a custom field type, so you can generate a checkbox group *or* support checkbox and radio groups as an option in the API. Right now, I don’t have a good solution for you.

    Thread Starter mutagennix

    (@mutagennix)

    I think the tickets have the same issue as the custom fields checkbox/radio you mentioned, “When an individual checkbox that is un-checked is submitted, it doesn’t send an empty value; it sends nothing at all“. The checkbox/radio group update you mentioned would be great for a future release.

    Since I only have a single event and wanted to only allow a single ticket type, adding mt_delete_cart() was the only way I could clear the cart first before changing/submitting my ticket option when “Add to cart” is pressed. Originally I had to delete individual tickets on the purchase/cart page that I didn’t want.

    The mt_delete_cart() function I added to “function mt_ajax_handler()” deletes the tickets but does not delete/clear custom fields data.

    In the mean time, is there a way for me to delete/clear all tickets and all custom fields data first, and then submit the selected tickets and custom field data when “Add to cart” is pressed?

    • This reply was modified 7 years, 4 months ago by mutagennix.
    Plugin Author Joe Dolson

    (@joedolson)

    Yes, you can do that.

    You need to call the function ‘mt_delete_data’ with an argument that’s the name of your custom field, e.g.

    mt_delete_data( 'my-field' );

    Best,
    Joe

    Plugin Author Joe Dolson

    (@joedolson)

    Wait – sorry, that’s not quite right. It’s mt_delete_data( ‘my-field_$event_id’ );, so you’ll need to know the ID of the event in order to delete the data. The event ID is just the post ID, so that shouldn’t be difficult to obtain.

    Thread Starter mutagennix

    (@mutagennix)

    Thank you so much for pointing me in the right direction.

    This is the way I used it in “mt-fields-api.php” and it works great for my purpose.

    function mt_handle_custom_field( $saved, $submit ) {
    	$event_id = $submit['mt_event_id'];
    	mt_delete_data('test_event_data1_' . $event_id);
    	mt_delete_data('test_event_data2_' . $event_id);
    	mt_delete_data('test_event_data3_' . $event_id);
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom fields cookie/user meta’ is closed to new replies.