Repository Forums Support WooCommerce Extended Coupon Features PRO Might WC_Cart::add_to_cart() cause issues …

Topic Resolution: Resolved
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #17394
    Heather Huffman
    Participant

    From your comment on line 706 of WJECF_Pro_Free_Products.php:
    //TODO: Might WC_Cart::add_to_cart() cause issues at triggering woocommerce_add_to_cart (which triggers calculate_totals)?

    When programmatically adding multiple products to the cart, your pro plugin pops the last one off. This happens even if there is no coupon configured to give away free products, but the comment on line 706 got me thinking that you know there is a problem. So…

        public function WJECF_bugfix()
        {
            try{
                ob_start();
                wc_maybe_define_constant( 'WOOCOMMERCE_CART', TRUE );
                WC()->cart->calculate_totals();
                woocommerce_cart_totals();
                $FU_WJECF = ob_get_clean();
            }
            catch( \Exception $e ){}
        }
    

    Thank you

    #17397
    Soft79
    Keymaster

    Actually that’s a very old comment and there are no known issues with that.

    What is the actual problem you’re trying to fix? When do you call WJECF_bugfix and what notice and/or exception are you suppressing?

    #17399
    Heather Huffman
    Participant

    1. Free version of your plugin was used for a long time (possibly years) without this issue.
    2. Toggling activate/deactivate on the pro version of your plugin causes the issue.

    We have a custom AJAX add to cart feature of the website so that products can combined in custom selections, and those combinations receive discounts. Example:

    https://wildalaskasalmonandseafood.com/product/wild-variety-pack/

    On the server side, we’re basically just looping through the data that is sent with the AJAX request, adding products to the cart in the same way that WooCommerce itself does it in \WC_AJAX::add_to_cart(). Even though we have like 50 plugins on the website, and also that your free plugin also worked fine, your pro plugin pops the last product off the submission. After multiple calls to WC()->cart->add_to_cart(), something that your pro plugin does takes the last product off, unless I recalculate the cart totals immediately after looping. Also, when deleting these combinations of multiple items from the cart, I also need to recalculate the cart totals, or your pro plugin leaves one of them in the cart. Again, activating/deactivating your pro plugin shows that the plugin causes the issue, and this was not the case with the free version.

    It doesn’t really matter that the comment was old, it was just a clue that led me to the solution to fix what was going on, and since I don’t want to spend hours wading through your code to find why it happens, I thought I’d just send you a message. Even without setting up a free product coupon, your pro plugin is doing something to the cart, and maybe that process needs some attention.

    Thank you.

    #17400
    Soft79
    Keymaster

    Well, the add-to-cart is not called by our plugin if there’s no free product to be added, so the comment is probably a red herring.

    You can disable certain modules from our plugin’s settings page if you (temporarily) enable debug mode of our plugin. Maybe this can help pinpoint which of the modules conflicts with your custom ajax handler. Can you send me the php code of your ajax handler to admin at soft79.nl? Maybe I can see what’s going on.

    #17401
    Soft79
    Keymaster

    It looks like something is preventing cart session from being stored (which should be triggered on calculate_totals). Anyhow this can be a combination of many factors, especially if you have that many plugins installed and custom code so I doubt it’s a bug in our plugin.

    #17404
    Heather Huffman
    Participant

    I guess I couldn’t expect you to calculate totals before doing whatever it is that you’re doing, and that would likely be the only way to fix the issue, but being that you’re plugin is modifying the cart, you should perhaps consider it.

    #17405
    Soft79
    Keymaster

    Closing this issue, it’s not a problem with our plugin.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.