• Resolved cassiocesio

    (@cassiocesio)


    The shipping price was not being updated when the user modifies the delivery address. After a lot of work I discovered that when I use the filter “woocommerce_cart_shipping_packages” the shipping price does not update, but when I remove the filter the plugin works normally. Do you have any idea for a solution?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author SilkyPress

    (@diana_burduja)

    This plugin has nothing to do at all with the shipping price. It only rearranges the checkout form, but it doesn’t care what form fields are there and how they behave.

    Are you sure this shipping price behavior is related to the WooCommerce Multi-Step Checkout plugin? If you deactivate the plugin everything works alright?

    Thread Starter cassiocesio

    (@cassiocesio)

    Yes, I turned everything off. I know that the plugin does not modify the fields and that it only displays the data, but when you use the same filter by calling an empty function the checkout hangs and no longer updates the data.

    You can test anywhere in your code:
    add_filter( ‘woocommerce_cart_shipping_packages’,’filter_woocommerce_cart_shipping_packages’);
    public function filter_woocommerce_cart_shipping_packages(){

    }

    Somehow updating the data is compromised.

    Plugin Author SilkyPress

    (@diana_burduja)

    Again, this behavior has nothing to do with the multi-step checkout plugin. You can deactivate the plugin and you’ll see the same behavior.

    The error comes from the fact that you’re not returning anything in the filter function. A filter ALWAYS takes in a variable, modifies it and then returns it modified (that is the definition of a filter). When the function doesn’t get any parameter and it returns nothing, the variable that needs to be filtered is actually transformed to an empty array. Therefore the functionality gets broken.

    Try to use the filter like this:

    add_filter( 'woocommerce_cart_shipping_packages','filter_woocommerce_cart_shipping_packages');
    function filter_woocommerce_cart_shipping_packages($shipping_packages) {
        // Modify here the $shipping_packages variable in some way and then return it.
        // Don't remove the return statement. Very important
    
        return $shipping_packages;
    }

    And I’m so mad right now that I had to spend half an hour setting up different shipping options and testing out your issue just to find out that the issue is that you have no clue how to use a filter. (Ok, I’m not really mad, but still …)

    Thread Starter cassiocesio

    (@cassiocesio)

    Hi Diana, sorry but I know how to use a filter, that’s not the problem (this function is complete on my site and it works). To be able to perform the test and check the problem you can put an alert inside the function.

    You will notice from the alert that the filter only runs when the page refreshes. Therefore, if the user updates the zip code in the first step, the filter will no longer be called. You can do the test this way:

    add_filter( 'woocommerce_cart_shipping_packages', array($this, 'filter_woocommerce_cart_shipping_packages'), 10);
    function filter_woocommerce_cart_shipping_packages() {
        // Modify here the $shipping_packages variable in some way and then return it.
        // Don't remove the return statement. Very important
          $message = "test";
          echo "<script type='text/javascript'>alert('$message');</script>";
          return "";
    }

    In order to work, the filter would need to be called again when the user modified the cep or when he clicked next. I believe that would solve the problem.

    Plugin Author SilkyPress

    (@diana_burduja)

    Of course you can write an alert inside the function, but the function needs to have a parameter and to return it.

    Try this:

    add_filter( 'woocommerce_cart_shipping_packages', array($this, 'filter_woocommerce_cart_shipping_packages'), 10);
    function filter_woocommerce_cart_shipping_packages($shipping_packages) {
        // Modify here the $shipping_packages variable in some way and then return it.
        // Don't remove the return statement. Very important
          $message = "test";
          echo "<script type='text/javascript'>alert('$message');</script>";
          return $shipping_packages;
    }

    If you have any more questions related to this issue, please use the support forum for the WooCommerce plugin. This whole issue has nothing to do with the multi-step checkout plugin.

    Thread Starter cassiocesio

    (@cassiocesio)

    Diana, I think I failed in communication and you didn’t understand the problem that is happening.

    I put the alert just for you see that the filter is only called when the checkout page is updated, when we change the postcode and press the next button to the next step the filter never called again. So the shipping price is not updated.

    The alert will show that it doesn’t enter the filter when you change the step. My question is not about using filters, forget the return, just make the test to realize that it doesn’t run the filter when changing step.

    Plugin Author SilkyPress

    (@diana_burduja)

    I know for sure that you’ve never tried out the exact code I gave you simply because if you would’ve tried it, then you would’ve seen that the shipping value changes when the country is changed.

    And don’t modifying it !!!! Don’t remove the $shipping_package variable. Don’t change one comma !!! Simply copy/paste the code and test the behavior of the shipping value.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Filter: woocommerce_cart_shipping_packages’ is closed to new replies.