Woocommerce:用户只能购买一次产品 [英] Woocommerce: Users able to buy a product only once

查看:338
本文介绍了Woocommerce:用户只能购买一次产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在主题的functions.php文件中添加哪些内容,以便用户只能购买一次产品?就像他们以前曾经在商店中购买过任何产品一样,他们被禁止再次购买.

What can I add to the functions.php file of my theme that would make it so that users could only buy a product once? As in if they have ever bought any product in the store before, they are prevented from purchasing it again.

我认为代码看起来像这样(伪代码),并且会使用woocommerce_add_cart_item_data过滤器.

I imagine the code would look something like this (pseudo code) and would use the woocommerce_add_cart_item_data filter.

add_filter( 'woocommerce_add_cart_item_data', 'woo_check_not_bought_before' );

function woo_check_not_bought_before( $cart_item_data ) {

    global $woocommerce;

    // Some MYSQL to check if product was bought before

    if ($bought_before == 'true') {       
         $woocommerce->cart->empty_cart();
    }

    // Echo some text to explain why you can only buy the product once

    return $cart_item_data;
}

推荐答案

您可以使用此代码来满足您的要求.在主题中创建一个名为woocommerce的文件夹.然后在其中创建一个名为loop的文件夹.然后在其中创建一个add-to-cart.php文件并编写以下代码:

You might use this code to achieve your requirement. Create a folder called woocommerce in your theme. Then create a folder named loop in it. Then create a add-to-cart.php file in it and write the below code:

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

但是用户仍然可以建立其他帐户,然后再次购买该产品.

But the user can still make a different account and buy that product again.

这篇关于Woocommerce:用户只能购买一次产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆