Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 限制用户在woocommerce中再次购买特定产品_Wordpress_Woocommerce - Fatal编程技术网

Wordpress 限制用户在woocommerce中再次购买特定产品

Wordpress 限制用户在woocommerce中再次购买特定产品,wordpress,woocommerce,Wordpress,Woocommerce,我有一个产品ID数组=[21,82]…如果用户已经购买了该产品,我想阻止用户再次购买该产品。是否可以在woocommerce中实现此功能?您可以尝试此功能 function sv_disable_repeat_purchase( $purchasable, $product ) { // Enter the ID of the product that shouldn't be purchased again $non_purchasable = 21;

我有一个产品ID数组=
[21,82]
…如果用户已经购买了该产品,我想阻止用户再次购买该产品。是否可以在woocommerce中实现此功能?

您可以尝试此功能

    function sv_disable_repeat_purchase( $purchasable, $product ) {
        // Enter the ID of the product that shouldn't be purchased again
        $non_purchasable = 21;

        // Get the ID for the current product (passed in)
        $product_id = $product->is_type( 'variation' ) ? $product->variation_id : 
    $product->id;

        // Bail unless the ID is equal to our desired non-purchasable product
        if ( $non_purchasable != $product_id ) {
            return $purchasable;
        }

        // return false if the customer has bought the product
        if ( wc_customer_bought_product( wp_get_current_user()->user_email, 
            get_current_user_id(), $product_id ) ) {
            $purchasable = false;
        }

        // Double-check for variations: if parent is not purchasable, then 
        //variation is not
    if ( $purchasable && $product->is_type( 'variation' ) ) {
        $purchasable = $product->parent->is_purchasable();
    }

    return $purchasable;
    }
    add_filter( 'woocommerce_variation_is_purchasable', 'sv_disable_repeat_purchase', 10, 
    2 );
    add_filter( 'woocommerce_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );

你可以试试这个功能

    function sv_disable_repeat_purchase( $purchasable, $product ) {
        // Enter the ID of the product that shouldn't be purchased again
        $non_purchasable = 21;

        // Get the ID for the current product (passed in)
        $product_id = $product->is_type( 'variation' ) ? $product->variation_id : 
    $product->id;

        // Bail unless the ID is equal to our desired non-purchasable product
        if ( $non_purchasable != $product_id ) {
            return $purchasable;
        }

        // return false if the customer has bought the product
        if ( wc_customer_bought_product( wp_get_current_user()->user_email, 
            get_current_user_id(), $product_id ) ) {
            $purchasable = false;
        }

        // Double-check for variations: if parent is not purchasable, then 
        //variation is not
    if ( $purchasable && $product->is_type( 'variation' ) ) {
        $purchasable = $product->parent->is_purchasable();
    }

    return $purchasable;
    }
    add_filter( 'woocommerce_variation_is_purchasable', 'sv_disable_repeat_purchase', 10, 
    2 );
    add_filter( 'woocommerce_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );

另一种方法..只需使用过滤器禁用PlaceOrder按钮

add_filter( 'woocommerce_order_button_html', 'replace_order_button_html', 10, 2 );
function replace_order_button_html( $order_button ) {

if ( is_user_logged_in() ) {
global $product;
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
$customer_email = $current_user->user_email;
$product_id= 182; 
if ( wc_customer_bought_product($customer_email,$user_id,$product_id)) {

    $order_button_text = __( "Max volume reached", "woocommerce" );

    $style = ' style="color:#fff;cursor:not-allowed;background-color:#999;"';
    return '<a class="button alt"'.$style.' name="woocommerce_checkout_place_order" id="place_order" >' . esc_html( $order_button_text ) . '</a>';
}else{
    return $order_button;
}

}}
add_filter('woocommerce_order_button_html','replace_order_button_html',10,2);
函数replace\u order\u button\u html($order\u button){
如果(用户是否已登录){
全球$产品;
$user\u id=get\u current\u user\u id();
$current_user=wp_get_current_user();
$customer\u email=$current\u user->user\u email;
$product_id=182;
if(wc_客户_购买的_产品($customer_email、$user_id、$product_id)){
$order_button_text=_uuuuuuuuuuuuuuuuuuuu(“达到的最大数量”,“woocommerce”);
$style='style=“color:#fff;光标:不允许;背景色:#999;”;

返回“

另一种方法..只需使用过滤器禁用PlaceOrder按钮

add_filter( 'woocommerce_order_button_html', 'replace_order_button_html', 10, 2 );
function replace_order_button_html( $order_button ) {

if ( is_user_logged_in() ) {
global $product;
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
$customer_email = $current_user->user_email;
$product_id= 182; 
if ( wc_customer_bought_product($customer_email,$user_id,$product_id)) {

    $order_button_text = __( "Max volume reached", "woocommerce" );

    $style = ' style="color:#fff;cursor:not-allowed;background-color:#999;"';
    return '<a class="button alt"'.$style.' name="woocommerce_checkout_place_order" id="place_order" >' . esc_html( $order_button_text ) . '</a>';
}else{
    return $order_button;
}

}}
add_filter('woocommerce_order_button_html','replace_order_button_html',10,2);
函数replace\u order\u button\u html($order\u button){
如果(用户是否已登录){
全球$产品;
$user\u id=get\u current\u user\u id();
$current_user=wp_get_current_user();
$customer\u email=$current\u user->user\u email;
$product_id=182;
if(wc_客户_购买的_产品($customer_email、$user_id、$product_id)){
$order_button_text=_uuuuuuuuuuuuuuuuuuuu(“达到的最大数量”,“woocommerce”);
$style='style=“color:#fff;光标:不允许;背景色:#999;”;

return'

您好,谢谢您的回答!我有一系列的产品。请协助case@LatheeshVMVilla如果你有一系列产品,你必须为每个循环添加,并完成此功能。好的,只是测试一下。是否有可能显示它是如何使用的。上面的Hi代码会大大减慢我的网站速度。任何可能的解决方案你好,谢谢回答!我有一系列的产品。请协助case@LatheeshVMVilla如果你有一系列的产品,你必须为每个循环添加一个循环,并完成这个功能。好的,只是测试一下。是否有可能展示它是如何使用的。上面的Hi代码会让我的网站非常慢。有什么可能的解决方案吗