I'm creating a custom shipping calculator for my WooCommerce store. I need to collect some extra form data from the user to calculate the shipping properly.
I have the form fields added to woocommerce_checkout_after_customer_details
. I can probably get this into the cart page without much trouble once I figure out the checkout page functionality, so let's focus on this.
I have a class extending WC_Shipping_Method
with a calculate_shipping
method a la this tutorial.
In that method, I want to use the extra form data (plus the destination) to calculate the shipping for that customer. Right now I just have this adding a dummy rate, which shows up.
I also created a custom cart-shipping.php
file to not show any inputs for the available method in the order review cart, just the label and cost. Since the picking happens in my custom form, having options here is unnecessary.
- Am I doing this right, or is this approach super hacky?
- If this is the correct approach, how do I access the extra form fields inside the
calculate_shipping
method?
Have tried
Named the final options in my custom form shipping_method_*
which triggers the wc-ajax=update_order_review
call... which inits my custom shipping method class, but does not appear to ever call the calculate_shipping
method anyhow. The only time this method seems to be called is when I'm actually adding a product to the order.
Also tried
Defined all of my possible delivery options and added them in calculate_shipping like $this->add_rate( $rate );
. I think then I can somehow select one (force the user into one) when the cart updates via update_order_review
ajax call? Again, the set option should be determined by these fields the user interacts with on the checkout form. But I haven't figured out how to set the shipping on that action yet.
from Get fields from checkout form into calculate_shipping
No comments:
Post a Comment