Forum Replies Created
-
AuthorPosts
-
bill.antoniadisParticipant
We’d like to come back to this issue. I’ve summed our progress up in the below question fully with some code examples. I’ve worded this in a way that we can post on other support forums but it’s WCMp-related in nature:
Question:
How to change Woocommerce vendor address during Cart/Checkout for use in Woocommerce Tax calculations?
Description:
TL;DR – Is there a way to alter the address WooCommerce Tax uses for the tax nexus using a filter/hook upon loading the cart and checkout pages? We tried (see below) and it didn’t work.
We are using a WordPress plugin for WooCommerce called WC-Marketplace (WCMp) to create a multi-vendor platform that allows vendors in our market to sell goods. Everything works as expected except for taxes. We only want to charge sales tax if the vendor and the customer reside in the same state. I understand this sounds like a product specific question but we have reason to believe it needs to be handled with code that’s specific to WooCommerce.
Example of what we want:
Vendor is in Ohio, Customer is in Ohio = Sales tax applied
Vendor is in California, Customer is in Ohio = No tax applied
We’re using WC Tax to automatically calculate sales tax within the USA. However, it seems to be using the address found in WooCommerce > Settings > General on the admin dashboard instead of the actual vendor’s address configured in their WCMp dashboard which is stored as an entry in the wp_usermeta table.https://woocommerce.com/products/tax/
Example:
Vendor is in California, Customer is in Ohio = Behaves as if Vendor is in Ohio as our general settings are configured to Ohio in this scenario.
We reached out to WCMp support and the way we they put it to us, we’ll need to write code to intercept the tax calculation ourselves. We believe it’s possible to do this using an filter/hook and override it with the correct address before taxes are calculated.After reading through WooCommerce documentation and code itself, here’s code I wrote to accomplish this:
// // Change tax location to use vendor location function custom_woocommerce_order_get_tax_location( $args, $instance ) { global $woocommerce; // Get vendor based on product ID $vendor_id = ''; foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ){ $post_obj = get_post( $cart_item['product_id'] ); $post_author = $post_obj->post_author; $vendor_id = $post_author; break; } if ($vendor_id) { // Set tax location based on vendor $args = []; $args['country'] = metadata_exists( 'user', $vendor_id, '_vendor_country' ) ? get_user_meta($vendor_id, '_vendor_country', true):''; $args['state'] = metadata_exists( 'user', $vendor_id, '_vendor_state' ) ? get_user_meta($vendor_id, '_vendor_state', true):''; $args['postcode'] = metadata_exists( 'user', $vendor_id, '_vendor_postcode' ) ? get_user_meta($vendor_id, '_vendor_postcode', true):''; $args['city'] = metadata_exists( 'user', $vendor_id, '_vendor_city' ) ? get_user_meta($vendor_id, '_vendor_city', true):''; } return $args; } add_filter('woocommerce_order_get_tax_location', 'custom_woocommerce_order_get_tax_location', 10, 2);
CopyIn short, the above recurses through items in the cart and gets the WCMp vendor’s ID based on which WP user created it (always the vendor). It then uses the vendor_id to get the address metadata from the wp_usermeta table and override the woocommerce_order_get_tax_location filter’s $args.
This is the code for that filter, get_tax_location()
I believe this is working to some extent as when I print out the WC Object, we see in the WC_Customer Object that the correct vendor’s State and Zip Code are listed as so:
[changes:protected] => Array ( [shipping] => Array ( [postcode] => 90003 [city] => Los Angeles [address_1] => [address_2] => [state] => CA ) )
CopyUnfortunately, the tax is still being calculated as if our business is the tax nexus and not the vendor.
What is the correct way to intercept and change the vendor address WC Tax uses before the cart and Checkout pages load the taxes?
Thanks a million!
bill.antoniadisParticipantThank you for the reply! Regarding issue #1 we see you’re not refunding the full amount of 121 euros. We are attempting to refund the full amount which is where we see this error. Can you show us a video of you refunding the full amount?
Thanks!
bill.antoniadisParticipantA follow up question on refunds. When the vendor approves the customer refund request and enters the amount to refund, does that refund then get sent to Stripe? We tested this and we don’t see anything in Stripe that indicates a refund was initiated.
bill.antoniadisParticipantSounds good. We’re meeting to review this today and tomorrow.
March 11, 2022 at 12:58 AM in reply to: We want to show the amount the Stripe gateway charges on the order page #135260bill.antoniadisParticipantHere is the information we want to display on the Order page from the /dashboard/transaction-details/{ID} page
We want Total Earned on the Order page to reflect the subtracted Gateway Fee and display the Gateway Fee as a line item.
March 10, 2022 at 11:57 PM in reply to: We want to show the amount the Stripe gateway charges on the order page #135256bill.antoniadisParticipantPlease find this is where we want to make the addition/change.
bill.antoniadisParticipantAlso, I’m attempting to use the above link to test per product tax and it’s not saving when I update the product.
bill.antoniadisParticipantHi, developer here.
Currently I’m attempting to modify your example for per-product tax rates to do this:
https://multivendorx.com/tech-blog/vendor-per-product-tax/
Particularly by using the
woocommerce_cart_totals_get_item_tax_rates
Copyfilter. My thought process is to use the product id to get the vendor id associated with the product, compare the vendor and customer locations, and set the tax rate based on that. The WCMp REST API does not seem to provide any feature to get the vendor id by product id so I’m looking both into doing this manually through SQL or using one of WCMp’s vendor functions but am still working through this.
I’m also noticing that the tax rates still seem to need to be manually set per state/etc so am currently investigating the connection between woocommerce tax and jetpack.
Does one of your developers know of a way to get the vendor’s address based on the products in the cart and then set the address being used by woocommerce tax plugin to that address when this filter runs?
bill.antoniadisParticipantThank you for your reply. Assuming this question has come up before, do your developers have any sample code or suggestions of where to make the change?
February 24, 2022 at 4:21 AM in reply to: WCMp Stripe Marketplace conflict when saving shipping address in My Account #134797bill.antoniadisParticipantWe were able to resolve this issue by deactivating the WooCommerce Stripe plugin.
January 28, 2022 at 9:27 PM in reply to: WCMp Stripe Marketplace conflict when saving shipping address in My Account #133723bill.antoniadisParticipantPlease find that I’ve attached a video of me attempting to save shipping data from my account both with and without the stripe marketplace plugin enabled.
I had to use Google Drive as the filesize is too large to upload here
https://drive.google.com/file/d/1_0ltSmkVdzgWFa4St0jtI3UcjYmwmQ4y/view?usp=sharing
January 28, 2022 at 9:26 PM in reply to: WCMp Stripe Marketplace conflict when saving shipping address in My Account #133722bill.antoniadisParticipantPlease find that I’ve attached a video of me attempting to save shipping data from my account both with and without the stripe marketplace plugin enabled.
January 28, 2022 at 1:10 AM in reply to: WCMp Stripe Marketplace conflict when saving shipping address in My Account #133686bill.antoniadisParticipantSee attached
January 8, 2022 at 10:25 AM in reply to: Trouble with both Dashboard and Store when using WCMp AFM #132848bill.antoniadisParticipantThis reply has been marked as private.January 8, 2022 at 10:25 AM in reply to: Trouble with both Dashboard and Store when using WCMp AFM #132845bill.antoniadisParticipantThis reply has been marked as private. -
AuthorPosts