Are orders custom post types?

Are orders in MarketPress a custom post type. I saw something the other day that indicated that they were, but I'm not sure. More importantly are they something that could be trackable by the BuddyPress Acheivements plugin?

I'm looking to get some integration between MarketPress and Acheivements (no, I don't want to use Cubepoints) to reward the sellers for sales. If orders are custom post types, then they also can theoretically be added to the activity stream too with something like, "John Smith just sold A Pink T-Shirt on the site John's Shirts." and then have Acheivements track the count and reward top sellers! That's some cool functionality is it not?

So, in WordPress lingo, what are orders? Custom post types, or something else and can they be integrated and tracked?

  • Saunt Valerian

    Turns out that yes, orders are custom post types!

    They are registered as such in marketpress.php

    Nice! So hopefully this will work. This snippet is intended to integrate orders into the activity stream. Can someone review and check it out? I think I've covered everything.

    Once I've sucessfully figured out how to get orders into the activity stream, I'll work on linking them with the Achievements plugin and then possibly release the entire workup as a new integrated plugin as BuddyPress MarketPress Achievements, but I'm not a coder, so I need more eyes on this and support to tune this up (credit of course will be given if it becomes a plugin!).

    // begin record orders to BP activity stream
    function orders_record_activity( $post_id, $post, $user_id = false ) {
    	global $bp, $wpdb;
    	//check for multisite install
    	if(!is_multisite()) return false;
    	$post_id = (int)$post_id;
    	$blog_id = (int)$wpdb->blogid;
    	if ( !$user_id )
    		$user_id = (int)$post->post_author;
    	/* This is to stop infinite loops with Donncha's sitewide tags plugin */
    	if ( (int)$bp->site_options['tags_blog_id'] == (int)$blog_id ) return false;
    	/* Don't record this if it's a regular post */
    	if ( $post->post_type != 'mp_order' ) return false;
    	if ( ( 'order_paid' == $post->post_status ) && ( '' == $post->post_password ) ) {
    		if ( (int)get_blog_option( $blog_id, 'blog_public' ) || !bp_core_is_multisite() ) {
    			/* Record this in activity streams */
    			$post_permalink = get_permalink( $post_id );
    			$activity_action = sprintf( __( '%1$s just sold %2$s, in the store: %3$s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>', get_blog_option($blog_id, 'blogname') );
    			$activity_content = $post->post_content;
    			bp_blogs_record_activity( array(
    				'user_id' => (int)$post->post_author,
    				'action' => apply_filters( 'bp_blogs_activity_new_post_action', $activity_action, &$post, $post_permalink ),
    				'content' => apply_filters( 'bp_blogs_activity_new_post_content', $activity_content, &$post, $post_permalink ),
    				'primary_link' => apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_permalink, $post_id ),
    				'type' => 'new_blog_post',
    				'item_id' => $blog_id,
    				'secondary_item_id' => $post_id,
    				'recorded_time' => $post->post_modified_gmt
    	} else
    		bp_blogs_remove_post( $post_id, $blog_id );
    	bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
    	do_action( 'bp_blogs_new_blog_post', $post_id, $post, $user_id );
    	/* check for existing entry and update if one exists */
    	$id = bp_activity_get_activity_id( array(
    		'user_id' => $user_id,
    		'component' => $component,
    		'type' => $type,
    		'item_id' => $item_id,
    		'secondary_item_id' => $secondary_item_id
    	) );
    	return bp_activity_add( array( 'id' => $id, 'user_id' => $user_id, 'action' => $action, 'content' => $content, 'primary_link' => $primary_link, 'component' => $component, 'type' => $type, 'item_id' => $item_id, 'secondary_item_id' => $secondary_item_id, 'recorded_time' => $recorded_time, 'hide_sitewide' => $hide_sitewide ) );
    add_action( 'update-order-status', 'orders_record_activity', 10, 2 );
    //end record orders to the BP activity stream
  • Saunt Valerian

    Okay, I tested it on my dev install and it didn't work.

    Note that in line 27 above I have it set to record when the status of the order goes to paid, but I wonder if it needs to be when the status is set to paid. Note that the add action at the end there is set to fire the function when update-order-status, but is that going to be correct? It looked like the correct action to hook into when I read through marketpress.php, but I'm not sure (after all, it doesn't work).

    Could it have something to do with mp_orders not being public regardless of the overall settings of the subblog?

  • Saunt Valerian

    NO, I've got it wrong, I'm pulling the wrong custom post type. I need to be pulling the product post type and recording a link to the product into the activity stream, but I need to be firing that function whenever an order for that product is created.

    Also, what happens when there is multiple products in an order, are we posting activity records for all of them? No, we shouldn't, we should be posting a record based on some criteria (highest price product, then if the products are the same price, then perhaps name?) so we only get one activity record when an order is place.

    This is obviously going to be more complex than I anticipated. I forgot about those other factors so slightly modifiying the standard code to add a CPT to the activity stream isn't going to be enough.

    I sure hope someone sees this and can chime in with some code ideas, I think this is a pretty exciting feature and would to see it working!

  • aecnu

    Greetings Saunt Valerian,

    Thank you for your idea and code snippet, it is greatly appreciated.

    The only one with the authority to go over this code snippet is the Market Press lead developer whom is currently on vacation until the 10th of July, though it is possible one of the other lead developers will take the ticket when I ping for the attention.

    I will do the applicable action now.

    Cheers, Joe

  • Paul

    @Saunt Valerian, This is excellent. I love members who attempt things on their own to get things started. Well done.

    To answer your initial question, Yes, orders in MarketPress are custom post types. You will find these are registered as the token 'mp_order'. Products are also a custom post type, 'product'. In my opinion this should probably have been 'mp_product' but that is off topic.

    So looking at your code. I do thing you need to fire on the ORDER status change and not product. Mainly because the action of adding a product to an order during the shopping process is not something you want to capture.

    I looked briefly at your code and didn't see anything out of place. Then again I'm not a BP developer. But I can tell you the action you have setup is not going to work.

    To point you in the right direction take a look at market press.php around line 3195 you will see the function update_order_status(). This function obviously handles updating the order status and some other maintenance. At the bottom of the function you can see where the WordPress function wp_transition_post_status() is called. This WordPress function was written to handle action for Posts where they transition from the post_status of draft to published or private, etc. We use the same logic for order post_status.

    I think you just need to update your add_action to be the following. You can review the WordPress wp_transition_post_status() function to see how it sets up the various actions.

    add_action("paid_mp_order", 10, 2);

    Then modify your local function to remove the user parameter.

    function orders_record_activity( $post_id, $post)

    LEt me know if this works.

  • Saunt Valerian

    Haven't had a chance to look at this more. I'm going to try your fix this week, but I've also reached out to coders on Freelancer and Elance for this. I think that logic bit when an order has more than one product is going to be problematic (unfortunately, the bids I've received so far are way to expensive for something that I'm certain can be done with nothing more than a code snippet of less than 100 lines).

    I'll test your suggestion this week and report back in a couple days, if I can get the basic part working, then I may save a step in getting this completed.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.