Frequently Used WooCommerce Codes

Share Now

1. Display The Product Price In A Wp_Query Loop In Woocommerce

<?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 ); ?>

2. Display How Many Products On Cart

$cartdata = WC()->cart->cart_contents_count;
echo $cartdata;

3. Woocommerce My Accoutn/Dashboard Link

<?php echo esc_url( trailingslashit( wc_get_account_endpoint_url( '' ) ) ); ?>
//another way
<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>

4. Woocommerce after checkout page

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 
}

5. Get 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; ?>">
Picture of Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert