在Woocommerce Variable产品上的jQuery中获取选定的变化价格

时间:2019-02-28 13:05:27

标签: javascript php jquery wordpress woocommerce

如何使用变体ID使用JavaScript查找变体价格? 这就是到目前为止。我有变体ID,但我不知道。我已经搜索了几个小时。

add_action( 'woocommerce_before_add_to_cart_quantity', 'func_option_valgt' );
function func_option_valgt() {

    global $product;
    global $woocommerce;

    if ( $product->is_type('variable') ) {

        ?>
        <script>
        jQuery(document).ready(function($) {

            $('input').change( function(){
                if( '' != $('input.variation_id').val() ) {

                    var var_id = $('input.variation_id').val();

                    var var_length = $('#cfwc-title-field').val(); //LENGTH

                    var var_diameter =$('#diameter').val();  //DIAMETER

                    alert('Du valgte variant #' + var_id + "    Lengde: " + var_length + "     Diameter: " + var_diameter + "    Variantpris: " + var_variant_pris);    
                }
            });
        });
        </script>
        <?php
    }
}

任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

通过以下重新访问的代码,您将在jQuery脚本中获得版本ID的价格:

add_action( 'woocommerce_before_add_to_cart_quantity', 'func_option_valgt' );
function func_option_valgt() {
    global $product;

    if ( $product->is_type('variable') ) {
        $variations_data =[]; // Initializing

        // Loop through variations data
        foreach($product->get_available_variations() as $variation ) {
            // Set for each variation ID the corresponding price in the data array (to be used in jQuery)
            $variations_data[$variation['variation_id']] = $variation['display_price'];
        }
        ?>
        <script>
        jQuery(function($) {
            var jsonData = <?php echo json_encode($variations_data); ?>,
                inputVID = 'input.variation_id';

            $('input').change( function(){
                if( '' != $(inputVID).val() ) {
                    var vid      = $(inputVID).val(), // VARIATION ID
                        length   = $('#cfwc-title-field').val(), // LENGTH
                        diameter = $('#diameter').val(),  // DIAMETER
                        vprice   = ''; // Initilizing

                    // Loop through variation IDs / Prices pairs
                    $.each( jsonData, function( index, price ) {
                        if( index == $(inputVID).val() ) {
                            vprice = price; // The right variation price
                        }
                    });

                    alert('variation Id: '+vid+' | Lengde: '+length+' | Diameter: '+diameter+' | Variantpris: '+vprice);
                }
            });
        });
        </script>
        <?php
    }
}

代码在您的活动子主题(或活动主题)的function.php文件上。经过测试,可以正常工作。

答案 1 :(得分:0)

更新代码以更新显示所选价格的类。

add_action( 'woocommerce_before_add_to_cart_quantity', 'func_option_valgt' );
function func_option_valgt() {
    global $product;

    if ( $product->is_type('variable') ) {
        $variations_data =[]; // Initializing

        // Loop through variations data
        foreach($product->get_available_variations() as $variation ) {
            // Set for each variation ID the corresponding price in the data array (to be used in jQuery)
            $variations_data[$variation['variation_id']] = $variation['display_price'];
        }
        ?>
        <script>
        jQuery(function($) {
            var jsonData = <?php echo json_encode($variations_data); ?>,
                inputVID = 'input.variation_id';

            $('input').change( function(){
                if( '' != $(inputVID).val() ) {
                    var vid      = $(inputVID).val(), // VARIATION ID
                        length   = $('#cfwc-title-field').val(), // LENGTH
                        diameter = $('#diameter').val(),  // DIAMETER
                        vprice   = ''; // Initilizing

                    // Loop through variation IDs / Prices pairs
                    $.each( jsonData, function( index, price ) {
                        if( index == $(inputVID).val() ) {
                            vprice = price; // The right variation price
                        }
                    });

                    //alert('variation Id: '+vid+' | Lengde: '+length+' | Diameter: '+diameter+' | Variantpris: '+vprice);
                    // Update the need class to show price
                    var csymbol = '&pound;'
                    // SET YOUR CURRANCY SYMBOL ABOVE
                    $(".availability").html(csymbol+vprice);
                }
            });
        });
        </script>
        <?php
    }
}