首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Woocommerce结帐中添加基于国家的弹出和自定义消息

在Woocommerce结帐中添加基于国家的弹出和自定义消息
EN

Stack Overflow用户
提问于 2018-10-16 10:38:16
回答 1查看 3.8K关注 0票数 1

当客户选择一个国家时,我使用下面的代码很好地为woocommerce结帐添加了一条消息。但是我也想使用这个短代码添加一个弹出。

代码语言:javascript
运行
复制
echo do_shortcode('[sg_popup id=3839] ');

但是,当我将它添加到文本消息下面时,弹出总是显示出来,也就是说,如果我添加了以下内容

代码语言:javascript
运行
复制
`echo '<div class="shipping-notice woocommerce-info"   style="display:none">Please allow 5-10 business days for delivery after order processing.'; echo do_shortcode('[sg_popup id=3839] '); echo '</div>'

我想这是因为它只是隐藏代码,所以它实际上运行的是短代码?但我还有别的选择吗?

任何帮助都是非常感谢的。

代码语言:javascript
运行
复制
add_action( 'woocommerce_before_checkout_billing_form', 'display_shipping_notice' );
function display_shipping_notice() {
    echo '<div class="shipping-notice woocommerce-info" style="display:none">Please allow 5-10 business days for delivery after order processing.</div>';
}

add_action( 'woocommerce_after_checkout_form', 'show_hide_shipping_notice' );
function show_hide_shipping_notice(){
    ?>
    <script>
        jQuery(document).ready(function($){
            // Set the country code (That will display the message)
            var countryCode = 'FR';

            $('select#billing_country').change(function(){
                selectedCountry = $('select#billing_country').val();

                if( selectedCountry == countryCode ){
                    $('.shipping-notice').hide();
                }
                else {
                    $('.shipping-notice').show();
                }
            });
        });
    </script>
    <?php
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-16 13:51:21

以下代码将显示或隐藏自定义发货通知,具体取决于所选国家(代码同时处理计费国和发货国)…

要替换您的短代码,您可以使用弹出窗口,比如使用甜警报2 (一个照明盒)。

代码语言:javascript
运行
复制
// Displaying a custom shipping notice
add_action( 'woocommerce_checkout_before_customer_details',  'checkout_country_shipping_notice' );
function checkout_country_shipping_notice() {
    ?>
    <style>.shipping-notice.hidden{display:none;}</style>
    <?php
    $country = WC()->customer->get_shipping_country();
    $country = empty($country) ? WC()->customer->get_billing_country() : $country;

    $text  = __("Please allow 5-10 business days for delivery after order processing.", "woocommerce" );
    $class = 'woocommerce-info shipping-notice';

    // Hidden if France or empty
    if( empty($country) || $country == 'FR' )
        $class .= ' hidden';

    echo '<div class="'.$class.'">'.$text.'</div>';
}

// The jQuery code to show / hide the custom shipping notice
add_action( 'wp_footer', 'checkout_country_script' );
function checkout_country_script() {
    // Only checkout page
    if( is_checkout() && ! is_wc_endpoint_url() ):
    ?>
    <script src="https://unpkg.com/sweetalert2@8.8.1/dist/sweetalert2.all.min.js"></script>
    <script src="https://unpkg.com/promise-polyfill@8.1.0/dist/polyfill.min.js"></script>
    <script type="text/javascript">
    jQuery(function($){
        var fc  = 'form.checkout',              sn = '.shipping-notice',
            bc  = 'select#billing_country',     sc = 'select#shipping_country',
            sda = 'input#ship-to-different-address-checkbox';

        // Function that show hide the shipping notice
        function showHideNotice(t){
            if( $(t).val() == 'FR' || $(t).val() == undefined ){
                $(sn).hide( function(){
                    $(this).removeClass('hidden');
                });
            } else {
                $(sn).hide( 'fast', function(){
                    if( ! $(this).hasClass('hidden') )
                        $(this).addClass('hidden');
                    $(sn).show();

                    // Add a Sweet alert 2 popup
                    swal({
                        title:  '<?php _e("Custom popup title", "woocommerce"); ?>',
                        text:   '<?php _e("This is the text for my popup", "woocommerce"); ?>',
                        type:   'info' // can be: warning, error, success, info or question
                    });
                });
            }
        }

        // Billing and shipping country change
        $(bc+','+sc).change(function(){
            if($(sda).prop('checked'))
                showHideNotice(sc);
            else
                showHideNotice(bc);
        });
    });
    </script>
    <?php
    endif;
}

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52833554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档