在WooCommerce的可变产品下拉列表中显示售罄文本

时间:2017-01-18 作者:Faisal Alvi

我在WooCommerce中有两种可变产品:

颜色[红色]

  • 尺寸[2,4]

    1. Stock Status:

      红色+2=库存outofstock
      当我选择“红色”时,我想显示\'SOLD OUT\' 之后的文本\'4\' “大小”下拉列表(即。4 - SOLD OUT).

      这可能需要:AJAX

      提前谢谢。

    1 个回复
    最合适的回答,由SO网友:Faisal Alvi 整理而成

    步骤1:将以下代码添加到变量中。php文件位于woocommerce插件中(单个产品/添加到购物车)

    <?php
        $product_variations = $product->get_available_variations();
    
        echo \'<ul class="attr_avail_custom dnone">\';
        foreach ($product_variations as $variation)  {
            $var_data = $variation[\'attributes\'];
    
            if(!$variation[\'is_in_stock\']) {
                echo \'<li>\';
                foreach ($var_data as $vk => $vv) {
    
                    echo "<span data-key=$vk data-value=$vv >$vv</span>";
                }
                echo \'</li>\';
            }
    
        }
        echo \'</ul>\';
    ?>
    
    步骤2:向javascript文件中添加以下代码
        //SOLD OUT text jquery for Prdocut detailed pages...
        if( jQuery(\'ul.attr_avail_custom\').length > 0 ) { /*loading jQuery only if the current page is product\'s detail page*/
    
            //On hovering mouse over the SIZE dropdown - adding SOLD OUT text according to the hidden \'out of stock\' attributes available in \'ul.attr_avail_custom\'...
            jQuery(\'select[id*="size"]\').hover(function () {
                var selected_color = jQuery(\'select[id*="colour"]\').val();
    
                //firslty removing SOLD OUT from all if added previously...
                jQuery(\'select[id*="size"] option:not([value=""])\').each(function () {
                    jQuery(this).text(jQuery(this).val());
                });
    
                /*checking if the selected colour is in the SOLD OUT pair printed (above select boxes) in hidden and coded in this file woocommerce (single-product\\add-to-cart\\variable.php) */
                jQuery(\'ul.attr_avail_custom [data-key*="colour"][data-value="\' + selected_color + \'"]\').each(function () { /*out of stock size loop fro selected colour*/
                    var out_stock_colour_val = jQuery(this).data(\'value\');
                    var out_stock_size_val = jQuery(this).parent().find(\'[data-key*="size"]\').data(\'value\');
    
                    //Finally adding SOLD OUT text...
                    jQuery(\'select[id*="size"] option[value="\' + out_stock_size_val + \'"]\').text(out_stock_size_val + \' - SOLD OUT\');
    
                });
            });
    
            //if mouse is hover over COLOUR dropdown - we must have to (1) Flush the SIZE if seleceted and (2) Triggering clicks on both Dropdowns to prevent AJAX conflicts... (this is required)
            jQuery(\'select[id*="colour"]\').hover(function () {
                //Flushing selecrted SIZE and triggering click..
                jQuery(\'select[id*="size"]\').trigger(\'click\');
    
                jQuery(\'select[id*="size"] option:eq(0)\').attr(\'selected\',\'selected\');
    
                //triggering click on COLOUR dropdown..
                jQuery(\'select[id*="colour"]\').trigger(\'click\');
    
            });
        }
    
    Note: 记住覆盖您的variable.php 主题文件夹中的文件。Click here 学习如何做到这一点。

    相关推荐

    WordPress AJAX错误400向远程站点发送数据的错误请求

    我正在使用发件人。net获取电子邮件订阅列表。这个网站给了我一些信息,可以将用户的电子邮件添加到订阅列表中。我想使用WordPress ajax来实现这一点。但它返回错误400错误请求。我的代码是:文件ajax新闻脚本。js公司: jQuery(document).ready(function($){ // Perform AJAX send news on form submit $(\'form#fnews\').on(\'submit\', funct