douzai3399 2015-04-21 16:03
浏览 92

Woocommerce通过ajax更新结账时的送货方式

What i'm trying to accomplish: In woocommerce, I need to check a date delivered via a datepicker field when user selects a date, and then update shipping options accordingly via ajax, so that things like free shipping can be taken out when they are not appropriate.

What I currently know/have figured out: I currently have the jQuery event firing and sending through a date to a custom script, which is kinda where I need to do. I have not been able to find a function within woocommerce classes that returns just the shipping data, so I don't think I can call that and return it as a fragment, as they do for the checkout already.

However, I did find that

WC_AJAX::update_order_review()

has an action call within it, and I have successfully hooked a function onto that action, AND i've been able to fire off the

t( 'body' ).trigger( 'update_checkout' );

which fires off the action that updates the checkout review block.

My real question: All this is great, and I'ts looking like its heading in the right direction, but I don't know woocommerce well enough to know how to get the shipping methods within my hooked action to unset them as necessary. Does anyone know if I can get them through the $woocommerce global object, and then have them be read by the rest of that WC_AJAX method??

Any help here would be greatly appreciated.

** notes: yes, I know thats a 't' and not a '$' in the jQuery. Not a mistake

  • 写回答

3条回答 默认 最新

  • dongye9071 2015-05-04 16:23
    关注

    So, for those of you who may need this, it required a little finagling in woocommerce, because of the way they handle output of cart/checkout values for shipping and payment options, but in the end, I could make it happen using woocommerce's hooks, and very little plugin modification, so, a pretty clean solution.

    Basically what I did was use woocommerce sessions, two hooks, and a javascript trigger that refreshes the checkout order review.

    Through a combination of the woocommerce_checkout_update_order_review hook that happens in the WC_AJAX::update_order_review and use of the aformentioned $( 'body' ).trigger( 'update_checkout' ); call, I could hook in to the action that updates the checkout review block. So far so good.

    What I did in this function was check if the field had a value, and if it did, save it in the WC()->session. THEN, since WC_AJAX::update_order_review calls woocommerce_order_review() to grab all the updated cart and shipping methods, I looked into the template associated with that function and found another hook, woocommerce_review_order_before_shipping, which allows me to modify the shipping methods before the cart template loops through and builds the shipping options in wc_cart_totals_shipping_html().

    Now, I know, you're thinking, wait, I need checkout shipping methods, not cart ones, but in reality, they seem to be one and the same.

    Hopefully my wasted hours will help someone else with a similar problem.

    Cheers.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端