Hello @chatmandesign
I understand you’re trying to create a backend process to generate a data feed that includes the shipping costs of your products without creating any side effects. I appreciate your detailed explanation and I’m here to help you resolve this issue.
It’s true that using WC_Cart and WC_Order for calculating shipping costs can interfere with the front-end user experience and create unwanted database entries respectively. However, you can achieve your goal by creating a custom function that uses the same logic as the calculate_shipping() method, but without the side effects.
Here is a basic example of how you can do this:
- Create an instance of the WC_Shipping_Zones class. This will allow you to access all the shipping zones you have set up in WooCommerce.
- For each zone, you can retrieve the shipping methods associated with it using the get_shipping_methods() method.
- For each shipping method, you can then calculate the cost for a specific product.
Here’s a skeleton of what this could look like:
function calculate_shipping_cost($product) {
$zones = WC_Shipping_Zones::get_zones();
foreach ($zones as $zone) {
$methods = $zone['shipping_methods'];
foreach ($methods as $method) {
// Here you can calculate the shipping cost for the $product
// using the $method. The specifics will depend on the method.
}
}
}
This is a simplified example, and the exact implementation would depend on your specific shipping methods and how you want to handle products that can be shipped to multiple zones.
This function can be run in a backend process without interfering with the front-end user experience or creating unwanted database entries.
Please note that we can’t provide support for code customization as per our support policy. If you have any further questions on development or custom coding, don’t hesitate to reach out to some of our great resources available for support. Our WooCommerce community is brimming with skilled open-source developers who are active on the following channels:
I hope this helps! If you have any further questions, feel free to ask.
Thanks!