Viewing 7 replies - 1 through 7 (of 7 total)
  • Roy Ho

    (@splashingpixelscom)

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

    Hi,
    This will put your feet on the path to a possible solution:
    https://www.ads-software.com/plugins/maximum-purchase-for-woocommerce/

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

    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

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

    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 );
    }

    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.

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

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Check if a customer has bought a product’ is closed to new replies.