【问题标题】:Updating Shipping Rate on checkout page on WooCommerce site在 WooCommerce 网站的结帐页面上更新运费
【发布时间】:2019-02-03 04:33:51
【问题描述】:

我们有一个自定义代码,允许最终用户选择交货的城市、日期和时间。交货率应根据城市而​​变化。城市选择框在结帐页面上。

更改城市会触发 ajax,从而成功更改订单审核部分下显示的运费。到目前为止,一切都很好。 不幸的是,提交订单后价格未达到订单。它迷路了。

提示可能是同一城市选择框也在产品页面上,如果用户在添加到购物车时确实选择了它,然后去结帐,则会显示交付率并执行提交订单时不会丢失。

是否需要触发任何刷新才能更新运费?

更新费率的函数(成功)

function adjust_shipping_rate( $rates ){
    global $woocommerce;

        foreach ($rates as $rate) {
            $cost = $rate->cost;
            $rate->cost = $_COOKIE['shipping_city_cost'];
        }
        return $rates;
}
add_filter( 'woocommerce_package_rates', 'adjust_shipping_rate', 50, 1 );

2017 年 5 月 21 日更新

这是更新 cookie 的方式:一旦选择框被更改,异步 ajax 调用就会被触发并执行以下 PHP 代码。

function get_and_set_shipping_rate(){
    $shipping_city = $_POST['city'];
    $shipping_cost = get_shipping_cost_by_city($shipping_city);
    setcookie('shipping_city_cost', $shipping_cost, time() + (86400 * 30), '/'); 
    $_COOKIE['shipping_city_cost'] = $shipping_cost;
    echo 'Shipping cost updated: '.$shipping_city.' : '.$shipping_cost;
}

add_action( 'wp_ajax_get_and_set_shipping_rate', 'get_and_set_shipping_rate' );
add_action( 'wp_ajax_nopriv_get_and_set_shipping_rate', 'get_and_set_shipping_rate' );

这里是 ajax 调用:

jQuery(document).on('change', '#shipping_delivery_city', function(){
     var requested_city = jQuery(this).val();
        var data = {
                'action': 'get_and_set_shipping_rate',
                'city': requested_city
            };

            jQuery.ajax({
                type: "POST",
                url: shipping_dates.ajax_url,
                data: data,
                async: false,
                success: function (response) {
                    console.log(response);
                }
            });
});

【问题讨论】:

  • $_COOKIE['shipping_city_cost'] 的设置如何?
  • @Reigel 感谢您在这里提供帮助。请参阅上面的更新。
  • 这些代码只需要在结帐页面上吗?
  • 它运行良好,但是当我选择另一种运输方式(如取货)时,运输成本不会改变。任何人都可以告诉我,当我选择另一种运输方式时,运费将为零(我的意思是选择另一种运输方式没有费用。

标签: ajax wordpress cookies woocommerce


【解决方案1】:

有了这个,我有两个假设。 1.) 您在结帐页面上使用此代码。 2.) 你已经有了自己的函数get_shipping_cost_by_city

add_action( 'woocommerce_checkout_update_order_review', 'woocommerce_checkout_update_order_review' );
function woocommerce_checkout_update_order_review( $post_data ){

    $data = array();
    $vars = explode('&', $post_data);
    foreach ($vars as $k => $value){
        $v = explode('=', urldecode($value));
        $data[$v[0]] = $v[1];
    }
    $shipping_cost = get_shipping_cost_by_city( $data['billing_city'] );
    WC()->session->set( 'shipping_city_cost', $shipping_cost );
    foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ) {
        // this is needed for us to remove the session set for the shipping cost. Without this, we can't set it on the checkout page.
        WC()->session->set( 'shipping_for_package_' . $package_key, false );
    }

}

add_filter( 'woocommerce_package_rates', 'adjust_shipping_rate', 50 );
function adjust_shipping_rate( $rates ){

    foreach ($rates as $rate) {
        $cost = $rate->cost;
        $rate->cost = WC()->session->get( 'shipping_city_cost' );
    }
    return $rates;
}

shipping_city_cost 的设置/获取由WC()->session->set 和WC()->session->get 完成。每当在结帐页面上完成更新时,我不需要为 woocommerce_checkout_update_order_review 放置一个 ajax 函数是一个 ajax 调用中的操作挂钩。

对于这个测试,我使用这个函数:

function get_shipping_cost_by_city( $city ) {
    if ( $city == 'Ormoc City' ){
        return 100;
    }
    return 130;
}

【讨论】:

  • 我相信这是正确的做法,因为这对我来说似乎是合乎逻辑的,我已经完成了相同的代码,但我的问题是当我改变城市时“woocommerce_checkout_update_order_review”不会触发
  • 谢谢@Reigel,我唯一不明白的是你代码中的注释// this is needed for us to remove the session set for the shipping cost. Without this, we can't set it on the checkout page你能解释一下你在这里的意思吗?
猜你喜欢
  • 2020-05-11
  • 2016-06-15
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
  • 2015-09-12
  • 2018-09-03
  • 2021-09-24
相关资源
最近更新 更多