可购买woocommerce挂钩
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;
});
钩住init字段,如果当前页面是产品ID,那么我们需要显示:无“添加到购物车”按钮,而是添加一个“下载”按钮。如果您有权访问主题生成器,请在单个产品上添加一个按钮,并将其隐藏在开头。这将使这一过程更加容易。
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:如果您希望用户在将项目添加到购物车之前登录,请执行以下操作
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:我自己并没有在头脑中运行这段代码。应该足够近才能跑。将检查它是否在我的系统上运行。
新代码:刚刚做了一次测试运行,它正常工作了
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
);
});