【问题标题】:How can I display text message if woocommerce quantity input is greater than stock quantity?如果 woocommerce 数量输入大于库存数量,如何显示短信?
【发布时间】:2019-01-31 03:15:05
【问题描述】:

我想显示一条自定义文本消息,以通知客户他们选择的输入数量(在单击“添加到购物车”按钮之前)。如果所选数量大于现有可用库存数量,则会出现此消息,位于单个产品页面中数量选择的正上方。例如:

Existing Stock Quantity: 2
User Selects: >2

在这种情况下,我想告诉客户类似这样的信息:“您选择的订单数量大于我们现有的库存。预计我们的库存最多会延迟 2 周补货。”

我尝试将自定义代码添加到代码片段中,看起来像这样:

function display_order_quantity_exceeds_stock_quantity_text( $message, $product ) {

if( $product->woocommerce_quantity_input() > $product->get_stock_quantity()) {
    $message = "Your selected order quantity is greater than our existing stock. Please expect a delay of up to 2 weeks for our stock to be replenished.";
}
return $message;

}

有谁知道我如何获得 woocommerce_quantity_input 并让它工作?

宁愿通过在代码片段中添加一个函数来获得解决方案,而不是使用 Javascript(如果可能)。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    在你的主题的 footer.php 中添加这个 jQuery 代码将在用户输入的值超过 stock 时触发警报

    <script type="text/javascript">
    
    function show_error($field, $mesg) {
        if ($field.prev('.error_msg').length) {
            $field.prev('.error_msg').html('<p>' + $mesg + '</p>');
        } else {
            jQuery('<div class="error_msg" style="color:#f00"><p>' + $mesg + '</p></div>').insertBefore($field);
        }
    
    }
    
    function remove_error($field) {
        if ($field.prev('.error_msg').length) {
            $field.prev('.error_msg').remove();
        }
    }
    
    jQuery(".quantity input[name=quantity]").on('change', function(e) {
    
        if (jQuery(this).val() > jQuery(this).attr("max")) {
            show_error(jQuery(this).parent(".quantity"), "Your selected order quantity is greater than our existing stock. Please expect a delay of up to 2 weeks for our stock to be replenished")
        } else {
            remove_error(jQuery(this).parent(".quantity"));
        }
    })
    
    </script>
    

    您必须启用管理库存,并为此产品设置库存编号才能使其正常工作。

    【讨论】:

    • 是的,我已启用托管库存并为不同的产品变体设置库存编号。它有效,但似乎没有正确进行计算。每次我增加订单数量时,都会弹出警报。另外,我不希望它提醒客户,而只是在数量选择 div 上方显示消息。你有解决办法吗?
    • 非常感谢您更新代码!但是,"jQuery(this).val() > jQuery(this).attr("max")" 比较似乎不起作用。只要输入数量达到2及以上,错误信息似乎正在显示。我在这里做错了什么吗?
    • 我已经修改了我的代码以包含 parseInt,它将数量作为字符串进行比较。
    • @ZhanHao 请确保您提供了产品的库存数量值imgur.com/a/MxHV2Ke
    • @steakoverflow 我已经尝试了您更新的代码,但它似乎仍然无法正确比较。我检查了 javascript 控制台,它似乎无法将 parseInt(qty_box.attr('max')) 识别为整数。每次将订单数量增加 1 时,我都会收到“2 NaN false,3 NaN false,4 NaN false”。
    【解决方案2】:

    我会通过 jQuery 执行此操作,因此将其添加到您的 functions.php($src 变量指向您的主题文件夹中的 JS 文件位置):

    function a3_enqueue_scripts() {
        if(is_singular( 'product' )){       
            $handle = 'a3_wooc_js';
    
            //path to your Javascript file
            $src = get_theme_file_uri( '/js/a3_wooc.js' );
    
            wp_enqueue_script( $handle, $src, array( 'jquery' ), false, false);  
        }
    }
    
    add_action( 'wp_enqueue_scripts', 'a3_enqueue_scripts' );
    

    在包含的 JS 文件中是这样的:

    (function($){
        $(document).ready(function(){
            //the jQuery selector depends on your theme output for the quantity text box identifiers
            $('[name="quantity"]').on('change', function(e){
                var qty_box = $(this);
                var error_message = $('<div class="error_msg">Your selected order quantity is greater than our existing stock. Please expect a delay of up to 2 weeks for our stock to be replenished</div>');
    
                console.log(parseInt(qty_box.val()), parseInt(qty_box.attr('max')), qty_box.val() > parseInt(qty_box.attr('max')));
    
                if(parseInt(qty_box.val()) > parseInt(qty_box.attr('max'))) {
                    // the action to take if the quantity exceeds max stock     
                    if($('.quantity .error_msg').length < 1){
                        $('.quantity').prepend(error_message);                              
                    }               
                }
                else {
                    $('.quantity .error_msg').remove();
                }
            });
        });
    })(jQuery);
    

    【讨论】:

    • 我已经尝试了您的步骤,但它似乎不起作用。当我尝试增加数量时,没有显示任何消息。我在这里错过了什么吗?
    • 您在主题中的哪个文件/文件夹中放置了 jQuery 代码?您的 $src 变量是否指向该路径?你是在单品页面上测试吗?你试过清除页面缓存、浏览器缓存吗?
    • 我已将 jQuery 放在我的 theme/assets/js 文件夹中并相应地更改了 $src 路径,它在控制台中正确引用。至于 php 函数,我已经添加了使用 Code Snippets 插件,而不是将其放在 functions.php 文件中。是的,我是在单品页面上测试的,但是随着我增加订单数量,它没有显示任何文字。尝试清除浏览器和页面缓存,但无济于事。
    • @steakoverflow:有什么方法可以将所有代码合并到一个 sn-p 中以添加到 functions.php 中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 2018-05-25
    • 2021-11-09
    • 2015-12-26
    • 1970-01-01
    • 2019-01-25
    相关资源
    最近更新 更多