Can I tie a MarketPress sale to a dynamically set post id?

I have a listing site that will allow for various upgrades to a listing. I am thinking about handling that sale of the upgrades via MarketPress. I need to be able to set a single item ex. Upgrade A. and then dynamically connect it to a post_id so when it is purchased can pull the id from the IPN and create the upgrade.

The bit I need is passing the post id to the purchase and getting it back via IPN. Is there a MarketPress filter I could use to hook into?

  • Milan

    Hello Titanium Creative

    Hope you are well today and thanks for asking us. :slight_smile:

    I am sorry but I am not exactly sure what you are asking, So could you please explain your this requirement step by step so that I can understand it well ? Please do include as much detail as you can because when its something about plugin's extensibility, we want be sure of its all aspects. :slight_smile:

    Looking forward to hearing back!
    Cheers,
    Milan

  • Titanium Creative

    Thanks for looking into this but I don't think it really answers the question. I wasn't really wondering if I could hire myself to achieve this. I was asking if there are nay hooks that would achieve this. Hooks, as I am sure you are well aware, are one of the worst documented aspects of WPMUDEV plugins.

    Based on your answer of "hire somebody or look it up yourself", I will start examining the source code to find hooks that may help.

    Sorry about being a little ranty but I have seen this sort of answer from you before and I really have come to expect, and usually get, more from my requests for help.

  • Adam Czajczyk

    Hello Titanium Creative!

    Although that will indeed require custom development I hope I may be able to give you at least some tips on this. I still however need some more explanation. I believe I get the basic idea but am not sure about some details.

    I imagine that the workflow here should be like this:

    1. Customer creates a post -> 2. Customer goes to the store -> 3. and buys "Upgrade" -> 4. MarketPress returns data (to be further processed by you/your custom code): user ID, product ID (indicating that "Upgrade A" was bought), post ID

    Is that correct?

    Then, what happens if a customer created more posts? Will there be more products (like different kind of updates)? How the customer is reaching the shop - I mean: customer simply "goes to the store" or is there any "semi-automated" link added to his post that redirects him to a particular product?

    Best regards,
    Adam

  • Titanium Creative

    The workflow sounds correct.
    Yes there will be other upgrade types that could be applied to the post.
    No they really don't need to be accessible via a product page per se. I will create buttons on the page or with an assignable post_id so the data can be transferred.

    An example product would be:
    Upgrade A - Add "UPGRADED" to your content! ;o) $25

    So I can set up the product. and I can
    add_filter('the_content', function($content) { return 'UPGRADED<br />'.$content; });

    Can I hook into the product or the payment in any way so I can tell the transaction it applies to a particular post_id? If not, what file handles this? I can extend it.
    something like:

    add_filter('some_unknown_hook', function($product_info_for_IPN)
         { return $product_info_for_IPN.' post_upgrade_id:$post->ID; });

    Getting the post id would depend on where the hook is in the process.

    Can I hook into the receipt to get that product id and apply it to the post?

    --OR--

    As I write this I am wondering if I am thinking about this backwards. Do I need to set up a post_meta called upgrades, and somehow edit that in the process. I think that may be the way but I still need to know how to connect the purchase to the post id.

    I suspect the process would be similar to the digital downloads but instead of downloading a specific file, I want to upgrade a specific post. And I want to do it with a generic product.

    As a work around, I suppose I could dynamically create products for each post but that will create a large data table very fast and most of the products would never be used.

    Thanks for helping me think through this. I really think properly placed hooks are the answer.

  • Adam Czajczyk

    Hello Titanium Creative!

    I reviewed MarketPress code and although there may be other solution this idea comes to my mind:

    1. Pass post ID to product

    There's a pre-defined product so it's URL is known. You could create a button in form of HTML form so clicking it would redirect user to the product page. While generating the form you could fetch post id

    $my_post_id = get_the_ID();

    and then pass it as a form's hidden input field:

    <input type="hidden" name="pass_post_id" value="<?php echo $my_post_id;?>">

    2. Store the post_id

    Now the user's on product page (upgrade page) and a post_id has been passed so this id should be stored somewhere. First, a custom page template for product page would be necessary so you could use a custom code to grab posted ID (so the value of $_POST sent via form).

    You could use PHP session to store that grabbed ID. A proposed (though not tested unfortunately) solution I posted here:

    https://premium.wpmudev.org/forums/topic/membership-plugin-fails-registration#post-1077891

    This linked post refers to slightly different scenario related to Membership 2 Pro plugin but the general idea would be similar.

    Alternatively, you could use e.g. WP Transients API:
    https://codex.wordpress.org/Transients_API

    The point would be to store post ID, user ID and product ID.

    3. The important part, I think, would be to make sure that each time user revisits the product page (upgrade page) this data (related to particular user ID) is updated; That's because if a user got e.g. two posts that could be upgraded or there are two different upgrades available for user we need and user goes to the product page but abandons purchase process -> this could get "messed up". I think though this update should be performed if (and only if) user visits the page using the button from point 1.

    4. Now user goes through the purchase process as usually, there's no need for any customization here.

    This would be the first part of the process. The second part should handle completed transaction

    I can think (with a little help of MP developers) of two ways to deal with it.

    1. Take a look please at

    do_action( 'mp_order/new_order', $this ); in class-mp-order.php

    I'm not 100% sure if this would handle all cases but in case of PayPal Express the

    $order->save()

    is called only when payment went through;

    This would be fired then by MarketPress.

    2. Alternative (I think more reliable) way would be to add action to any standard WP hook that let's you read user ID and then simply fetch orders assigned to this user ID of "paid" status (marked "paid" which happens automatically if payment successfully went through or manually if admin decided to mark order "paid").

    In both cases you could then fetch back data stored previously (in PHP session or using Transients API). As the data includes post, user and product ID you should now be able to use it to upgrade content if necessary. A note: in case of latter solution (number 2) you'd additionally want to compare product ID fetched from order with a product ID stored - in order to be sure that's the proper order :slight_smile:

    I think this all should make quite a solid foundation. I'll asked one of our devs for a little help on point 2 above (fetching order) so I'll update this thread again soon.

    Hope that helps :slight_smile:

    Have a nice day!
    Adam

  • Titanium Creative

    WOW Adam!! That is some awesome information as well as awesome effort!!

    Again, I was a little ranty earlier and am sorry for that. but, this is WAY beyond the effort I was expecting! I will look into this (I'm trying to get it completed this weekend) and let you know how it all works out.

    I think letting the user purchase the upgrade and then providing a notice that they have an unattached upgrade and asking which post they want to attach it to is simpler. Making it automatic would be more elegant and professional.

    thanks again!!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.