Last active
January 24, 2019 12:07
-
-
Save amolv/5a126cc85881284ec3d2079c5ab89e66 to your computer and use it in GitHub Desktop.
Woocommerce add duplicate order of a product
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 | |
// Block duplicate purchase of products in Woocommerce using hooks | |
// variationtion skipped here as I am not using in current project, though you can add support | |
function in_disable_repeat_purchase( $purchasable, $product ) { | |
// Don't run on parents of variations, | |
// this will already check variations separately | |
if ( $product->is_type( 'variable' ) ) { | |
return $purchasable; | |
} | |
// Get the ID for the current product (passed in) | |
$product_id = $product->is_type( 'variation' ) ? $product->variation_id : $product->id; | |
// return false 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', 'in_disable_repeat_purchase', 10, 2 ); | |
function in_purchase_disabled_message() { | |
global $product; | |
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" style="margin:10px 0;">You\'ve already purchased this product! You can download product files again from <a href="/my-account/free-trial-downloads/">Trial Products</a>.</div></div>'; | |
} | |
} | |
add_action( 'woocommerce_single_product_summary', 'in_purchase_disabled_message', 31 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment