Repository › Forums › Support › WooCommerce Extended Coupon Features PRO › Might WC_Cart::add_to_cart() cause issues …
- This topic has 6 replies, 2 voices, and was last updated 2 years, 2 months ago by Soft79.
-
AuthorPosts
-
March 13, 2021 at 7:37 am #17394Heather HuffmanParticipant
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
March 13, 2021 at 9:23 am #17397Soft79KeymasterActually 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?
March 13, 2021 at 5:14 pm #17399Heather HuffmanParticipant1. 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.
March 13, 2021 at 9:46 pm #17400Soft79KeymasterWell, 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.
March 14, 2021 at 9:29 am #17401Soft79KeymasterIt 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.
March 15, 2021 at 3:51 pm #17404Heather HuffmanParticipantI 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.
March 15, 2021 at 4:12 pm #17405Soft79KeymasterClosing this issue, it’s not a problem with our plugin.
-
AuthorPosts
- You must be logged in to reply to this topic.