User generated A+ options into MarketPress for coupon codes

I'm working on a site to connect local students with tutors, and have been working with MarketPress and Appointments+, all within BuddyPress, where we are using Groups as "subjects" (this will be rewritten when finalized).

Out first goal was to be able to book appointments (either in home or in studio) with a specific A+ service provider, when the student is viewing that tutor's profile. With a little help from WPMU, we managed to do so.

The "integrate A+ with MarketPress" option works, as we have coupon codes working when a user selects their service provider and other options via the MarketPress generated "product page".

We'd essentially like to get the MP checkout experience to take over directly from the Profile page. See the code from the following thread.

https://premium.wpmudev.org/forums/topic/appointments-calendar-on-buddypress-profile-pages

Here's the existing A+ code in the functions.php

function tab_template_app_schedule() {
	add_action( 'bp_template_content', 'screen_content_app_schedule' );
	bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );

}
function screen_content_app_schedule(){
echo do_shortcode( '[app_services worker="'.bp_displayed_user_id().'" autorefresh="1"]' );
echo do_shortcode( '[app_schedule worker="'.bp_displayed_user_id().'" logged=" " title="My Schedule from START to END"]' );
echo do_shortcode( '[app_pagination]' );
echo do_shortcode( '[app_confirmation]' );
echo do_shortcode( '[app_paypal]' );
}

add_action( 'bp_setup_nav', 'my_setup_nav', 25 );

The issue is that this code keeps all stages of the booking within A+, so there's no option to insert a coupon code. I've seen multiple threads that mention A+ doesn't support coupon codes directly, so I'm wondering if there's a way to move a new booking into a MarketPress "product" page, somewhere after the user has chosen their options.

Right now, once the service provider, service and time are selected, the "confirm this appointment" button appears, using the [app_confirmation] shortcode. The next shortcode is the [app_paypal].

What I'd like to do is take the user generated information (teacher/time/service) and make it a MarketPress product. If this redirects the user away to a server-generated product page, that's fine, though it would be more elegant if they could stay on the profile page.

  • Panos

    Hi David ,

    Appointments + plugin provides an addon for integrating with MarketPress. You can find this addon and activate it at admin menu > Appointments > Settings > Add-ons.

    After activating this add-on you can see some new options available at the General settings under the Payments submenu. In that page make sure you have checked the "Integrate with MarketPress" checkbox.

    If you haven't tried this add-on, I would suggest to give it a try. You can create new MarketPress products which contain A+ shortcodes.

    Hope this was helpful. Please let me know if you need further explanation on this integration or if I am missing something here :slight_smile:

    Thanks!
    Panos

  • David

    Hi,

    We do have integration with Marketpress turned on, and the coupon code does work if a user sets an appointment via the product page (in the top menu item "Book A Lesson":wink:.

    What we would like to do is use the Marketpress coupon code from the Profile page.

    A student clicks on "Tutors" in the top menu. This takes them to a list of Groups, which is what we're using to distinguish tutors of different subjects. They then select a Profile from that Group.

    When viewing the Profile of a tutor, we have created a tab called "My Schedule". Clicking on this Profile tab allows the student to choose a service and book it from there.

    See the attached screenshots for reference. We would like to be able to use a Marketpress coupon code before the student clicks on the "Please confirm 50.00 payment...." which take them to PayPal.

    We realize that A+ does not support coupon codes, so I'm wondering if there is a way to take the time/service information that the user selects, and turn that into a Marketpress item, so that we can use Marketpress coupon codes.

  • Panos

    Hey David ,

    turn that into a Marketpress item

    This requires an amount of custom coding that exceeds the purpose of our support, but even so, in order to be able to use the coupon (say that it has been added in a custom manner) it will only be "recognized" through MarketPress checkout and not through the paypal payment of Appointments. I hope I have explained what I want to say :slight_smile: Please let me know if you need further explanation on this!

    So we need to checkout/pay with MarketPress' checkout. So I was thinking of redirecting to cart.

    Have you enabled the "Auto-add appointments into cart" in Appointments > Seetings > Payments page?

    If so, appointment should be added to cart automatically and after making an appointment we can redirect user to cart page where he can add coupon and checkout with marketpress.

    You can try that by adding the following snipped in your child theme functions.php:

    add_action( 'wp_footer', 'wpmudev_redirect_to_checkout_after_app' );
    
    function wpmudev_redirect_to_checkout_after_app(){
    
    	?>
    	<script type="text/javascript">
    	jQuery( document ).ready(function(){
    		jQuery(function($){
    
    		    $(document).ajaxComplete( function( event, xhr, settings ) {
    
    		        var AjaxData =  settings.data ;
    
    		        if ( ( typeof AjaxData !== undefined ) && AjaxData.indexOf("pre_confirmation") >= 0 ) {
    
    		            document.location="http://your-site/store/cart/";
    
    		        }
    
    		    });
    
    		});
    	});
    
    	</script>
    
    	<?php
    }

    Instead of
    wp_footer
    you could also try
    bp_enqueue_scripts

    Let me know how you think of this :slight_smile:

    Thanks!
    Panos

  • Panos

    Hey ddavidblackmore_1 ,

    I managed to replicate this, and indeed appointment won't get added to cart since it's not a product page nor an appointments page.

    Could you please try out the following snipped:

    add_filter('app-appointment-appointment_created', 'wpmudev_appointment_created_from_bp_profile', 1, 7);
    
    function wpmudev_appointment_created_from_bp_profile( $additional, $insert_id, $post_id, $service, $worker, $start, $end ){
    
    	if (bp_is_user()){
    
    		$post_id = 106;
    
    		$variation = wpmudev_app_add_variation_from_bp($insert_id, $post_id, $service, $worker, $start, $end);
    		if (empty($variation)) return $additional;
    
    		$cart = MP_Cart::get_instance();
    		$items = $cart->get_items();
    		if (is_array($items) && false === array_search($variation, array_keys($items))) {
    
    			$cart->add_item($variation);
    		}
    		$additional['mp'] = 1;
    		$additional['variation'] = $variation;
    
    		return $additional;
    
    	}
    
    	return $additional;
    }
    
    function wpmudev_app_add_variation_from_bp ($app_id, $post_id, $service, $worker, $start, $end) {
    
    		global $appointments;
    
    		$variation_id = wp_insert_post(array(
    			'post_parent' => $post_id,
    			'post_title' => get_the_title($post_id),
    			'post_status' => 'publish',
    			'post_type' => MP_Product::get_variations_post_type(),
    		), false);
    		if (empty($variation_id)) return false;
    
    		$price = false;
    		$raw_price = apply_filters('app_mp_price', $appointments->get_price( true ), $service, $worker, $start, $end);
    		if (function_exists('filter_var') && defined('FILTER_VALIDATE_FLOAT') && defined('FILTER_FLAG_ALLOW_THOUSAND')) {
    			$price = filter_var($raw_price, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);
    		}
    		if (false === $price) {
    			$price = str_replace(',', '', $raw_price);
    		}
    
    		update_post_meta($variation_id, 'name', $app_id);
    		update_post_meta($variation_id, 'sku', $appointments->service);
    		update_post_meta($variation_id, 'regular_price', $price);
    
    		return $variation_id;
    	}

    Paste it in your child theme's functions.php. Replace the number 106 with the id of your appointments page. It works smoothly on my side but I would like to know how it works for you :slight_smile:

    Thanks!
    Panos

  • David

    Thanks very much!

    I changed the post id to the proper number and it's partially working...

    When the student selects the time, they're presented with the "Please Check Appointment Details and Confirm Below" box (see attached).

    When they click the "Please click here..." button (highlighted in red in the image), the Appointment IS added to the cart, and the coupon can be used, so that's very good.

    Is it possible to redirect the student TO their shopping cart when they click on the "Please click here..." button?

  • Panos

    Hi David ,

    The js I attached in my previous response for redirection is using the wrong ajax hook. Please try adding the following snipped in your child theme's functions.php:

    add_action( 'wp_footer', 'wpmudev_redirect_to_checkout_after_app' );
    
    function wpmudev_redirect_to_checkout_after_app(){
    
    ?>
    <script type="text/javascript">
    jQuery( document ).ready(function(){
    	jQuery(function($){
    
    	$(document).ajaxComplete( function( event, xhr, settings ) {
    
    		var AjaxData = settings.data;
    
    		if ( ( typeof AjaxData !== undefined ) && AjaxData.indexOf("post_confirmation") >= 0 ) {
    
    		document.location="http://your-site/store/cart/";
    
    		}
    
    		});
    
    	});
    });
    
    </script>
    
    <?php
    }

    It should redirect after the confirmation button is being clicked :slight_smile:

    Thanks!
    Panos

  • David

    Our coupon codes are not working after we migrated our site to a new server, see the note below from Doug, he says it may be related to the appointments page.
    Can you help us solve this issue as it is crucial to have coupons to test and generate new students?

    David

    Let them know that we’ve migrated the site to a new server, and to the best of our knowledge it stopped working after that point.
    If they ask for access to the admin area on the site, you can do so by going to the following link....

    https://tootyr.com/wp-admin/admin.php?page=wpmudev-support

    and clicking on the button on the right that says “Grant Support Access”.
    Doug

  • Panos

    Hi David ,

    Apologies for delay here!

    Just to be clear, issue is that the MarketPress coupons are not adding a discount?

    Also, I followed the link but it doesn't have that button. Could you please send in admin and ftp access so I could have a closer look? You can send that privately through our contact form: https://premium.wpmudev.org/contact/#i-have-a-different-question

    Send in:Subject: "Attn: Panos Lyrakis"

    - Admin login:
    Admin username
    Admin password
    Login url

    - FTP credentials
    host
    username
    password
    (and port if required)

    - Page url where this can be replicated

    - link back to this thread for reference

    You can also reply here once you sent that info so I get notified faster.

    Thanks!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.