将 Woo Commerce 下拉菜单转换为单选按钮

2023-12-20

我在将变体下拉列表转换为支持 woocommerce 的网站的单选按钮时遇到问题。我已经尝试找到答案here https://stackoverflow.com/questions/11819604/dropdown-converted-to-radio-buttons-no-longer-triggering-specific-function但我似乎无法使这个解决方案发挥作用。我还尝试编辑variable.php 文件中的代码。

到目前为止,最成功的是第二种方法,编辑variable.php代码并将标签更改为标签。问题是单击单选按钮时不会触发自然的 jQuery 事件。

如果你们中有人知道一种更简单的方法将下拉列表转换为单选按钮,或者简单地触发 jQuery 事件,我们将不胜感激。

我可以得到的相关代码:

<?php $loop = 0; foreach ( $attributes as $name => $options ) : $loop++; ?>

<tr>

<td class="label"><label for="<?php echo sanitize_title($name); ?>"><?php echo $woocommerce->attribute_label($name); ?></label></td>

<td class="value"><select id="<?php echo esc_attr( sanitize_title($name) ); ?>" name="attribute_<?php echo sanitize_title($name); ?>">

<option value=""><?php echo __('Choose an option', 'woocommerce') ?>&hellip;</option>

<?php 

    if ( is_array( $options ) ) {

        if ( empty( $_POST ) )

            $selected_value = ( isset( $selected_attributes[ sanitize_title( $name ) ] ) ) ? $selected_attributes[ sanitize_title( $name ) ] : '';

        else

            $selected_value = isset( $_POST[ 'attribute_' . sanitize_title( $name ) ] ) ? $_POST[ 'attribute_' . sanitize_title( $name ) ] : '';

        // Get terms if this is a taxonomy - ordered

        if ( taxonomy_exists( sanitize_title( $name ) ) ) {

            $terms = get_terms( sanitize_title($name), array('menu_order' => 'ASC') );

            foreach ( $terms as $term ) {

                if ( ! in_array( $term->slug, $options ) ) continue;

                echo '<option value="' . $term->slug . '" ' . selected( $selected_value, $term->slug ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>';

            }

        } else {

            foreach ( $options as $option )

                echo '<option value="' . $option . '" ' . selected( $selected_value, $option ) . '>' . apply_filters( 'woocommerce_variation_option_name', $option ) . '</option>';

        }

    }

?>

相关 JavaScript。我将“选择”元素更改为“输入:无线​​电”,但它仍然不起作用。

jQuery(文档).ready(函数($) {

/**
 * Variations form handling
 */
$('form.variations_form')

    // On clicking the reset variation button
    .on( 'click', '.reset_variations', function( event ) {

        $(this).closest('form.variations_form').find('.variations select').val('').change();

        return false;
    } )

    // Upon changing an option
    .on( 'change', '.variations select', function( event ) {

        $variation_form = $(this).closest('form.variations_form');
        $variation_form.find('input[name=variation_id]').val('').change();

        $variation_form
            .trigger( 'woocommerce_variation_select_change' )
            .trigger( 'check_variations', [ '', false ] );

        $(this).blur();

        if( $().uniform && $.isFunction( $.uniform.update ) ) {
            $.uniform.update();
        }

    } )

    // Upon gaining focus
    .on( 'focusin', '.variations select', function( event ) {

        $variation_form = $(this).closest('form.variations_form');

        $variation_form
            .trigger( 'woocommerce_variation_select_focusin' )
            .trigger( 'check_variations', [ $(this).attr('name'), true ] );

    } )

    // Check variations
    .on( 'check_variations', function( event, exclude, focus ) {
        var all_set             = true;
        var any_set             = false;
        var showing_variation   = false;
        var current_settings    = {};
        var $variation_form     = $(this);
        var $reset_variations   = $variation_form.find('.reset_variations');

        $variation_form.find('.variations select').each( function() {

            if ( $(this).val().length == 0 ) {
                all_set = false;
            } else {
                any_set = true;
            }

            if ( exclude && $(this).attr('name') == exclude ) {

                all_set = false;
                current_settings[$(this).attr('name')] = '';

            } else {

                // Encode entities
                value = $(this).val()
                    .replace(/&/g, '&amp;')
                    .replace(/"/g, '&quot;')
                    .replace(/'/g, '&#039;')
                    .replace(/</g, '&lt;')
                    .replace(/>/g, '&gt;');

                // Add to settings array
                current_settings[ $(this).attr('name') ] = value;
            }

        });

        var product_id          = parseInt( $variation_form.attr( 'data-product_id' ) );
        var all_variations      = window[ "product_variations_" + product_id ];

        // Fallback
        if ( ! all_variations ) 
            all_variations = window[ "product_variations" ];

        var matching_variations = find_matching_variations( all_variations, current_settings );

        if ( all_set ) {

            var variation = matching_variations.pop();

            if ( variation ) {

                // Found - set ID
                $variation_form
                    .find('input[name=variation_id]')
                    .val( variation.variation_id )
                    .change();

                $variation_form.trigger( 'found_variation', [ variation ] );

            } else {

                // Nothing found - reset fields
                $variation_form.find('.variations select').val('');

                if ( ! focus )
                    $variation_form.trigger( 'reset_image' );

            }

        } else {

            $variation_form.trigger( 'update_variation_values', [ matching_variations ] );

            if ( ! focus )
                $variation_form.trigger( 'reset_image' );

            if ( ! exclude ) {
                $variation_form.find('.single_variation_wrap').slideUp('200');
            }

        }

        if ( any_set ) {

            if ( $reset_variations.css('visibility') == 'hidden' )
                $reset_variations.css('visibility','visible').hide().fadeIn();

        } else {

            $reset_variations.css('visibility','hidden');

        }

    } )

    // Reset product image
    .on( 'reset_image', function( event ) {

        var $product        = $(this).closest( '.product' );
        var $product_img    = $product.find( 'div.images img:eq(0)' );
        var $product_link   = $product.find( 'div.images a.zoom:eq(0)' );
        var o_src           = $product_img.attr('data-o_src');
        var o_title         = $product_img.attr('data-o_title');
        var o_href          = $product_link.attr('data-o_href');

        if ( o_src && o_href && o_title ) {
            $product_img
                .attr( 'src', o_src )
                .attr( 'alt', o_title )
                .attr( 'title', o_title );
            $product_link
                .attr( 'href', o_href );
        }

    } )

    // Disable option fields that are unavaiable for current set of attributes
    .on( 'update_variation_values', function( event, variations ) {

        $variation_form = $(this).closest('form.variations_form');

        // Loop through selects and disable/enable options based on selections
        $variation_form.find('.variations select').each(function( index, el ){

            current_attr_select = $(el);

            // Disable all
            current_attr_select.find('option:gt(0)').attr('disabled', 'disabled');

            // Get name
            var current_attr_name   = current_attr_select.attr('name');

            // Loop through variations
            for ( num in variations ) {

                var attributes = variations[ num ].attributes;

                for ( attr_name in attributes ) {

                    var attr_val = attributes[ attr_name ];

                    if ( attr_name == current_attr_name ) {

                        if ( attr_val ) {

                            // Decode entities
                            attr_val = $("<div/>").html( attr_val ).text();

                            // Add slashes
                            attr_val = attr_val.replace(/'/g, "\\'");
                            attr_val = attr_val.replace(/"/g, "\\\"");

                            // Compare the meercat
                            current_attr_select.find('option[value="' + attr_val + '"]').removeAttr('disabled');

                        } else {
                            current_attr_select.find('option').removeAttr('disabled');
                        }

                    }

                }

            }

        });

        // Custom event for when variations have been updated
        $variation_form.trigger('woocommerce_update_variation_values');

    } )

    // Show single variation details (price, stock, image)
    .on( 'found_variation', function( event, variation ) {
        var $variation_form = $(this);

        var $product        = $(this).closest( '.product' );
        var $product_img    = $product.find( 'div.images img:eq(0)' );
        var $product_link   = $product.find( 'div.images a.zoom:eq(0)' );

        var o_src           = $product_img.attr('data-o_src');
        var o_title         = $product_img.attr('data-o_title');
        var o_href          = $product_link.attr('data-o_href');

        var variation_image = variation.image_src;
        var variation_link = variation.image_link;
        var variation_title = variation.image_title;

        $variation_form.find('.variations_button').show();
        $variation_form.find('.single_variation').html( variation.price_html + variation.availability_html );

        if ( ! o_src ) {
            o_src = ( ! $product_img.attr('src') ) ? '' : $product_img.attr('src');
            $product_img.attr('data-o_src', o_src );
        }

        if ( ! o_href ) {
            o_href = ( ! $product_link.attr('href') ) ? '' : $product_link.attr('href');
            $product_link.attr('data-o_href', o_href );
        }

        if ( ! o_title ) {
            o_title = ( ! $product_img.attr('title') ) ? '' : $product_img.attr('title');
            $product_img.attr('data-o_title', o_title );
        }

        if ( variation_image && variation_image.length > 1 ) {
            $product_img
                .attr( 'src', variation_image )
                .attr( 'alt', variation_title )
                .attr( 'title', variation_title );
            $product_link
                .attr( 'href', variation_link );
        } else {
            $product_img
                .attr( 'src', o_src )
                .attr( 'alt', o_title )
                .attr( 'title', o_title );
            $product_link
                .attr( 'href', o_href );
        }

        var $single_variation_wrap = $variation_form.find('.single_variation_wrap');

        if ( variation.sku )
             $product.find('.product_meta').find('.sku').text( variation.sku );
        else
             $product.find('.product_meta').find('.sku').text('');

        $single_variation_wrap.find('.quantity').show();

        if ( ! variation.is_in_stock && ! variation.backorders_allowed ) {
            $variation_form.find('.variations_button').hide();
        }

        if ( variation.min_qty )
            $single_variation_wrap.find('input[name=quantity]').attr( 'data-min', variation.min_qty ).val( variation.min_qty );
        else
            $single_variation_wrap.find('input[name=quantity]').removeAttr('data-min');

        if ( variation.max_qty )
            $single_variation_wrap.find('input[name=quantity]').attr('data-max', variation.max_qty);
        else
            $single_variation_wrap.find('input[name=quantity]').removeAttr('data-max');

        if ( variation.is_sold_individually == 'yes' ) {
            $single_variation_wrap.find('input[name=quantity]').val('1');
            $single_variation_wrap.find('.quantity').hide();
        }

        $single_variation_wrap.slideDown('200').trigger( 'show_variation', [ variation ] );

    } );

/**
 * Initial states and loading
 */
$('form.variations_form .variations select').change();


/**
 * Helper functions for variations
 */

// Search for matching variations for given set of attributes
function find_matching_variations( product_variations, settings ) {
    var matching = [];

    for (var i = 0; i < product_variations.length; i++) {
        var variation = product_variations[i];
        var variation_id = variation.variation_id;

        if ( variations_match( variation.attributes, settings ) ) {
            matching.push(variation);
        }
    }
    return matching;
}

// Check if two arrays of attributes match
function variations_match( attrs1, attrs2 ) {
    var match = true;
    for ( attr_name in attrs1 ) {
        var val1 = attrs1[ attr_name ];
        var val2 = attrs2[ attr_name ];
        if ( val1 !== undefined && val2 !== undefined && val1.length != 0 && val2.length != 0 && val1 != val2 ) {
            match = false;
        }
    }
    return match;
}

});

几年后,官方仓库中有一个解决方案:http://wordpress.org/plugins/woocommerce-radio-buttons/ http://wordpress.org/plugins/woocommerce-radio-buttons/

缺少的部分是插件出队是 WooCommerceadd-to-cart-variation.js脚本并加载自己的脚本,以便通过更改单选按钮输入来触发正确的事件。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 Woo Commerce 下拉菜单转换为单选按钮 的相关文章

  • PHP 无法加载动态库“php_pdo_oci.dll”

    我在 Windows 8 上运行 Apache 2 4 7 和 PHP 5 5 9 我安装了 PHPUnit 并开始弹出此警告图像 警告 是的 我在 php ini 中启用了扩展加载以及 extension dir 以更正文件夹 并且该文件
  • VBA / HTML / jQuery 选择自动完成 - 在列表中选择

    我正在尝试使用 Excel 中的 VBA 在网站的列表中选择一个值 这不是一个 正常列表 该网站使用 jQuery 选择自动完成 如下所示 example http davidwalsh name demo jquery chosen ph
  • 如何在ASP.NET Webform中使用Jquery表单插件?

    我遇到了这个插件 http malsup com jquery form getting started http malsup com jquery form getting started 我想知道如何在 ASP NET WebForm
  • 非 DOM 对象上的 jQuery 自定义事件

    我最近阅读了一些代码 其功能如下 bob name Bob Smith rank 7 bob bind nameChanged function bob trigger nameChanged 这似乎有效 但我在 jQuery 文档或源代码
  • 在网页上的文本框中键入内容时删除所有空格

    我如何在用户打字时即时删除输入到文本框中的空格 function var txt myTextbox var func function txt val txt val replace s g txt keyup func blur fun
  • 在 WooCommerce 中添加到购物车之前清空购物车

    我正在使用 WP 作业管理器和 Woo Subscriptions Now 最初 我选择了一个套餐 Woo Subscription 然后我添加了所有细节 但没有提交 回到网站 所以要再次购买 我需要选择一个套餐 于是我选择了套餐并填写了详
  • jQuery 选择 # id 以单词为前缀,计数器为后缀

    有没有办法用 jQuery 选择所有带有前缀 my 和后缀 0 9 的 id 像这样的 my 1 4 还是可以用循环来实现 div div div div div div div div div div 第一个想法 似乎效果很好 div i
  • 如何在 Zend MVC 中实现 SSL

    我之前已经通过使用特定的安全文件夹 例如服务器上的 https 文件夹与 http 文件夹 实现了安全页面 我已经开始使用 Zend Framework 并希望应用程序的某些部分 例如登录 使用 https 我在谷歌上搜索过 甚至在这里搜索
  • PHP print_r() 中 _r 的含义是什么?

    我见过这个答案 https stackoverflow com questions 13103410 what does r suffix mean就这样 但我不确定它对于 PHP 是否相同 如果是 可重入的含义是什么 From PHP n
  • 如何通过php获取网页的Open Graph协议?

    PHP 有一个简单的命令来获取网页的元标记 get meta tags 但这仅适用于具有名称属性的元标记 然而 开放图谱协议如今变得越来越流行 从网页获取 opg 值的最简单方法是什么 例如 我看到的基本方法是通过 cURL 获取页面并使用
  • 如何让 jquery Tooltipster 插件适用于新创建的 DOM 元素?

    我正在使用 Tooltipster 插件http calebjacob com tooltipster http calebjacob com tooltipster 这很棒 但我已经动态生成了插入到 DOM 中的内容 工具提示程序似乎没有
  • 未捕获的错误:找不到模块“jquery”

    我在用Electron https github com atom electron制作桌面应用程序 在我的应用程序中 我正在加载一个外部站点 Atom 应用程序之外 可以说http mydummysite index html http
  • 如何通过ssh检查ubuntu服务器上是否存在php和apache

    如何通过ssh检查Ubuntu服务器上apache是 否安装了php和mysql 另外如果安装的话在哪个目录 如果安装了其他软件包 例如 lighttpd 那么它在哪里 确定程序是否已安装的另一种方法是使用which命令 它将显示您正在搜索
  • 如何删除文件

    我们有一个脚本 scripts ourscript php和一个文件 media movie1 flv 当我们运行时 我们如何删除这个文件ourscript php Using unlink http php net manual en f
  • IE 中的 jQuery .width(val) 错误 - 无效参数

    通过ajax加载内部div book table 后 我想调整正文的宽度以适应更大的内容 var new width parseInt book table css width 407 body width new width 在 FF 和
  • 如何将送货地址复制到帐单地址

    我想知道是否可以将送货地址复制到帐单地址 当用户单击与送货地址相同的复选框时 送货地址值将被复制到账单输入字段 我完成了大部分部分 但我不确定如何将选择菜单 状态 值复制到帐单地址 我真的很感谢任何帮助 My code document r
  • 我可以在 PHP 会话变量中安全地存储用户名和密码吗?

    我想在 REST api 之上制作一个轻量级的 web 应用程序 用户只需进行一次身份验证 从那时起 所有针对 web api 的请求都希望通过以某种方式保持用户名和密码有效来完成 我已经做了一个工作原型我在哪里将用户名和密码存储在会话变量
  • 如何在jquery中获取保存时间和当前时间的差异?

    我想在 javascript 或 jquery 中获取保存时间和当前时间之间的时差 我节省的时间看起来像Sun Oct 24 15 55 56 GMT 05 30 2010 java中的日期格式代码如下 String newDate 201
  • PHP递归遍历对象树[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 如何清除 APC 缓存而不使 Apache 崩溃?

    如果 APC 存储大量条目 清除它们会导致 httpd 崩溃 如果 apc clear cache user 花费的时间超过 phps max execution time 调用 apc clear cache 的脚本 将在之前被 php

随机推荐

  • 如果未安装.NET 3.5,如何避免 FileNotFoundException?

    如果您尝试在未安装此版本 NET Framework 的 Windows 计算机上启动 NET 3 5 应用程序 您会收到FileNotFoundException对于某些系统程序集 例如 System Core 3 5 0 0 是否可以捕
  • 将 UIButton 旋转 360 度

    我一直在尝试运行一个旋转我的动画UIButton360 度使用此代码 UIView animateWithDuration 3 0 animations self vineTimeCapButton transform CGAffineTr
  • 将对象设置为 nil 时不会调用 dealloc 方法

    我有个问题 我首先创建了一个扩展 NSObject 的对象 我提供了描述和 dealloc 方法的重写 这是我的 Employee m 文件 implementation Employee NSString description retu
  • 错误:不兼容的类型:Fragment 无法转换为 MapFragment

    在我的项目中 我使用带有选项卡的导航抽屉 在其中一个选项卡中 我想调用谷歌地图 但我遇到了这个错误 Error incompatible types Fragment cannot be converted to MapFragment 包
  • .NET Core DI,为包注册默认实现

    如何使用 NET Core 的 IoC 容器注册默认实现 并提供一种覆盖现有实现的方法 例如 我可能想创建一个包 为某些服务提供默认实现 namesapce Package public interface ISomeService pub
  • 使用脚本打印 Google Sheet 中下拉列表中的所有选项 [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 有没有人有任何脚本可以让我使用 Google Sheet 中的应用程序脚本打印 Drop Down 中的
  • 如何跳过在 htmlwidgets::saveWidget() 中编写依赖项?

    当用plotly可视化数据时 我想将小部件编写为html文档 而不需要htmlwidgets saveWidget每次都编写依赖项 假设这些依赖项已经就位 以节省处理时间 小部件需要是独立的以节省磁盘空间 library plotly t
  • YouTube API 配额为零 [重复]

    这个问题在这里已经有答案了 我正在尝试使用 youtube api 来收集项目的一些数据 为此 我使用带有从电子邮件帐户获取的刷新令牌的微服务来生成 accessToken 但是 每当我使用生成的令牌时 我都会收到一条回复 说我已达到配额上
  • primefaces bar 图表自定义 x 轴

    我的应用程序中有 p barchart 图 类似于展示案例中的第二个条形图 http www primefaces org showcase ui barChart jsf http www primefaces org showcase
  • C++ 布尔值短路

    我是 C 新手 很好奇编译器如何处理布尔值的惰性求值 例如 if A 1 B 2 如果 A 等于 1 B 2 部分是否会被求值 No the B 2部分不予评价 这就是所谓的短路评估 http en wikipedia org wiki S
  • 在 Unity3D 中向着色器添加 alpha

    我对着色器编程一无所知 但现在我需要将 alpha 添加到我想要使用的着色器中 实际上我想淡入和淡出我的精灵 但它不在我使用的着色器中 Shader Shader Sprites ClipArea2Sides Properties Main
  • 如何将javascript中的数字格式化为两位小数?

    我需要在 JavaScript 中将数字格式化为两位小数 为了做到这一点 我使用 toFixed 方法 该方法工作正常 但在数字没有任何小数位的情况下 它不应显示小数点 例如10 00 应该只是 10 而不是 10 00 toFixed 将
  • 以 HTML 形式发送 jasperreport

    我在用JapserReports用于在 Java 中显示报告 我可以发送报告PDF格式到网络浏览器 现在我想将报告发送到HTML格式 以下是我发送报告的方法HTML format protected void processRequest
  • 使用 C++ 中的 openssl 以编程方式生成 CA 证书

    我想使用 openssl 和 C 生成 CA 证书 然后用它来签署证书 所以我实现了一个生成证书的函数 std shared ptr
  • 在主线程的镀铬块中同时录制视频和音频,导致音频无效

    所以 我认为这是一个相当有趣的问题 希望这不是一个棘手的问题 我有一个正在 Chrome 中录制的音频 视频 getUserMedia 流 单独来看 曲目录制得非常好 然而 当尝试同时记录两者时 其中一个会阻塞主线程 从而阻塞另一个线程 我
  • 如何处理后台和前台的 firebase 通知?

    我想在后台和前台处理 firebase 通知消息 我将发送一条消息 其中包含来自开发人员的 YouTube 链接 当用户点击通知栏时 它必须引导用户打开该链接 有谁知道它是如何完成的 public void onMessageReceive
  • 如何仅选择/格式化字典中的值到列表或 numpy 数组中?

    如何让它只打印平均值列表 我只需要它与我的 np 格式完全相同 数组 以便我可以比较它们以查看它们是否相同 Code import numpy as np from pprint import pprint centroids np arr
  • 跟踪 aws lambda/cloudwatch 日志

    了解如何访问 lambda 日志另一个答案 https stackoverflow com questions 47846959 how do you look at console log output of the amazon lam
  • 可以让 jqGrid 搜索框留在页面上吗?

    现在 我必须单击 jqGrid 搜索图标才能弹出搜索框 我想做的是始终在网格上方打开搜索框 而不是弹出窗口 我在他们的演示中没有看到任何内容 但我希望有人已经做到了或知道如何做到 做你需要的最简单的方法是 var grid list prm
  • 将 Woo Commerce 下拉菜单转换为单选按钮

    我在将变体下拉列表转换为支持 woocommerce 的网站的单选按钮时遇到问题 我已经尝试找到答案here https stackoverflow com questions 11819604 dropdown converted to