如何限制woocommerce产品购买

时间:2021-02-15 08:35:14

标签: php wordpress woocommerce

我有一个教育和课程销售网站。我想限制我的产品,以便每个用户可以购买每个产品一次。 购买课程后,添加到购物车按钮将被禁用,按钮文本更改为您现在是课程成员。

add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );
 
add_action('woocommerce_before_add_to_cart_quantity','prevent_repaet_buy');
function prevent_repaet_buy(){
  $user=get_current_user_id();
  global $product;
  $id = $product->get_id();
  $downloads     = WC()->customer->get_meta();
  echo "<pre>";
    print_r($downloads);
      echo "</pre>";
}

1 个答案:

答案 0 :(得分:0)

    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;
        echo '<p class="btn btn-info">you are now member of course </p>';
    }

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