【问题标题】:Updating Client Woocommerce Shipping Address before checkout page在结帐页面之前更新客户 Woocommerce 送货地址
【发布时间】:2018-02-08 13:32:15
【问题描述】:

我想在主页上弹出一个简单的模型,让用户在结帐/购物车页面之前选择他们的送货国家。

我制作了一个小的选择框弹出模型,然后使用 AJAX 运行此功能以更改用户发货国家/地区,但它没有更新。

// 本地化

//*********** Ajax Change shipping country on press ***********/

function localize_array() {

$localize = array(
    'ajax_url' => admin_url( 'admin-ajax.php' ),
    'nonce'    => wp_create_nonce( 'yo-ajax-call-ak' ),
);

return $localize;

}

function ajax_change_shipping() {
wp_enqueue_script( 'ak-country-change', get_template_directory_uri() . '/inc/js/select-country.js', array( 'jquery', ), '1.0', true );
wp_localize_script( 'ak-country-change', 'akselect', localize_array() );
}

add_action( 'wp_enqueue_scripts', 'ajax_change_shipping' );

// ajax 调用

$(document).ready(function () {

    $('body').on('change', '.yo-lang-select', function () {

        var select = $(this);
        var value = select.val();
        $.ajax({
            url: akselect.ajax_url,
            method: 'POST',
            dataType: 'json',
            data: {
                action: 'get_the_defualt_lang',
                value: value
                nonce: akselect.nonce
            },
            success: function (respond) {
                console.log(respond);
            },
            error: function (jqXHR, textStatus, errorThrown) {

                console.log(jqXHR, textStatus, errorThrown);

            }
        });
    });

});

//这是函数

add_action( 'wp_ajax_get_the_defualt_lang', 'get_the_defualt_lang' );
add_action( 'wp_ajax_nopriv_get_the_defualt_lang','get_the_defualt_lang' );

function get_the_defualt_lang() {

$lang = $_POST['value'];

add_action( 'woocommerce_add_to_cart', 'set_country_befor_cart_page' );
function set_country_befor_cart_page() {

    WC()->customer->set_country( $lang );
    WC()->customer->set_shipping_country( $lang );

}

$respond = $lang;

echo json_encode( $respond );


die();

}

//弹出表单

<div data-effect="mfp-zoom-out" id="shop-pop_1" class="mfp-hide">
<select class="yo-lang-select" data-type="lang" title="">
    <option value="US">United States</option>
    <option value="GB">United Kingdom (UK)</option>
    <option value="US">Other Countries</option>

</select>

// 使用钩子将 from 插入到商店页面

add_action( 'woocommerce_after_shop_loop', 'ak_store_popup' );

function ak_store_popup() {
  get_template_part( 'template-parts/popups/popup', 'store-main' );
}

【问题讨论】:

  • 我在结帐前添加了表单的代​​码和挂钩以在商店页面中获取它,希望对您有所帮助,感谢您的反馈
  • 我现在添加了,谢谢

标签: php jquery ajax wordpress woocommerce


【解决方案1】:

更新:通过一些额外的更改成功测试:

  • jQuery:加载带有选择字段的页面时,现在通过 ajax 设置“US”默认值。
  • 选择字段:只需 2 个值(默认值:'US' | 另一个:'GB')。您可以添加许多其他人。
  • 国家/地区现在设置正确(帐单国家/地区是必需的)。

1) jQuery 代码:

带有jQuery 的Wordpress 有一些特别之处,就是短地$ 不起作用……所以要使它起作用,您的jQuery 代码需要从以下开始:@ 987654323@

jQuery(document).ready(function ($) {
    var select = $('.yo-lang-select'),
        value = select.val();

    function ajax_update( value ){
        $.ajax({
            url: akselect.ajax_url,
            method: 'POST',
            dataType: 'json',
            data: {
                action: 'get_the_defualt_lang',
                value: value
                //nonce: akselect.nonce
            },
            success: function (respond) {
                console.log(respond);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(jqXHR, textStatus, errorThrown);
            }
        });
    }

    ajax_update( value );

    $('body').on('change', select, function () {
        value = select.val();
        ajax_update( value );
    });
});

2) HTML 选择字段:

<div data-effect="mfp-zoom-out" id="shop-pop_1" class="mfp-hide">
<select class="yo-lang-select" data-type="lang" title="">
    <option value="US">United States</option>
    <option value="GB">United Kingdom (UK)</option>
</select>

3) PHP 函数:

  • 现在您得到了正确的响应,并且 ajax 工作正常。
  • 另一个问题是使用 set_country_befor_cart_page 挂钩函数 woocommerce_add_to_cart 嵌入在您的 php ajax 脚本中,因为它永远不会运行,也永远不会获得 $lang
  • 我添加了帐单国家/地区,以真正设置国家/地区主要用于结帐...

所以不如这样使用它:

add_action( 'wp_ajax_get_the_defualt_lang', 'get_the_defualt_lang' );
add_action( 'wp_ajax_nopriv_get_the_defualt_lang','get_the_defualt_lang' );
function get_the_defualt_lang() {

    $lang = $_POST['value'];

    WC()->customer->set_country( $lang );
    WC()->customer->set_billing_country( $lang ); // Updated HERE Too
    WC()->customer->set_shipping_country( $lang );

    echo json_encode( $lang );

    die();
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

这一次,这是经过测试和工作的。

  • 当您的选择字段加载到弹出窗口中时,您可能需要在 jQuery 中进行一些调整来处理此事件。
  • 您可能需要使用if ( ! is_user_logged_in() ) { 限制您的脚本,仅适用于未登录/注册的客户

【讨论】:

  • 该功能仍然没有更新客户国家/地区。
  • 现在工作正常,感谢您的帮助!
  • 从我的角度来看,这看起来不错,但是当我调用 WC()->cart->get_cart_shipping_total();设置新的运输国家后,我仍然得到错误的运输价格......有什么提示吗?
  • 我可以给你+1000000。 .这工作得很好。我一直在寻找这个“WC()->customer->set_....”,我在这里找到了它。谢谢兄弟
猜你喜欢
  • 2021-11-12
  • 1970-01-01
  • 2015-09-17
  • 2016-10-31
  • 1970-01-01
  • 2014-04-04
  • 2021-10-10
  • 2017-10-02
  • 2017-11-09
相关资源
最近更新 更多