允许特定商品只能由用户在Woocommerce中购买一次

时间:2019-03-21 19:56:00

标签: php arrays wordpress woocommerce product

以下用于Woocommerce的代码,允许在购买后再次购买

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

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

请有人帮我将$ non_purchasable = 40021更改为一系列产品

2 个答案:

答案 0 :(得分:1)

这是您的带有数组的代码。请检查它是否满足您的要求:

function sv_disable_repeat_purchase( $purchasable, $product ) {

    // Enter the ID of the product that shouldn't be purchased again
    $non_purchasable_arr = array(
        40021,
        40022
    );

    // 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
    foreach ( $non_purchasable_arr as $non_purchasable ) {
        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 :(得分:1)

更新 (已删除过时的不需要$product->variation_id…)

您可以简单地使用PHP条件函数in_array()…而且,您的代码已过时:

  • 这已经过时了$product->variation_id
  • $product->id替换为$product->get_id()

这是简化的重新访问的代码版本:

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(40021, 40038, 40171);

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

代码在您的活动子主题(或活动主题)的function.php文件上。经过测试,可以正常工作。