[英]Hide Add to cart block when a variable product is out of stock in Woocommerce
In Woocommerce, I am using Woocommerce Waitlist plugin that shows a "Join Waitlist" button, near the "Add to Cart" button when product is out of stock. 在Woocommerce中,我正在使用Woocommerce Waitlist插件,当产品缺货时,该插件显示“加入候补名单 ”按钮,靠近“添加到购物车”按钮。 On my variable subscriptions, I have tried to hide the "Add to cart" button block when the product is out of stock, without success. 在我的变量订阅中,我试图在产品缺货时隐藏“添加到购物车”按钮块,但没有成功。
We are using the Vantage theme and Woocommerce subscriptions , if it can help. 我们正在使用Vantage主题和Woocommerce订阅 ,如果它可以帮助。
How to hide the "Add to Cart" block when variable product is out of stock in Woocommerce? 如何在Woocommerce中变量产品缺货时隐藏“添加到购物车”块?
Updated - To detect "out of stock" selected variation and hide add to cart button block, the way is to use jQuery: 更新 - 要检测“缺货”选择的变体并隐藏添加到购物车按钮块,方法是使用jQuery:
add_action( 'wp_footer', 'single_add_to_cart_event_text_replacement' );
function single_add_to_cart_event_text_replacement() {
global $product;
// Only single variable products
if( ! ( is_product() && $product->is_type('variable') ) )
return;
?>
<script type="text/javascript">
jQuery(function($){
var vs = 'table.variations select', vi = 'input.variation_id',
atc = 'woocommerce-variation-add-to-cart', atcd = atc+'-disabled',
atc = '.'+atc;
// 1. On start (With a mandatory light delay)
setTimeout(function(){
if ( 0 < $(vi).val() && $(atc).hasClass(atcd) ) {
$(atc).hide();
}
}, 250);
// 2. On variation change
$('.variations_form').on( 'blur', vs, function(){
if( 0 < $(vi).val() && $(atc).hasClass(atcd) ){
$(atc).hide();
} else {
$(atc).show();
}
});
})
</script>
<?php
}
Code goes in function.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。
The CSS way (not convenient) CSS方式 (不方便)
As the <div>
container get a tag class woocommerce-variation-add-to-cart-disabled
that grey out the add to cart button you could use a CSS rule to hide the entire block the button and the quantity field: 当<div>
容器获得一个标签类woocommerce-variation-add-to-cart-disabled
,使添加到购物车按钮变灰时,您可以使用CSS规则隐藏整个块按钮和数量字段:
.woocommerce-variation-add-to-cart-disabled { display:none !important; }
But When no variations are selected it will also hide add to cart, so it's not convenient. 但是如果没有选择任何变化,它也会隐藏添加到购物车, 所以这不方便。
Note: I don't know where your waitlist plugin adds its buttons, so if it adds them to the cart form then they will get hidden too, so this won't work. 注意:我不知道你的waitlist插件添加其按钮的位置,所以如果它将它们添加到购物车表单,那么它们也会被隐藏,所以这不起作用。
Two Points: 两点:
1: Set Allow back-orders? 1:设置允许延期交货? on each product to Do not allow. 在每个产品上都不允许。 This will stop Woocommerce from displaying the add to cart button. 这将阻止Woocommerce显示添加到购物车按钮。 You do have to do this for each individual product, but it can be done using Bulk Edit. 您必须为每个产品执行此操作,但可以使用批量编辑来完成。
2: Hide the add to cart form using CSS. 2:使用CSS隐藏添加到购物车表单。 Something like this will target all products which are not in stock: 这样的东西将针对所有没有库存的产品:
.type-product:not(.instock) form.cart { display: none; }
Hope that help to you 希望对你有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.