• Resolved chatmandesign

    (@chatmandesign)


    I need to create a backend process that will generate a data feed that includes the shipping costs of our products. To do this, I need to be able to calculate the shipping price on a product without creating any side effects.

    The only calculate_shipping() methods I’ve found belong to WC_Cart and WC_Order.

    When I attempted to use WC_Cart for this (via WC()->cart), it ended up interfering with customers using the cart on the front end, which is obviously totally unacceptable.

    From what I can tell, it looks like WC_Order->calculate_shipping() automatically saves an order to the database, which I expect would result in our database quickly getting filled up with garbage orders by this backend process.

    There has to be a better way to do this. How can I simply run the shipping calculation without generating side effects?

    Thank you.

Viewing 1 replies (of 1 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    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:

    1. 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.
    2. For each zone, you can retrieve the shipping methods associated with it using the get_shipping_methods() method.
    3. 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!

Viewing 1 replies (of 1 total)
  • The topic ‘Calculate shipping without side effects’ is closed to new replies.