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

时间:2018-10-29 01:48:11

标签: wordpress woocommerce

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

2 个答案:

答案 0 :(得分: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 );

答案 1 :(得分:0)

另一种方法..只是使用过滤器禁用下订单按钮

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

}}

感谢Loic:https://stackoverflow.com/a/50222706/3697484