I built an eCommerce using Woocommerce where I sell photo galleries. The products for sale are therefore the digital galleries (unlockable and visible on the product page by purchasing the product) and in download.
I thought I had solved everything by inserting a simple condition inside the single-product.php file that makes the block with the product gallery appear only if the user has that product among his orders.
global $woocommerce, $post, $product;
$current_user = wp_get_current_user();
$allowed_roles = array('administrator');
if(( array_intersect($allowed_roles, $current_user->roles ) ) || (wc_customer_bought_product( $current_user->user_email,$current_user->ID, $product->get_id() ) ) ) {
/* the code that renders the rest the content block with product gallery */
}
But I noticed that Woocommerce creates an order even if the payment was not successful. So if a user tries to buy but fails he still unlocks the gallery and this is a big problem ...
I don't really know what to do. I thought there might be two solutions:
- Make sure that woocommerce does not create or delete orders that have not been paid for successfully.
- Or have the condition evaluate whether the user has among his completed orders the product id of the page he is on and if there is, then he unlocks the gallery of the product page he is on.
But I have absolutely no idea how to do it. What do you think? Do you think there is a better solution? Thanks...