如何限制客户购买一种带有woocommerce的产品

I want that customer can purchase only one product from woo commerce whenever they came back to shop page they will be redirected to my account page.

  <?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)) {
    $redirect = $myaccount;
}

i am using the following code placed in loop folder but its not working.

i want that user can purchase product once in lifetime note - its not about purchasing one product at one time its like if someone purchased the product Then he/she will never able to purchase any other product.

The loop/add-to-cart.php template creates a link. Your template only lists some variables (where are $redirect and $myaccount defined?) and does not create a link so it doesn't do anything.

A better solution would be to filter the link that is created in the loop/add-to-cart.php template via the woocommerce_loop_add_to_cart_link filter. This way if the item hasn't been purchased you can leave the regular link in tact.

Add the following to your theme's functions.php file:

add_filter( 'woocommerce_loop_add_to_cart_link', 'so_add_to_cart_link', 10, 2 );
function so_add_to_cart_link( $link, $product ){

    $current_user = wp_get_current_user();

    if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->id)) {
        $link = sprintf( '<a href="%s" rel="nofollow" class="button product_type_%s">%s</a>',
            esc_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ),
            esc_attr( $product->product_type ),
            __( 'View my account', 'theme-text-domain' ),
        ),
    }

    return $link;
}

Note that the above hasn't been tested, so be wary of typos.

Open your theme functions.php, and put below code at the end.

add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );

function woo_custom_add_to_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();

return $cart_item_data;
}

Now you can test by adding new product to your existing cart with items, see whether it will added the latest product only and remove all previous products in the cart.