简体   繁体   English

在 WooCommerce 中产品缺货时更改单个添加到购物车文本

[英]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.

相关问题 隐藏在Woocommerce中变量产品缺货时添加到购物车块 - Hide Add to cart block when a variable product is out of stock in Woocommerce 如何在 Woocommerce 缺货产品文本下方添加单个产品的 Woocommerce 产品价格 - How can I add below of Woocommerce out of stock product text the Woocommerce product price on a single product WooCommerce:当产品已在购物车中时更改添加到购物车的文本 - WooCommerce: change the add to cart text when the product is already in cart 如果库存状态为0,Woocommerce更改将添加到购物车文本 - Woocommerce change add to cart text if stock status is 0 仅在 WooCommerce 单个产品页面上将添加到购物车文本更改为“应用” - Change Add to Cart text to “Apply” on WooCommerce single product pages only 如何在woocommerce中为缺货产品启用“添加到购物车”按钮? - How to enable add to cart button for out of stock product in woocommerce? 如果产品在购物车中,则更改“添加到购物车”按钮文本,并在 WooCommerce 中满足条件时重定向到购物车页面 - Change "add to cart" button text if product is in cart and redirect to cart page when condition is met in WooCommerce 产品售罄时更改“添加到购物车”文本 - Change 'add to cart' text when product is sold out WooCommerce-产品已经在购物车中,更改“添加到购物车”文本为ajax - WooCommerce - Product already in cart, change “add to cart” text ajax 当产品在 Woocommerce 的购物车中时,更改添加到购物车按钮样式 - Change add to cart button style when product is in cart in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM