Search
Close this search box.
Search
Close this search box.

WooCommerce functions

How to check if woocommerce is activated in theme

if ( class_exists( 'WooCommerce' ) ) {
    // code that requires WooCommerce
} else {
    // you don't appear to have WooCommerce activated
}

How to get woocommerce products using a query

$args = array(
    'post_type' => 'product', // get only products
    'posts_per_page' => -1, // get all products
    'product_cat' => 'book', // get only products that have the book category slug
);
query_posts($args);
while (have_posts()) : the_post();
    global $product;
    echo $product->get_sku();
    echo get_the_ID();
endwhile;

How to get product categories

See get_terms() for values accepted for $args. To change the order of the categories, just go to Admin / Products / Categories and reorder them using the drag-and-drop functionality.

$args = array(
    'taxonomy' => 'product_cat',
    'hide_empty' => 0
);
$product_categories = get_categories($args);
foreach ($product_categories as $cat) {
    echo $cat->name; // get category name
    echo get_term_link( $cat ); // get category url
    $thumbnail_id = get_woocommerce_term_meta($cat->term_id, 'thumbnail_id', true);
    $image = wp_get_attachment_url($thumbnail_id);
    if ($image) {
        echo '<img src="' . $image . '" alt="" />'; // get category thumbnail
    }
}

How to get a product category link by ID

get_term_link( $woo_cat_id, 'product_cat' );

How to get a product’s attribute by ID

$product = new WC_Product( get_the_ID() );
$price = $product->price;

How to get a product by it’s ID

$product = get_product($product_id);
$product_title = $product->post->post_title;

single-product.php

How to get the short description

<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>

How to get the product’s category ID by product ID

$term_list = wp_get_post_terms($product->id,'product_cat',array('fields'=>'ids'));
$cat_id = (int)$term_list[0];

How to get the product’s attributes

Returns the product’s attributes’ names.

$attributes = $product->get_attributes();

How to get the product’s available variations

// we need to check that the product is variable, or else it will throw an error
if ($product->product_type == 'variable') {
    $variations = $product->get_available_variations();
    foreach ($variations as $variation => $value) {
        echo $value['variation_id'];
    }
}

archive-product.php

The loop

while (have_posts()) : the_post();
    global $product;
    // use your functions here
endwhile;

How to get the product type

echo $product->product_type;

How to get the price in html format

echo $product->get_price_html();

How to check if a product is out of stock

if (!$product->is_in_stock()) {
    // out of stock
}

Cart functions

add_action( 'woocommerce_checkout_process', 'cart_functions' );
add_action( 'woocommerce_before_cart' , 'cart_functions' );

function cart_functions(){
    echo WC()->cart->get_cart_total(); // echoes cart subtotal (without shipping costs or vat) as a string with price format, eg. 20€
    echo WC()->cart->cart_contents_total; // echoes cart subtotal (without shipping costs or vat) as a float number
    echo WC()->cart->tax_total; // echoes cart taxes as a float number
    echo WC()->cart->total; // echoes cart total as a float number
}

Javascript functions for archive products

				
					jQuery( '.single_variation_wrap' ).each(function(){
        jQuery(this).on( 'show_variation', function( event, variation ) {
            console.log( variation );
            //jQuery(this).find
        });
    })
				
			

Javascript functions for single product

				
					jQuery( '.single_variation_wrap' ).on( 'show_variation', function( event, variation ) {
      console.log( variation );
})