I have a Woocommerce shop selling virtual products. How can I disable users from buying the same product again? If the user bought the product already then he/she shouldn't be able to add this product to the cart.
1条回答 默认 最新
- dongqiang8683 2019-04-24 13:41关注
Simply add a functions in your child theme functions.php
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(**ADD YOUR PRODUCT IDS HERE**); // 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; }
add your product ids in this section ADD YOUR PRODUCT IDS HERE i have marked in code
*UPDATE BELOW
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; } // 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 );
Update 2
function purchase_disabled_message() { global $product; if ( $product->is_type( 'variable' ) ) { foreach ( $product->get_children() as $variation_id ) { // Render the purchase restricted message if it has been purchased if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $variation_id ) ) { render_variation_non_purchasable_message( $product, $variation_id ); } } } else { if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $product->id ) ) { echo '<div class="woocommerce"><div class="woocommerce-info wc-nonpurchasable-message">You\'ve already purchased this product! It can only be purchased once per customer.</div></div>'; } } } add_action( 'woocommerce_single_product_summary', 'purchase_disabled_message', 31 ); function render_variation_non_purchasable_message( $product, $no_repeats_id ) { if ( $product->is_type( 'variable' ) && $product->has_child() ) { $variation_purchasable = true; foreach ( $product->get_available_variations() as $variation ) { if ( $no_repeats_id === $variation['variation_id'] ) { $variation_purchasable = false; echo '<div class="woocommerce"><div class="woocommerce-info wc-nonpurchasable-message js-variation-' . sanitize_html_class( $variation['variation_id'] ) . '">You\'ve already purchased this product! It can only be purchased once per customer.</div></div>'; } } } if ( ! $variation_purchasable ) { wc_enqueue_js(" jQuery('.variations_form') .on( 'woocommerce_variation_select_change', function( event ) { jQuery('.wc-nonpurchasable-message').hide(); }) .on( 'found_variation', function( event, variation ) { jQuery('.wc-nonpurchasable-message').hide(); if ( ! variation.is_purchasable ) { jQuery( '.wc-nonpurchasable-message.js-variation-' + variation.variation_id ).show(); } }) .find( '.variations select' ).change(); "); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥100 set_link_state
- ¥15 虚幻5 UE美术毛发渲染
- ¥15 CVRP 图论 物流运输优化
- ¥15 Tableau online 嵌入ppt失败
- ¥100 支付宝网页转账系统不识别账号
- ¥15 基于单片机的靶位控制系统
- ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
- ¥15 手机接入宽带网线,如何释放宽带全部速度