My changes:

This commit is contained in:
Ryan Halliday 2018-01-02 16:53:51 +13:00
parent 19e07404a3
commit 20166b2aeb
2 changed files with 24 additions and 9 deletions

View file

@ -1,8 +1,7 @@
=== Disable Downloadable Repeat Purchase - WooCommerce ===
Contributors: squareonemedia, rwebster85
Contributors: squareonemedia, rwebster85, ryanhalliday
Author URI: https://www.squareonemedia.co.uk
Plugin URL: https://wordpress.org/plugins/disable-downloadable-repeat-purchase-woocommerce/
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VAYF6G99MCMHU
Requires at Least: 3.7
Tested Up To: 4.5.2
Tags: woocommerce, downloads, downloadable, repeat purchase, wordpress, wordpress.org
@ -42,5 +41,8 @@ It uses div tags, including <div class="woocommerce-info wc-nonpurchasable-messa


== Changelog ==
= 1.1 =
* Improved performance by saving query results
= 1.0 =
* Initial release


View file

@ -1,17 +1,19 @@
<?php
/*
Plugin Name: Disable Downloadable Repeat Purchase - WooCommerce
Plugin Name: WooCommerce Disable Downloadable Repeat Purchase
Description: Disable the ability for logged in users to purchase items they already own that are downloadable. Links are provided on the product page for ease of re-download. It does not apply to products the customer has been refunded for, or has their download expired. It checks if the product has been purchased and is available to download on their account.
Author: Square One Media
Author: Square One Media, Ryan Halliday
Author URI: https://www.squareonemedia.co.uk
Version: 1.0
Text Domain: disable-downloadable-repeat-purchase
Version: 1.1
Text Domain: woocommerce-disable-downloadable-repeat-purchase
License: GPLv2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/

$customers_downloadable_products = 0;

function som_disable_repeat_purchase( $purchasable, $product ) {
global $customers_downloadable_products;

// Get the ID for the current product
$product_id = $product->id;
@ -19,7 +21,12 @@ function som_disable_repeat_purchase( $purchasable, $product ) {
// return false if the customer has bought the product and is currently available for download
if ( wc_customer_bought_product( get_current_user()->user_email, get_current_user_id(), $product_id ) && ($product->downloadable == 'yes') ) {

if ( $downloads = WC()->customer->get_downloadable_products() ) {
if ($customers_downloadable_products === 0){
echo 'Refetch';
$customers_downloadable_products = WC()->customer->get_downloadable_products();
}

if ( $downloads = $customers_downloadable_products ) {

foreach ( $downloads as $download ) :
if ($download['product_id'] == $product->id) {
@ -37,12 +44,18 @@ add_filter( 'woocommerce_is_purchasable', 'som_disable_repeat_purchase', 10, 2 )
function som_repeat_purchase_disabled_message() {

global $product;
global $customers_downloadable_products;
$once = 0;
$end = false;

if ( wc_customer_bought_product( get_current_user()->user_email, get_current_user_id(), $product->id ) && ($product->downloadable == 'yes') ) {

if ( $downloads = WC()->customer->get_downloadable_products() ) {
if ($customers_downloadable_products === 0){
echo 'Refetch';
$customers_downloadable_products = WC()->customer->get_downloadable_products();
}

if ( $downloads = $customers_downloadable_products ) {

do_action( 'woocommerce_before_available_downloads' ); ?>

@ -89,4 +102,4 @@ function som_repeat_purchase_disabled_message() {
}

}
add_action( 'woocommerce_single_product_summary', 'som_repeat_purchase_disabled_message', 31 );
add_action( 'woocommerce_single_product_summary', 'som_repeat_purchase_disabled_message', 31 );