Skip to content

Instantly share code, notes, and snippets.

@bekarice
Last active March 9, 2020 07:13
Show Gist options
  • Select an option

  • Save bekarice/0143d1b423857b0c6885 to your computer and use it in GitHub Desktop.

Select an option

Save bekarice/0143d1b423857b0c6885 to your computer and use it in GitHub Desktop.

Revisions

  1. bekarice revised this gist Feb 5, 2020. 1 changed file with 18 additions and 21 deletions.
    39 changes: 18 additions & 21 deletions wc-disable-product-repeat-purchase.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    <?php
    <?php // only copy if needed!

    /**
    * Disables repeat purchase for the product
    *
    @@ -10,25 +11,20 @@ function sv_disable_repeat_purchase( $purchasable, $product ) {

    // Enter the ID of the product that shouldn't be purchased again
    $non_purchasable = 356;

    // Get the ID for the current product (passed in)
    $product_id = $product->is_type( 'variation' ) ? $product->variation_id : $product->id;
    $product_id = $product->get_id();

    // Bail unless the ID is equal to our desired non-purchasable product
    if ( $non_purchasable != $product_id ) {
    if ( $non_purchasable !== $product_id ) {
    return $purchasable;
    }

    // return false if the customer has bought the product
    if ( wc_customer_bought_product( 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_variation_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );
    @@ -39,17 +35,15 @@ function sv_disable_repeat_purchase( $purchasable, $product ) {
    * Shows a "purchase disabled" message to the customer
    */
    function sv_purchase_disabled_message() {
    global $product; // get the current product to check if purchasing should be disabled

    // Enter the ID of the product that shouldn't be purchased again
    $no_repeats_id = 356;
    $no_repeats_product = wc_get_product( $no_repeats_id );

    // Get the current product to check if purchasing should be disabled
    global $product;

    if ( $no_repeats_product->is_type( 'variation' ) ) {
    // Bail if we're not looking at the product page for the non-purchasable product
    if ( ! $no_repeats_product->parent->id === $product->id ) {
    if ( ! $no_repeats_product->get_parent_id() === $product->get_id() ) {
    return;
    }

    @@ -58,7 +52,7 @@ function sv_purchase_disabled_message() {
    sv_render_variation_non_purchasable_message( $product, $no_repeats_id );
    }

    } elseif ( $no_repeats_id === $product->id ) {
    } elseif ( $no_repeats_id === $product->get_id() ) {
    if ( wc_customer_bought_product( get_current_user()->user_email, get_current_user_id(), $no_repeats_id ) ) {
    // 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>';
    @@ -68,19 +62,21 @@ function sv_purchase_disabled_message() {
    add_action( 'woocommerce_single_product_summary', 'sv_purchase_disabled_message', 31 );


    if ( ! function_exists( 'sv_render_variation_non_purchasable_message' ) ) :
    /**
    * Generates a "purchase disabled" message to the customer for specific variations
    *
    * @param \WC_Product $product the WooCommerce product
    * @param int $no_repeats_id the id of the non-purchasable product
    */
    function sv_render_variation_non_purchasable_message( $product, $no_repeats_id ) {

    // Double-check we're looking at a variable product
    if ( $product->is_type( 'variable' ) && $product->has_child() ) {
    $variation_purchasable = true;

    foreach ( $product->get_available_variations() as $variation ) {

    // only show this message for non-purchasable variations matching our ID
    if ( $no_repeats_id === $variation['variation_id'] ) {
    $variation_purchasable = false;
    @@ -89,7 +85,7 @@ function sv_render_variation_non_purchasable_message( $product, $no_repeats_id )
    }
    }
    }

    // show / hide this message for the right variation with some jQuery magic
    if ( ! $variation_purchasable ) {
    wc_enqueue_js("
    @@ -106,4 +102,5 @@ function sv_render_variation_non_purchasable_message( $product, $no_repeats_id )
    .find( '.variations select' ).change();
    ");
    }
    }
    }
    endif;
  2. bekarice revised this gist Sep 23, 2015. No changes.
  3. bekarice created this gist Sep 22, 2015.
    109 changes: 109 additions & 0 deletions wc-disable-product-repeat-purchase.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,109 @@
    <?php
    /**
    * Disables repeat purchase for the product
    *
    * @param bool $purchasable true if product can be purchased
    * @param \WC_Product $product the WooCommerce product
    * @return bool $purchasable the updated is_purchasable check
    */
    function sv_disable_repeat_purchase( $purchasable, $product ) {

    // Enter the ID of the product that shouldn't be purchased again
    $non_purchasable = 356;

    // Get the ID for the current product (passed in)
    $product_id = $product->is_type( 'variation' ) ? $product->variation_id : $product->id;

    // Bail unless the ID is equal to our desired non-purchasable product
    if ( $non_purchasable != $product_id ) {
    return $purchasable;
    }

    // return false if the customer has bought the product
    if ( wc_customer_bought_product( 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_variation_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );
    add_filter( 'woocommerce_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );


    /**
    * Shows a "purchase disabled" message to the customer
    */
    function sv_purchase_disabled_message() {

    // Enter the ID of the product that shouldn't be purchased again
    $no_repeats_id = 356;
    $no_repeats_product = wc_get_product( $no_repeats_id );

    // Get the current product to check if purchasing should be disabled
    global $product;

    if ( $no_repeats_product->is_type( 'variation' ) ) {
    // Bail if we're not looking at the product page for the non-purchasable product
    if ( ! $no_repeats_product->parent->id === $product->id ) {
    return;
    }

    // Render the purchase restricted message if we are
    if ( wc_customer_bought_product( get_current_user()->user_email, get_current_user_id(), $no_repeats_id ) ) {
    sv_render_variation_non_purchasable_message( $product, $no_repeats_id );
    }

    } elseif ( $no_repeats_id === $product->id ) {
    if ( wc_customer_bought_product( get_current_user()->user_email, get_current_user_id(), $no_repeats_id ) ) {
    // 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 );


    /**
    * Generates a "purchase disabled" message to the customer for specific variations
    *
    * @param \WC_Product $product the WooCommerce product
    * @param int $no_repeats_id the id of the non-purchasable product
    */
    function sv_render_variation_non_purchasable_message( $product, $no_repeats_id ) {

    // Double-check we're looking at a variable product
    if ( $product->is_type( 'variable' ) && $product->has_child() ) {
    $variation_purchasable = true;

    foreach ( $product->get_available_variations() as $variation ) {
    // only show this message for non-purchasable variations matching our ID
    if ( $no_repeats_id === $variation['variation_id'] ) {
    $variation_purchasable = false;
    // edit your customer message here
    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.</div></div>';
    }
    }
    }

    // show / hide this message for the right variation with some jQuery magic
    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();
    ");
    }
    }