【问题标题】:how to limit on woocommerce product buying [closed]如何限制woocommerce产品购买[关闭]
【发布时间】:2021-02-15 08:35:14
【问题描述】:

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

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

【问题讨论】:

  • 您添加到问题中的代码确实没有任何意义。这就是为什么我将您的问题视为“为我请求编写代码”的原因。 纯代码编写请求和/或建议是 Stack Overflow 上的 off-topic,我们希望这里的问题与特定的编程问题相关。请阅读How do I ask a good question?Stack Overflow question checklist
  • 如果我知道,我会自己写代码,这里不用问了。
  • 在尝试时被卡住我不知道,谁能帮我做这件事是有区别的。正如我之前提到的,stackoverflow 不适合此类请求

标签: php wordpress woocommerce


【解决方案1】:
    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 );


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-03
    • 2011-04-28
    • 2017-09-30
    • 1970-01-01
    • 2016-11-19
    • 2019-08-16
    • 1970-01-01
    • 2019-09-13
    相关资源
    最近更新 更多