简体   繁体   English

在 Woocommerce 结帐中根据国家/地区添加弹出窗口和自定义消息

[英]Add pop up and a custom message based on country in Woocommerce checkout

I am using the below code which works well to add a message to the woocommerce checkout when customers choose a country.我正在使用以下代码,当客户选择一个国家/地区时,该代码可以很好地向 woocommerce 结帐添加消息。 But i also want to add a pop up using this shortcode但我也想使用这个短代码添加一个弹出窗口

echo do_shortcode('[sg_popup id=3839] ');

But when i add it below the text message the pop up always shows, ie if i add the following但是当我在短信下方添加它时,弹出窗口总是显示,即如果我添加以下内容

`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>'

I imagine its because it is only hiding the code so it is actually running the shortcode?我想这是因为它只是隐藏了代码,所以它实际上是在运行短代码? But what other options do I have?但我还有什么其他选择?

Any help is appreciated.任何帮助表示赞赏。

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
}

The following code will show or hide a custom shipping notice depending on the chosen country (the code handle both billing and shipping country)…以下代码将根据所选国家/地区显示或隐藏自定义运输通知(该代码处理帐单和运输国家/地区)...

To replace your shortcode, you can use instead a popup like using Sweet Alert 2 (a lightbox) :要替换您的短代码,您可以使用类似Sweet Alert 2 (灯箱)的弹出窗口:

// 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;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

在此处输入图片说明

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

相关问题 在 WooCommerce 结账时根据发货国家显示自定义消息 - Show custom message in WooCommerce checkout based on shipping country 如何在 WooCommerce 结账时根据国家/地区制作所需的自定义字段? - How to make a custom field required based on country on WooCommerce checkout? 在 Woocommerce Checkout 页面中添加信息丰富的自定义消息 - Add an informative custom message in Woocommerce Checkout page 在 Woocommerce Checkout 中的帐单国家/地区下方添加自定义字段 - Add a custom field below billing country in Woocommerce Checkout 结帐时自定义 WooCommerce 消息/通知 - Custom WooCommerce message/notice at checkout 在Woocommerce中根据购物车数量添加自定义结帐字段 - Add a custom checkout field based on cart items quantity in Woocommerce 基于开关切换在 WooCommerce Checkout 中添加自定义折扣 - Add Custom discount in WooCommerce Checkout based on a switch toggle 根据 Woocommerce 中的产品类别添加自定义结帐字段 - Add custom checkout field based on product category in Woocommerce 根据运输国家/地区选择Woocommerce结帐电话字段 - Make Woocommerce checkout phone field optional based on shipping country Woocommerce条件结帐字段和基于国家和购物车总额的欧盟增值税 - Woocommerce conditional checkout fields and Eu VAT based on country and cart total
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM