2

I've made a single-page checkout template for donations. The donation plugin is attached to the cart and will update the cart properly, however the cart will not display on my page if 0 items are in the basket when the user arrives. The intended end user will generally have no items in their cart.

How can I force the WooCommerce cart to show up if there are no products in the cart?

1
  • For any trying to answer; what is needed is to cause WC_Cart::is_empty() to return false on you custom template or page. Seems the action woocommerce_cart_loaded_from_session may be the best place to trick the logic. I solved my current problem by changing the is_empty() function directly - which is a wrong solution. --- function is_empty(){ global $wp; $current_url = home_url(add_query_arg(array(),$wp->request)); if($current_url==$CheckoutURL) return false; return 0 === sizeof( $this->get_cart() ); } As you may note, sizeof($this->get_cart()) is ultimately the deciding factor. – Garet Claborn Oct 19 '17 at 12:45
1

global $woocommerce; $count = $woocommerce->cart->cart_contents_count;

This should be the variable you'd want to work with. Try var_dump on this to display its value.

2
  • I tried add_filter('template_include', 'cart_setup'); and manually setting the count + putting a blank object in the cart_contents array if using my checkout template, but no luck changing behavior. – Garet Claborn Oct 18 '17 at 22:55
  • turns out WC doesn't check cart_contents_count for is_empty() function. It does sizeof( $this->get_cart() ). And get_cart() will check for wp_loaded, then check if the cart has been got from session and grab it if not, finally it just returns array_filter( (array) $this->cart_contents ) the step where it grabs cart from session sometimes. at the end of grabbing the cart the action woocommerce_cart_loaded_from_session is called. if cart is in the right state immediately after this call, it may be tricked. i didn't figure out that state so i cheated and changed is_empty() – Garet Claborn Oct 19 '17 at 12:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.