PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 4, 2022

[FIXED] Hide WooCommerce product if already purchased by logged in user

 March 04, 2022     hook-woocommerce, php, product, woocommerce, wordpress     No comments   

Issue

I am creating an LMS using LearnDash + WooCommerce. So far so good. Now, I'd like to hide a Product when the logged-in user has already purchased it to a) prevent the user from purchasing it twice, and b) so that I can display a product grid for them without the already purchased item(s).

Another example: if a user who purchased ITEM A goes to the shop, ITEM A should not even be displayed for them.

Thanks so much!


Solution

So, I ended up with a nice workaround:

On the product page, I created a CUSTOM FIELD where I can input the COURSE ID.

Then, with PHP, I check the Product and retrieve the custom field for the COURSE ID.

LearnDash has a shortcode for students... so after retrieving the course ID, I can display a message/button for users who have registered already for this course.

This is the code:

<?php
    global $product;
    $postid = $product->get_id();
    $courseid = get_post_meta( $product->get_id(), 'ID_del_curso', true );
    $html = '"]<center><div class="alreadyPurchased"><i class="fas fa-graduation-cap paddingRightM"></i> Ya te has registrado</div></center>';
    $openIT = '[student course_id="';
    $closeIT = '[/student]';
    echo do_shortcode( $openIT . $courseid . $html . $closeIT ); 
?>

Hope it helps!



Answered By - Marcio Vieyra
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing