Frequently Used WooCommerce Codes

Quick Summary
- In a WP_Query loop, read _price, _regular_price or _sale_price with get_post_meta and format it with wc_price using your store's currency.
- WC()->cart->cart_contents_count gives the cart's total quantity, and get_permalink on the woocommerce_myaccount_page_id option returns the My Account URL.
- Hook woocommerce_review_order_after_payment in functions.php to add content such as a secure-checkout badge below the classic checkout's payment section.
Five WooCommerce snippets that come up again and again when you build a custom theme. Snippets 1, 2, 3 and 5 go in template files; snippet 4 goes in your child theme’s functions.php.
Prices and cart count
1. Display the product price in a WP_Query loop
<?php $price = get_post_meta( get_the_ID(), '_price', true ); ?>
<?php echo wc_price( $price ); ?>
//REGULAR PRICE
<?php $price = get_post_meta( get_the_ID(), '_regular_price', true ); ?>
<?php echo wc_price( $price ); ?>
//SALE PRICE
<?php $price = get_post_meta( get_the_ID(), '_sale_price', true ); ?>
<?php echo wc_price( $price ); ?>Inside the loop, get_the_ID() is the current product. _price is the active price (the sale price while a sale runs), _regular_price the normal price and _sale_price the sale price, which is empty when the product isn’t on sale. wc_price() formats the number with your store’s currency settings. The //REGULAR PRICE and //SALE PRICE labels sit outside the PHP tags, so remove them or they print as text.
2. Display how many products are in the cart
$cartdata = WC()->cart->cart_contents_count;
echo $cartdata;This prints the total quantity in the cart, so two units of one product count as 2. WC()->cart->get_cart_contents_count() returns the same number. To keep the count current after an AJAX add to cart, refresh it with cart fragments.
Account link and checkout content
3. WooCommerce My Account/Dashboard link
<?php echo esc_url( trailingslashit( wc_get_account_endpoint_url( '' ) ) ); ?>
//another way
<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>Both lines print the URL of the My Account page, ready for a link’s href. The second reads the page ID stored in the woocommerce_myaccount_page_id option. Pass an endpoint such as orders to wc_get_account_endpoint_url() to link straight to that section. The //another way line is outside the PHP tags too.
4. Add content after the payment section at checkout
add_action( 'woocommerce_review_order_after_payment', 'add_content_on_checkout' );
function add_content_on_checkout() {
echo "<img src='safe_checkout.png' >"; //add the URL of the image
}The woocommerce_review_order_after_payment action runs below the payment section of the classic checkout page, a good spot for a secure-checkout badge. Replace safe_checkout.png with the full URL of your image and add alt text.
Product featured image
5. Get the featured image of a product in WooCommerce
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), 'single-post-thumbnail' );?>
<img src="<?php echo $image[0]; ?>" data-id="<?php echo $loop->post->ID; ?>">wp_get_attachment_image_src() returns an array for the product’s featured image, and $image[0] is its URL. The code expects $product_id and a $loop query to exist already, so adjust both to your loop. You can swap 'single-post-thumbnail' for any registered image size, such as woocommerce_thumbnail or full.
These snippets cover everyday tweaks. For a store built or reworked around your products, see my WooCommerce development services.
Related articles
- getting the shop, cart and checkout URLs in PHP: the other store links a theme needs
- frequently used WordPress theme snippets: the WordPress counterpart to this list
