WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Check if a customer has bought a product (8 posts)

  1. nicolesev
    Member
    Posted 1 year ago #

    Hi,

    How would I check if a logged in user has bought a specific product and if yes, to hide that product? Basically we'd like to limit a specific product to be purchased only once by each user.

    I saw a function for checking in the API docs http://docs.woothemes.com/wc-apidocs/function-woocommerce_customer_bought_product.html But with my limited knowledge in PHP, I'm not sure how to create a function for what I need.

    Or is there an existing plugin that does this? I've tried searching and I didn't find anything.

    I'd appreciate the help, thanks! :)

    http://wordpress.org/extend/plugins/woocommerce/

  2. Roy Ho
    Member
    Posted 1 year ago #

    But they can always login as a new user and still purchase it...

  3. varktech.com
    Member
    Posted 1 year ago #

    Hi,
    This will put your feet on the path to a possible solution:
    http://wordpress.org/plugins/maximum-purchase-for-woocommerce/

  4. m3ndi3
    Member
    Posted 7 months ago #

    Hey I was looking forever for this too and I figured it out! What I did was changed the Add to cart button to "Purchased" if a person already bought it.

    1. Go into loop>add-to-cart.php
    2. Place

    $current_user = wp_get_current_user();
    $email = $current_user->email;
    if ( woocommerce_customer_bought_product( $email, $current_user->ID, $product->id)) : echo 'Purchased'; else :

    On top after

    global $product;

    3. Scroll to the very bottom and put

    <?php endif; ?>

    Below the other

    <?php endif; ?>

    This will replace all Add to cart buttons in the catalogs and similar products, to "Purchased"

    Next in individual product pages:

    1. Go into single-product> add-to-cart>each .php
    2. Same thing.. Place

    $current_user = wp_get_current_user();
    $email = $current_user->email;
    if ( woocommerce_customer_bought_product( $email, $current_user->ID, $product->id)) : echo 'Purchased'; else :

    On top after

    global $woocommerce, $product;

    3. Scroll to the very bottom and put

    <?php endif; ?>

    Below the other

    <?php endif; ?>

    If you need to show anything else depending on if a customer bought a specific product, then just use

    $current_user = wp_get_current_user();
    $email = $current_user->email;
    if ( woocommerce_customer_bought_product( $email, $current_user->ID, 'specific product id' ))

    Hope that helped you!!

  5. bowenac
    Member
    Posted 5 months ago #

    m3ndi3 does your solution no longer work? I just tried it and it seems they have updated some of the files. I changed a few things as well but it is not working.

    I will update with an updated solution if I find one.

    Thanks

  6. m3ndi3
    Member
    Posted 5 months ago #

    I haven't updated to the new Woocommerce so I'm not sure if this works anymore, sorry :/

  7. bowenac
    Member
    Posted 5 months ago #

    Updated Solution:

    Note: It is better to create a folder structure so that your changes will override the default woocommerce files.

    So create two folders, one called woocommerce and inside that folder make one called loop. Inside the loop folder create a new file called add-to-cart.php and use the code below.

    <?php
    /**
     * Loop Add to Cart
     *
     * @author 		WooThemes
     * @package 	WooCommerce/Templates
     * @version     2.1.0
     */
    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    global $product;
    $current_user = wp_get_current_user();
    
    if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->id)) {
    	echo 'Purchased';
    }
    else
    {
    echo apply_filters( 'woocommerce_loop_add_to_cart_link',
    	sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="button %s product_type_%s">%s</a>',
    		esc_url( $product->add_to_cart_url() ),
    		esc_attr( $product->id ),
    		esc_attr( $product->get_sku() ),
    		$product->is_purchasable() ? 'add_to_cart_button' : '',
    		esc_attr( $product->product_type ),
    		esc_html( $product->add_to_cart_text() )
    	),
    $product );
    }
  8. bowenac
    Member
    Posted 5 months ago #

    I decided to make a plugin for this... I made the plugin so that it could be controlled per item. So a setting shows up for each product to only allow it to be purchased once.

    http://www.pnw-design.com/product/woo-limit-one-purchase/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.