简体   繁体   English

使用Select2而不是实际选择时,WooCommerce事件未触发

[英]WooCommerce event not triggering when using Select2 instead of actual select

I've built a custom website using Wordpress and WooCommerce and have installed Select2 to generate custom selects which is working fine. 我已经使用Wordpress和WooCommerce建立了一个自定义网站,并安装了Select2来生成自定义选择,效果很好。 The issue I am having is with some of the selects on the WooCommerce pages, specifically those that trigger an event on change. 我遇到的问题是WooCommerce页面上的某些选择,特别是那些触发更改事件的选择。

The custom selects successfully change the option selected, but the issue arises with selects that are meant to trigger an event. 定制选择成功更改了所选择的选项,但是用于触发事件的选择出现了问题。 For example, the colour variation dropdown on the product page or the 'Sort By' select on the store page. 例如,产品页面上的颜色变化下拉列表或商店页面上的“排序依据”选择。

I've looked through the WooCommerce JS files and discovered some WooCommerce specific events that are triggered when a selection is made using the actual select box but I'm not sure how to implement this when using Select2 instead. 我查看了WooCommerce JS文件,发现了一些WooCommerce特定事件,这些事件是在使用实际选择框进行选择时触发的,但是我不确定在使用Select2时如何实现此事件。

Here is a copy of the WooCommerce JS in relation to the event I'm talking about (in this case the change to the select for product variations): 这是与我正在谈论的事件有关的WooCommerce JS的副本(在本例中为产品变体的select的更改):

.on( 'change', '.variations select', function() {
        $form.find( 'input[name="variation_id"], input.variation_id' ).val( '' ).change();
        $form.find( '.wc-no-matching-variations' ).remove();

        if ( $use_ajax ) {
            if ( $xhr ) {
                $xhr.abort();
            }

            var all_attributes_chosen  = true;
            var some_attributes_chosen = false;
            var data                   = {};

            $form.find( '.variations select' ).each( function() {
                var attribute_name = $( this ).data( 'attribute_name' ) || $( this ).attr( 'name' );

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

                data[ attribute_name ] = $( this ).val();
            });

            if ( all_attributes_chosen ) {
                // Get a matchihng variation via ajax
                data.product_id = $product_id;

                $xhr = $.ajax( {
                    url: wc_cart_fragments_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_variation' ),
                    type: 'POST',
                    data: data,
                    success: function( variation ) {
                        if ( variation ) {
                            $form.find( 'input[name="variation_id"], input.variation_id' )
                                .val( variation.variation_id )
                                .change();
                            $form.trigger( 'found_variation', [ variation ] );
                        } else {
                            $form.trigger( 'reset_data' );
                            $form.find( '.single_variation_wrap' ).after( '<p class="wc-no-matching-variations woocommerce-info">' + wc_add_to_cart_variation_params.i18n_no_matching_variations_text + '</p>' );
                            $form.find( '.wc-no-matching-variations' ).slideDown( 200 );
                        }
                    }
                } );
            } else {
                $form.trigger( 'reset_data' );
            }
            if ( some_attributes_chosen ) {
                if ( $reset_variations.css( 'visibility' ) === 'hidden' ) {
                    $reset_variations.css( 'visibility', 'visible' ).hide().fadeIn();
                }
            } else {
                $reset_variations.css( 'visibility', 'hidden' );
            }
        } else {
            $form.trigger( 'woocommerce_variation_select_change' );
            $form.trigger( 'check_variations', [ '', false ] );
            $( this ).blur();
        }

        // Custom event for when variation selection has been changed
        $form.trigger( 'woocommerce_variation_has_changed' );
    } )

And then my own attempt to utilise this event: 然后我自己尝试利用此事件:

$('#pa_colour').select2();
$('#pa_colour').on('change', function(){
    var $form = $(this).parents('form');
    $form.trigger( 'woocommerce_variation_select_change' );
    $form.trigger( 'woocommerce_variation_has_changed' );
});

Unfortunately the site isn't live yet so I can't provide a link but hopefully you get the idea. 不幸的是,该站点尚未启用,因此我无法提供链接,但希望您能理解。

If someone can help me here I'd be so appreciative, I'm not exactly sure how Wordpress hooks (if this is what this is) work and I may be just missing something obvious. 如果有人可以在这里帮助我,我将不胜感激,我不太确定Wordpress钩子的工作原理(如果是这样的话),而我可能只是缺少一些明显的东西。

Thanks, Kathryn 谢谢,凯瑟琳

This isn't a solution exactly, but I ended up replacing the Select2 plugin with the Selectric plugin and that works perfectly. 完全不是一个解决方案,但是我最终用Selectric插件替换了Select2插件,并且效果很好。 Oh well! 那好吧! Thanks guys. 多谢你们。 http://lcdsantos.github.io/jQuery-Selectric/ http://lcdsantos.github.io/jQuery-Selectric/

I came across the same issue and found a solution in the last comment in this thread Select2 not showing selected value 我遇到了相同的问题,并在此线程Select2的最后一条注释中找到了解决方案, 未显示所选值

The comment by Matt inspired by Kevin suggested wrapping the select2 call in $(window).bind("load", function() {...}); Matt的灵感来自Kevin的建议,建议将select2调用包装在$(window).bind(“ load”,function(){...})中; which worked for me. 对我有用。

Kudos to those guys. 对那些家伙表示敬意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM