通过ajax更新woocommerce送货方式 [英] Updating woocommerce Shipping Method via ajax

查看:127
本文介绍了通过ajax更新woocommerce送货方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Wordpress和Woocommerce建立一个食品订购网站。到目前为止,我一直表现不错,但卡在一定程度上应该自动选择送货方式。

I am building a food ordering website using Wordpress and Woocommerce. So far I have been doing good but got stuck at one point where the Shipping method should be chosen automatically.

流程是什么样的?用户访问我的网站并输入交付地址的邮政编码,然后我的网站显示以该邮政编码提供的餐馆。有些餐馆用更短的时间和更少的运费来提供邮政编码,但有些餐馆花费更多的时间并收取更多的运费。

What is the flow like? The user visits my websites and enters the zip code of delivery address and then my website shows the restaurants which deliver in that zip code. Some restaurants deliver in that zip code in less time and less shipping charges but some take more time and take more shipping charges.

到目前为止,我所做的是我取的任何餐厅提供的数据库中的运输代码。如果列表中不存在用户的邮政编码,我们提醒我们继续前进。

What I have done so far is that I fetch the shipping codes from the database any restaurant delivers to. If the user's zip code doesn't exist in the list we alert else we move forward.

下一步是获取所有在该邮政编码中提供的餐馆交货时间,然后在更长的交货时间内提供该邮政编码的餐馆。基于较少或更多的交货时间,我分别在A区或B区标记用户。

Next step is to fetch all the restaurants that deliver in that zip code in less delivery time and then restaurants that deliver in that zip code in more delivery time. Based on less or more delivery time I mark the user in Zone A or Zone B respectively.

我为A区和B区创建了统一运费方式,可以是从购物车/结账页面的下拉列表中选择。现在问题就在于它。

I have flat rate shipping methods created for Zone A and Zone B which can be selected from the drop down in the Cart/Checkout Page. Now here it what the trouble is.

用户不应该选择运费率。应根据用户标记的区域自动选择和应用它。

The user shouldn't select what the shipping rate would be. It should be automatically selected and applied based on what Zone the user has been marked into.

这种自动更新不是我能够实现的。

This automatic updating is not what I am able to achieve.

我尝试过什么?

正确的流程应该是将此区域置于Woocommerce会话中,应该自动选择并在用户到达购物车或结帐页面时应用。

The right process should be to put this Zone into Woocommerce session and it should be picked automatically and applied when the user reaches the cart or checkout page.

我无法找到正确的方法来调用可能触发ajax调用来更新运费。任何想法都会有所帮助。

I am not able to find the right method to call which could trigger the ajax call to update shipping. Any ideas would be helpful.

问候

Stackoverflow上的类似线程

推荐答案

在JS中使用 $(body).trigger('update_checkout'); 。这将触发对 WC_AJAX-> update_order_review 的请求,然后该请求将获取结帐区域中的所有表单数据。如果您需要传递任何自定义数据,请向结帐添加隐藏输入,并允许您通过 $ _ POST 数据传递值。

Use $(body).trigger('update_checkout'); in your JS. This will fire off a request to WC_AJAX->update_order_review, which will then pick up all the form data in the checkout area. If you need to pass any custom data, add a hidden input to the checkout and it should allow you to pass a value along through the $_POST data.

从那里,如果你需要弄乱可用的方法和会话设置,你可以做我做的事情并覆盖 shipping_html 函数在cart-shipping.php中,就像这样。

From there, if you need to mess with available methods and the session settings, you can do what I did and overwrite the shipping_html function in cart-shipping.php, like so.

function woo_new_cart_shipping_html()
{
  global $woocommerce;

  $data_stream = extract_data_from_str( $_POST['post_data'] );

  $packages      = $woocommerce->shipping->get_packages();
  $chosen_method = isset( $woocommerce->session->chosen_shipping_methods[0] ) ? $woocommerce->session->chosen_shipping_methods[0] : '';

  foreach ( $packages as $i => $package )
  {
     $methods = apply_filters( 'woo_filter_available_methods_by_date',     $package['rates'], $data_stream );

     wc_get_template( 'cart/cart-shipping.php', array('package' => $package,
                                                 'available_methods' =>      apply_filters( 'woo_sort_shipping_methods_by_cost', $methods ),
                                                 'show_package_details' => ( sizeof( $packages ) > 1 ),
                                                 'index' => $i,
                                                 'chosen_method' =>  apply_filters( 'woo_set_lowest_shipping_method', $chosen_method, $methods, $data_stream ) ) );

  }
}

这是更新证明,它将允许您通过添加过滤器来自定义该模板。

This is update proof, and it will let you customize that template by adding filters.

至于重置会话以与所选方法相对应,遗憾的是,我没有给您答案我自己仍在努力解决这个问题。

As for resetting the session to correspond with the selected method, I don't have an answer for you, unfortunately, as I'm still wrestling with that exact problem myself.

这篇关于通过ajax更新woocommerce送货方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆