Updated (compatibility for Woocommerce 3+)
Yes, perhaps a conditional function record that returns true if the current customer has already purchased specific product identifiers. This code continues in the functions.php file of your active child theme or theme.
Here is the conditional function:
function has_bought_items() {
$bought = false;
$prod_arr = array( '21', '67' );
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order',
'post_status' => 'wc-completed'
) );
foreach ( $customer_orders as $customer_order ) {
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
$order = wc_get_order( $customer_order );
foreach ($order->get_items() as $item) {
if ( version_compare( WC_VERSION, '3.0', '<' ) )
$product_id = $item['product_id'];
else
$product_id = $item->get_product_id();
if ( in_array( $product_id, $prod_arr ) )
$bought = true;
}
}
return $bought;
}
This code has been verified and works.
APPLICATION:
For example, you can use it in some WooCommerce templates that you previously copied into your active child theme or theme:
, ():
$restricted_products = array( '20', '32', '75' );
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
if ( !has_bought_items() && in_array( $product_id, $restricted_products ) ) {
} else {
}
add-to-cart :
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
$restricted_products = array( '37', '53', '70' );
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
if ( !has_bought_items() && in_array( $product_id, $restricted_products ) ) {
echo '<a class="button greyed_button">' . __("Disabled", "your_theme_slug") . '</a>';
echo '<br><span class="greyed_button-message">' . __("Your message goes hereβ¦", "your_theme_slug") . '</span>';
} else {
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product_id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
),
$product );
}
greyed_button style.css . greyed_button-message.