30

I have been researching and tweaking away at my custom Wordpress theme and overridden WooCommerce templates with now WooCommerce installed to rectify a bunch of small formatting issues that occur on the WooCommerce pages. I'm down to now the cart and checkout, which yeah they use cart.php (and whatever checkout php), but also uses the main Wordpress theme page.php. I've been able to use is_woocommerce() in conditionals other places but not here as I've learned the WooCommerce docs say that can't be used on checkout and cart.

is_woocommerce() - Returns true if on a page which uses WooCommerce templates (cart and checkout are standard pages with shortcodes and thus are not included). http://docs.woothemes.com/document/conditional-tags/

So how can I alter the appearance of these pages? I have a "View All Posts" and a date/time at the top, and categories sidebar of page.php that I don't want showing up for obvious reasons on the checkout and cart since they don't make sense at all. What are my options?

Can I make it use a different template other than page.php?

Is this an option? Is it bad practice... does it cause more load on every page with this loop? Where do I put it? http://saiyedfaishal.wordpress.com/2014/01/06/check-if-it-is-woocommerce-page/

What's the best way to go about this? Thanks for any help! This question is somewhat related to - How to modify woocommerce_before_cart action

Community
  • 1
  • 1
Michael K
  • 1,031
  • 2
  • 14
  • 27

6 Answers6

43

Another way to totally override the cart.php is to copy:

woocommerce/templates/cart/cart.php to   
yourtheme/woocommerce/cart/cart.php

Then do whatever you need at the yourtheme/woocommerce/cart/cart.php

ken
  • 13,869
  • 6
  • 42
  • 36
  • Cool, good to know, thanks! I had actually already done that for archive-product.php and single-product.php. My main concern was with WooCommerce using the main page.php though I guess which was trickier. – Michael K Apr 08 '15 at 21:16
21

You can use function: wc_get_page_id( 'cart' ) to get the ID of the page. This function will use the page setup as 'cart' page and not the slug. Meaning it will keep working also when you setup a different url for your 'cart' on the settings page. This works for all kind of Woocommerce special page, like 'checkout', 'shop' etc.

example:

if (wc_get_page_id( 'cart' ) == get_the_ID()) {
  // Do something.
}
  • Thanks! This is a better solution. I put it in place. – Michael K Nov 19 '14 at 17:13
  • I'll just note that is_cart() and is_checkout() are specific to WooCommerce so they will break your site if you ever deactivate/remove WooCommerce, as you have to remember to remove those from your theme files as well. – Michael K Dec 01 '17 at 17:24
10

You can use the is_cart() conditional tag:

if (! is_cart() ) {
  // Do something.
}
PimPiet
  • 101
  • 1
  • 5
9

WooCommerce has a number of options for modifying the cart, and checkout pages. Here are the three I'd recomend:

Use WooCommerce Conditional Tags

is_cart() and is_checkout() functions return true on their page. Example:

if ( is_cart() || is_checkout() ) {
    echo "This is the cart, or checkout page!";
}

Modify the template file

The main, cart template file is located at wp-content/themes/{current-theme}/woocommerce/cart/cart.php

The main, checkout template file is located at wp-content/themes/{current-theme}/woocommerce/checkout/form-checkout.php

To edit these, first copy them to your child theme.

Use wp-content/themes/{current-theme}/page-{slug}.php

page-{slug}.php is the second template that will be used, coming after manually assigned ones through the WP dashboard.

This is safer than my other solutions, because if you remove WooCommerce, but forget to remove this file, the code inside (that may rely on WooCommerce functions) won't break, because it's never called (unless of cause you have a page with slug {slug}).

For example:

  • wp-content/themes/{current-theme}/page-cart.php
  • wp-content/themes/{current-theme}/page-checkout.php
dǝɥɔS ʇoıןןƎ
  • 1,674
  • 5
  • 19
  • 42
  • Thanks for the answer. I'll just note that is_cart() and is_checkout() are specific to WooCommerce so they will break your site if you ever deactivate/remove WooCommerce. I'm guessing page-cart.php only exists if the theme specifically has that to support WooCommerce (my theme does not have it for example). If you add that file to your theme, will it use that instead of page.php? – Michael K Dec 01 '17 at 17:21
  • Yes that’s all correct. Feel free to add that to the answer if you like – dǝɥɔS ʇoıןןƎ Dec 01 '17 at 22:19
  • page-{slug}.php is the second template that will be used, coming after manually assigned ones through the wp dashboard – dǝɥɔS ʇoıןןƎ Dec 01 '17 at 22:30
  • Good to know. Thanks. – Michael K Dec 01 '17 at 23:25
2

I've found this works well as a conditional within page.php that includes the WooCommerce cart and checkout screens.

!is_page(array('cart', 'checkout'))
Michael K
  • 1,031
  • 2
  • 14
  • 27
  • Checking back here... this solution I found has the advantage that it does not use WooCommerce-specific functions, so it won't break your site if you ever deactivate or remove WooCommerce. That's a nasty surprise when parts of your site don't work and you have to remember that you had added WooCommerce specific code to some theme files. – Michael K Dec 01 '17 at 17:23
1

I used the page-checkout.php template to change the header for my cart page. I renamed it to page-cart.php in my /wp-content/themes/childtheme/woocommerce/. This gives you more control over the wrapping html, header and footer.

fryvisuals
  • 53
  • 1
  • 1
  • 7