I created a rule that fires on event After deleting a cart item. In there I check whether the cart is empty using function uc_cart_get_total_qty or(count(uc_cart_get_contents())) to figure out if the cart is empty to trigger some actions.

To my surprise by printing out count in drupal_set_message I saw that it returns 1 (uc_cart_get_contents also contains the object). Other than that everything else like displaying cart count in pager header block or show if cart is empty works fine.

Comments

Dor@ created an issue. See original summary.

tr’s picture

Issue tags: -cart, -empty, -delete, -cart_item, -event, -uc_cart_get_total_qty, -qty
tr’s picture

uc_cart_get_total_qty() is just a convenience function wrapper around uc_cart_get_contents(), so it's no surprise that they give you the same answer. Note that uc_cart_get_total_qty() was never really used, and has been removed for D8, so you should use uc_cart_get_contents() (In D8 this is \Drupal::service('uc_cart.manager')->get()->getContents()).

The cart is cached, so it needs to be refreshed if you add or delete items. Refreshing/reloading is an expensive operation so it's not done automatically, only when needed. This is done properly within Ubercart, but if you use the Ubercart API on your own you need to take special care to refresh the cart cache when needed. (In D8, this has been changed and you no longer need to explicitly refresh the cached cart.)

Anyway, if you use count(uc_cart_get_contents(NULL, 'rebuild')) you should get the correct value in this situation.

Dor@’s picture

Thank you it is fine now. Should I change the status to closed->works as designed?

Dor@’s picture

Title: uc_cart_get_total_qty returns 1 right after event After deleting a cart item » uc_cart_get_total_qty returns 1 right after event After deleting a cart item when cart is now empty
tr’s picture

Status: Active » Closed (works as designed)

Glad it works for you.