woocomerce限制只能购买一次

Published
June 6, 2022
浏览次数 :  0


// posted by Robin Scott https://silicondales.com/tutorials/woocommerce-tutorials/woocommerce-tutorial-allow-users-purchase-items/ - prevent user adding item to cart if they bought it before...
add_filter('woocommerce_add_to_cart_validation','sd_bought_before_woocommerce_add_to_cart_validation',20, 2);
function sd_bought_before_woocommerce_add_to_cart_validation($valid, $product_id){
    $current_user = wp_get_current_user();
    if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id)) {
        wc_add_notice( __( 'ERROR MESSAGE GOES HERE SORRY YOU CANNOT BUY ANYTHING TWICE OKAY!!', 'woocommerce' ), 'error' );
        $valid = false;
    }
    return $valid;
}

粘贴到functions.php

方法2

/**
* @snippet       Deny second purchase
* @how-to        Get CustomizeWoo.com FREE
* @author        Rodolfo Melogli
* @testedwith    WooCommerce 6
* @donate $9     https://businessbloomer.com/bloomer-armada/
*/

add_filter( 'woocommerce_is_purchasable', 'bbloomer_deny_purchase_if_already_purchased', 9999, 2 );
 
function bbloomer_deny_purchase_if_already_purchased( $is_purchasable, $product ) {
   if ( is_user_logged_in() && wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
      $is_purchasable = false;
   }
   return $is_purchasable;
}

Top