Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionI have a Wordprees website and I sell book (product ID 769) on my site, I want woocommerce replace "add to cart" button with "Download" button if
- user is logged in
- and purchased (product ID "769")
user will find "download" button instant of "add to cart" button in product single page
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionI have a Wordprees website and I sell book (product ID 769) on my site, I want woocommerce replace "add to cart" button with "Download" button if
- user is logged in
- and purchased (product ID "769")
user will find "download" button instant of "add to cart" button in product single page
Share Improve this question asked Jan 9, 2021 at 17:38 dadadada 12 bronze badges 1- 1 You might be better of taking this question to the WooCommerce Slack community as community members here consider WooCommerce to be off topic. Then again, you might get answers here too. – Matthew Brown aka Lord Matt Commented Jan 10, 2021 at 6:21
1 Answer
Reset to default 2hook 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
);
});