Last active
June 28, 2019 15:48
-
-
Save bekarice/03a049bb2748441373768410de293ffb to your computer and use it in GitHub Desktop.
Disables repeat purchases for products within a particular category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // only copy if needed | |
/** | |
* Helper function to see if a customer has purchased a product | |
* in a given category | |
* | |
* REQUIRES WC 2.5+ | |
* | |
* @param \WC_Product $product the WooCommerce product instance | |
* @return bool - true if product is in category and has been purchased | |
*/ | |
function sv_wc_has_customer_purchased( $product ) { | |
// enter the category for which a single purchase is allowed | |
$non_repeatable = 'clothing'; | |
// bail if this product is in not in our target category | |
if ( ! has_term( $non_repeatable, 'product_cat', $product->get_id() ) ) { | |
return false; | |
} | |
// the product has our target category, so return whether the customer purchased | |
return wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $product->get_id() ); | |
} | |
/** | |
* Disables repeat purchase for a product category; checks if a product is in the category | |
* and if it's already been purchased, then disables purchasing if so | |
* | |
* @param bool $purchasable true if product can be purchased | |
* @param \WC_Product $product the WooCommerce product | |
* @return bool - the updated is_purchasable check | |
*/ | |
function sv_wc_disable_repeat_purchase( $purchasable, $product ) { | |
if ( sv_wc_has_customer_purchased( $product ) ) { | |
$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_wc_disable_repeat_purchase', 10, 2 ); | |
add_filter( 'woocommerce_is_purchasable', 'sv_wc_disable_repeat_purchase', 10, 2 ); | |
/** | |
* Shows a "purchase disabled" message to the customer | |
*/ | |
function sv_purchase_disabled_message() { | |
// get the current product to check if purchasing should be disabled | |
global $product; | |
// now we know we're in the category, check if we've purchased already | |
if ( sv_wc_has_customer_purchased( $product ) ) { | |
// Create your message for the customer here | |
echo '<div class="woocommerce"><div class="woocommerce-info wc-nonpurchasable-message"> | |
You\'ve already purchased this product! It can only be purchased once. | |
</div></div>'; | |
} | |
} | |
add_action( 'woocommerce_single_product_summary', 'sv_purchase_disabled_message', 31 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment