[英]Change single add to cart text when product is out of stock in WooCommerce
In Woocommerce I am trying to change the add to cart text from "Add to cart" to "Out of stock" on single product pages when the product is out of stock, for simple products and the product variations on variable products.在 Woocommerce 中,当产品缺货时,我试图将添加到购物车的文本从“添加到购物车”更改为“缺货”,适用于简单产品和可变产品的产品变体。
We are currently not using any sort of stock management because all of our products are hand made & we do set stock status manually.我们目前没有使用任何类型的库存管理,因为我们所有的产品都是手工制作的,我们会手动设置库存状态。
Is there any possible way to achieve that?有什么办法可以实现吗?
For all products except variable products, is quite simple… But for variable products and it's variations, it requires some more code and the usage of javascript/jQuery.对于除可变产品之外的所有产品,非常简单……但是对于可变产品及其变体,它需要更多代码和 javascript/jQuery 的使用。
So the following code will change add to cart text to "Out of stock" when the current product is out of stock even for selected product variations on variable products:因此,即使对于可变产品的选定产品变体,当当前产品缺货时,以下代码也会将添加到购物车的文本更改为“缺货”:
// For all products except variable product
add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_single_add_to_cart_text_filter_callback', 20, 2 );
function product_single_add_to_cart_text_filter_callback( $button_text, $product ) {
if( ! $product->is_in_stock() && ! $product->is_type('variable') ) {
$button_text = __("Out of stock", "woocommerce");
}
return $button_text;
}
// For all product variations (on a variable product)
add_action( 'woocommerce_after_add_to_cart_button', 'after_add_to_cart_button_action_callback', 0 );
function after_add_to_cart_button_action_callback() {
global $product;
if( $product->is_type('variable') ) :
$data = [];
// Loop through variation Ids
foreach( $product->get_visible_children() as $variation_id ){
$variation = wc_get_product( $variation_id );
$data[$variation_id] = $variation->is_in_stock();
}
$outofstock_text = __("Out of Stock", "woocommerce");
?>
<script type="text/javascript">
jQuery(function($){
var b = 'button.single_add_to_cart_button',
t = $(b).text();
$('form.variations_form').on('show_variation hide_variation found_variation', function(){
$.each(<?php echo json_encode($data); ?>, function(j, r){
var i = $('input[name="variation_id"]').val();
if(j == i && i != 0 && !r ) {
$(b).html('<?php echo $outofstock_text; ?>');
return false;
} else {
$(b).html(t);
}
});
});
});
</script>
<?php
endif;
}
Code goes in function.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的 function.php 文件。 Tested and works.测试和工作。
Tried to write as comment but seems code formatting not working, so writing as an answer.试图写成评论,但似乎代码格式不起作用,所以写成一个答案。
For a single variation add to cart shortcode this hack will help.对于将单个变体添加到购物车短代码,此 hack 将有所帮助。
// For variation product
add_filter( 'woocommerce_product_add_to_cart_text', 'product_variation_add_to_cart_text_filter_callback', 20, 2 );
function product_variation_add_to_cart_text_filter_callback( $button_text, $product ) {
if ( ! $product->is_in_stock() && $product->is_type( 'variation' ) ) {
$button_text = __( "Out of stock", "woocommerce" );
}
return $button_text;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.