0

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...

  • The `wc_customer_bought_product()` function checks for the following order (paid) statuses 'processing' & 'completed'. So if an order does not contain this status it will normally return false. You can of course always adjust the function to only check for 'completed' orders. Do you think this would solve your problem? – 7uc1f3r Nov 05 '21 at 15:22
  • it would be a great idea! Thanks! But unfortunately, I'm not a developer. How can I change Woocommerce `wc_customer_bought_product ()` function? Maybe I should copy it and make some changes on purpose so that it is not overwritten when Woocommerce updates? Sorry for the silly questions, but I'm not very familiar with Wordpress development. – Carlo Alberto Nov 05 '21 at 16:16
  • See: [target only Orders with a complete status](https://stackoverflow.com/a/53051339/11987538) answer code – 7uc1f3r Nov 05 '21 at 16:24

0 Answers0