【问题标题】:Allow only to purchase a product once per customer in Woocommerce [closed]在 Woocommerce 中,每位客户仅允许购买一次产品 [关闭]
【发布时间】:2019-09-13 19:09:49
【问题描述】:

我有一家销售虚拟产品的 Woocommerce 商店。如何禁止用户再次购买相同的产品?如果用户已经购买了该产品,那么他/她应该无法将该产品添加到购物车中。

【问题讨论】:

  • 这不是代码问题,所以我不会将此作为答案发布。在产品编辑页面的库存部分,选中标有“单独销售”的框
  • “库存”部分下的“单独销售”仅适用于当前购物车会话。我们只想允许每个客户购买一次产品。我已经修改了问题。

标签: php wordpress woocommerce product orders


【解决方案1】:

只需在您的子主题functions.php中添加一个函数

add_filter( 'woocommerce_variation_is_purchasable', 'products_purchasable_once', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'products_purchasable_once', 10, 2 );
function products_purchasable_once( $purchasable, $product ) {
    // Here set the product IDs in the array that can be purchased only once 
    $targeted_products = array(**ADD YOUR PRODUCT IDS HERE**);

    // Only for logged in users and not for variable products
    if( ! is_user_logged_in() || $product->is_type('variable') )
        return $purchasable; // Exit

    $user = wp_get_current_user(); // The WP_User Object

    if ( in_array( $product->get_id(), $targeted_products ) &&
    wc_customer_bought_product( $user->user_email, $user->ID, $product->get_id() ) ) {
        $purchasable = false;
    }

    return $purchasable;
}

在此部分添加您的产品 ID 在此处添加您的产品 ID 我已在代码中标记

*以下更新

function disable_repeat_purchase( $purchasable, $product ) {
    if ( $product->is_type( 'variable' ) ) {
        return $purchasable;
    }

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

    // return false statement if the customer has bought the product / variation
    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_is_purchasable', 'disable_repeat_purchase', 10, 2 );

更新 2

function purchase_disabled_message() {

    global $product;

    if ( $product->is_type( 'variable' ) ) {

        foreach ( $product->get_children() as $variation_id ) {
            // Render the purchase restricted message if it has been purchased
            if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $variation_id ) ) {
                render_variation_non_purchasable_message( $product, $variation_id );
            }
        }

    } else {
        if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $product->id ) ) {
            echo '<div class="woocommerce"><div class="woocommerce-info wc-nonpurchasable-message">You\'ve already purchased this product! It can only be purchased once per customer.</div></div>';
        }
    }
}
add_action( 'woocommerce_single_product_summary', 'purchase_disabled_message', 31 );


function render_variation_non_purchasable_message( $product, $no_repeats_id ) {

    if ( $product->is_type( 'variable' ) && $product->has_child() ) {

        $variation_purchasable = true;

        foreach ( $product->get_available_variations() as $variation ) {

            if ( $no_repeats_id === $variation['variation_id'] ) {
                $variation_purchasable = false; 
                echo '<div class="woocommerce"><div class="woocommerce-info wc-nonpurchasable-message js-variation-' . sanitize_html_class( $variation['variation_id'] ) . '">You\'ve already purchased this product! It can only be purchased once per customer.</div></div>';
            }
        }
    }

    if ( ! $variation_purchasable ) {
        wc_enqueue_js("
            jQuery('.variations_form')
                .on( 'woocommerce_variation_select_change', function( event ) {
                    jQuery('.wc-nonpurchasable-message').hide();
                })
                .on( 'found_variation', function( event, variation ) {
                    jQuery('.wc-nonpurchasable-message').hide();
                    if ( ! variation.is_purchasable ) {
                        jQuery( '.wc-nonpurchasable-message.js-variation-' + variation.variation_id ).show();
                    }
                })
            .find( '.variations select' ).change();
        ");
    }
}

【讨论】:

  • 请问如何适用于所有产品?不指明产品 ID
  • 您是允许购买其他产品还是在客户购买您的一种产品后完全锁定所有产品
  • 它应该只允许每个用户购买一次特定产品。因此,如果用户已经购买了该产品,他/她应该无法将其添加到购物车
  • 更新了我的答案,这没有经过测试,但应该可以解决问题
  • 谢谢。如何在单个产品页面中将“您已购买此产品”的消息集成到它?
猜你喜欢
  • 2019-08-12
  • 1970-01-01
  • 1970-01-01
  • 2022-07-11
  • 1970-01-01
  • 2014-11-15
  • 2019-05-01
  • 2019-05-07
  • 1970-01-01
相关资源
最近更新 更多