【问题标题】:Update cart shipping data with AJAX in WooCommerce在 WooCommerce 中使用 AJAX 更新购物车运输数据
【发布时间】:2019-09-16 10:38:50
【问题描述】:

我正在尝试使用 AJAX 更新我的结帐购物车的发货...

我已经在functions.php中找到了动作

function jwd_update_shipping()
{
    WC()->cart->calculate_shipping();

    echo "hi";
    die();
}
add_action('jwd_update_shipping_callback', 'jwd_update_shipping');

然后在js中我这样称呼它,像这样

jQuery.ajax({
    type: "POST",
    url: 'MYSITE.com/wp-admin/admin-ajax.php',
    data: ({
        action: "jwd_update_shipping"
    }),
    success: function(response) {
        console.log("got this: " + response);
        if (response.type == "success") {
            alert("here");
            jQuery('body').trigger('update_checkout');
        } else {
            alert("fail");
        }
    },
    error: function(request, status, error) {
        alert(request.responseText);
    }
});

我刚刚收到一个 0 警报,这表明 AJAX 调用失败。

【问题讨论】:

  • 在计算运费之前放一个回声,它实际上是否进入了 jwd_update_shipping .. 也可以在 ajax 中为动作数据尝试括号 ()
  • 您的 add_action() 错误。请阅读文档 - codex.wordpress.org/AJAX_in_Plugins

标签: php jquery ajax woocommerce checkout


【解决方案1】:

首先,您的 ajax 请求没有到达相关的 php 函数……此外,您还需要更多的东西来刷新结帐……请尝试以下操作:

// The jQuery script that send the Ajax request
add_action( 'wp_footer', 'refresh_shipping_js' );
function refresh_shipping_js() {
    // Only on checkout
    if( is_checkout() && ! is_wc_endpoint_url() ):
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined') 
            return false;

        var refresh = 'yes';

        $.ajax({
            type: "POST",
            url: wc_checkout_params.ajax_url,
            data: ({
                'action': 'updating_shipping',
                'refresh_shipping': refresh,

            }),
            success: function(response) {
                if( response === '1' ) {
                    $(document.body).trigger('update_checkout');
                    console.log('Success: '+response); // For testing (to be removed)
                } else {
                    console.log('Failled: '+response); // For testing (to be removed)
                }
            },
            error:function(error) {
                console.log('Error: '+error); // For testing (to be removed)
            }
        });
    });
    </script>
    <?php
    endif;
}

// function that gets the Ajax data
add_action( 'wp_ajax_updating_shipping', 'updating_shipping' );
add_action( 'wp_ajax_nopriv_updating_shipping', 'updating_shipping' );
function updating_shipping() {
    if ( isset($_POST['refresh_shipping']) && $_POST['refresh_shipping'] === 'yes' ){
        WC()->session->set('refresh_shipping', '1' );
    } else {
        WC()->session->set('refresh_shipping', '0' );
    }
    echo  WC()->session->get('refresh_shipping');
    die(); // Alway at the end (to avoid server error 500)
}

// Function that refresh session shipping methods data
add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods', 10, 1 );
function refresh_shipping_methods( $post_data ){
    if ( WC()->session->get('refresh_shipping' ) === '1' ) {
        foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
            WC()->session->set( 'shipping_for_package_' . $package_key, false );
        }
        WC()->cart->calculate_shipping();
    }
}

【讨论】:

    猜你喜欢
    • 2016-10-09
    • 2018-07-29
    • 2017-01-11
    • 2017-01-18
    • 2017-04-24
    • 2017-01-20
    • 1970-01-01
    • 2016-10-31
    • 2014-01-20
    相关资源
    最近更新 更多