【问题标题】:Trigger update_checkout with custom select field in Woocommerce在 Woocommerce 中使用自定义选择字段触发 update_checkout
【发布时间】:2019-02-03 17:18:18
【问题描述】:

问题: jQuery 代码没有触发“update_checkout”。

我在订单审核表中添加了一个自定义选择字段,供客户选择小费金额,如图所示:

我想在选择值更改时触发 update_checkout,但它没有发生。如果我选择运输方式,它会触发“update_checkout”并正确更新金额百分比。

HTML:

<tr class="propina">
        <th id="propina_field">Propina</th>
            <td>
                <span class="woocommerce-input-wrapper">
                    <select name="propina" id="propina" class="select" data-placeholder="">
                        <option value="0">0</option>
                        <option value="1">1%</option>
                        <option value="2">2%</option>
                        <option value="3">3%</option>
                        <option value="5">5%</option>
                        <option value="7">7%</option>
                        <option selected value="10">10%</option>
                    </select>
                </span>
            </td>                           
</tr>

functions.php:

add_action( 'wp_head', 'woocommerce_tip' );
function woocommerce_tip() {
    ?>
    <script type="text/javascript">
    jQuery( document ).ready(function( $ ) {
        $('#propina').change(function(){
            jQuery('body').trigger('update_checkout');
        });
    });
    </script>
    <?php
}

【问题讨论】:

  • 问题是什么?还是问题?
  • 我的错误,更新的问题。问题是 jQuery 代码不会触发“update_checkout”。

标签: php jquery wordpress woocommerce checkout


【解决方案1】:

更新

为了测试你的代码,就像你的问题截图一样,我使用了以下方法来让相同的选择字段显示在正确的位置:

add_action('woocommerce_review_order_before_order_total', 'review_order_tip_line');
function review_order_tip_line() {
    ?>
    <tr class="propina"><th id="propina_field">Propina</th><td><span class="woocommerce-input-wrapper">
        <select name="propina" id="propina" class="select" data-placeholder="">
            <option value="0">0</option>
            <option value="1">1%</option>
            <option value="2">2%</option>
            <option value="3">3%</option>
            <option value="5">5%</option>
            <option value="7">7%</option>
            <option selected value="10">10%</option>
        </select>
    </td></span></tr>
    <?php
}

那么这里是你重新访问的 jQuery 代码:

add_action( 'wp_footer', 'woocommerce_tip_script' );
function woocommerce_tip_script() {
    // Only on checkout page
    if( ! is_checkout() ) return;
    ?>
    <script type="text/javascript">
    jQuery(function($){
        // On 'select#propina' change (live event)
        $('form.woocommerce-checkout').on( 'change', 'select#propina', function(){
            // Set the select value in a variable
            var a = $(this).val();

            // Update checkout event
            $('body').trigger('update_checkout');

            // Restoring the chosen option value
            $('select#propina option[value='+a+']').prop('selected', true);

            // Just for testing (To be removed)
            console.log('trigger "update_checkout"');

            // Once checkout has been updated
            $('body').on('updated_checkout', function(){
                // Restoring the chosen option value
                $('select#propina option[value='+a+']').prop('selected', true);

                // Just for testing (To be removed)
                console.log('"updated_checkout" event, restore selected option value: '+a);
            });
        });
    })
    </script>
    <?php
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试和工作。

【讨论】:

  • 谢谢你,就像一个魅力。也非常感谢您恢复选定的值,真的很有帮助!
猜你喜欢
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 2012-11-20
  • 2015-04-19
  • 2018-07-21
  • 2018-05-30
  • 2016-09-30
  • 2015-04-28
相关资源
最近更新 更多