-
hook into the woocommerce hook is_purchasable
add_filter("woocommerce_is_purchasable", function($product, $isPurchasable) { if(!$isPurchasable) return $isPurchasable; $productId = $product->get_id(); $user = wp_get_current_user(); if($user->exists() && $productId == 769) { $userId = $user->ID; $userEmail = $user->user_email; if(wc_customer_bought_product($userEmail, $userId, $productId)) { //block the purchase so the user sees read more instead of add to cart $isPurchasable = false; } } return $isPurchasable; });
-
hook into the init field and and if the current page is that product id
then we will need to display:none the add to cart button and instead add a download
button. If you have access to a theme builder add a button on the single product and
hide it in the beginning. That will make this process much easier.add_action("init", function() { global $product; if(is_product() && $product->get_id() == 769) { $className = "add_to_cart_button"; $downloadLink = ""; // your link here printf( "<script> let element = document.getElementByClassName(\"%s\"); element.style.display=\"none\"; let link = document.createElement(\"a\"); link.classList.add(\"button\"); link.setAttribute(\"href\", \"%s\"); link.textContent = \"download\"; let parent = element.parentElement; parent.insertAdjacentElement(\"afterend\", link); </script>", $className, $downloadLink ); } });
ps: If you want the user to be logged in before adding the item to cart do the following
add_filter("woocommerce_is_purchasable", function($product, $isPurchasable)
{
if(!$isPurchasable)
return $isPurchasable;
$productId = $product->get_id();
$user = wp_get_current_user();
//block the user from even adding it to cart before logging in
if($productId == 769 && !$user->exists())
return false;
if($user->exists() && $productId == 769)
{
$userId = $user->ID;
$userEmail = $user->user_email;
if(wc_customer_bought_product($userEmail, $userId, $productId))
{
//block the purchase so the user sees read more instead of add to cart
$isPurchasable = false;
}
}
return $isPurchasable;
});
ps2: I did not run this code myself just off the top of my head. Should be close enough though to run. will check if it runs on my system.
New Code: Just did a test run and it works
add_filter("woocommerce_is_purchasable", function($isPurchasable, $product)
{
if(!$isPurchasable)
return $isPurchasable;
$productId = $product->get_id();
$user = wp_get_current_user();
if($user->exists() && $productId == 796)
{
$userId = $user->ID;
$userEmail = $user->user_email;
if(wc_customer_bought_product($userEmail, $userId, $productId))
{
//block the purchase so the user sees read more instead of add to cart
$isPurchasable = false;
}
}
return $isPurchasable;
}, 10, 2);
add_filter("woocommerce_is_purchasable", function($isPurchasable, $product)
{
if(!$isPurchasable)
return $isPurchasable;
$productId = $product->get_id();
$user = wp_get_current_user();
//dont allow non existing users to buy
if(!$user->exists() && $productId == 796)
$isPurchasable = false;
return $isPurchasable;
}, 10, 2);
add_action("woocommerce_after_single_product", function()
{
global $post;
$product = wc_get_product($post->ID);
if(empty($product) || is_null($product))
return;
if($product->get_id() != 796)
return;
$user = wp_get_current_user();
if(!$user->exists())
return;
if(!wc_customer_bought_product($user->user_email, $user->ID, $product->get_id()))
return;
$productId = $product->get_id();
$downloadLink = "#"; // your link here
echo sprintf(
"<script>
let element = document.getElementById(\"product-%d\");
let form = element.getElementsByTagName(\"form\")[0];
let addToCartButton = form.getElementsByTagName(\"button\")[0];
element = addToCartButton;
let classList = element.classList;
element.style.display=\"none\";
let link = document.createElement(\"a\");
link.classList = classList;
link.setAttribute(\"href\", \"%s\");
link.textContent = \"download\";
let parent = element.parentElement;
parent.insertAdjacentElement(\"afterend\", link);
</script>",
$productId,
$downloadLink
);
});