Categories
WooCommerce

WooCommerce – Hide trailing zeros on prices

<?php
add_filter( 'woocommerce_price_trim_zeros', '__return_true' );
Categories
WooCommerce WordPress

WooCommerce – How to add custom tracking code for the thank you page

<?php
/**
 * Add custom tracking code to the thank-you page
 */
add_action( 'woocommerce_thankyou', 'cs_custom_tracking' );

function cs_custom_tracking( $order_id ) {

	// Lets grab the order
	$order = wc_get_order( $order_id );

	/**
	 * Put your tracking code here
	 * You can get the order total etc e.g. $order->get_total();
	 */
	 
	// This is the order total
	$order->get_total();
 
	// This is how to grab line items from the order 
	$line_items = $order->get_items();

	// This loops over line items
	foreach ( $line_items as $item ) {
  		// This will be a product
  		$product = $order->get_product_from_item( $item );
  
  		// This is the products SKU
		$sku = $product->get_sku();
		
		// This is the qty purchased
		$qty = $item['qty'];
		
		// Line item total cost including taxes and rounded
		$total = $order->get_line_total( $item, true, true );
		
		// Line item subtotal (before discounts)
		$subtotal = $order->get_line_subtotal( $item, true, true );
	}
}
Categories
WooCommerce

WooCommerce – Exclude products from a particular category on the shop page

/**
 * Exclude products from a particular category on the shop page
 */
function cs_pre_get_posts_query( $q ) {

    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'clothing' ), // Don't display products in the clothing category on the shop page.
           'operator' => 'NOT IN'
    );


    $q->set( 'tax_query', $tax_query );

}
add_action( 'woocommerce_product_query', 'cs_pre_get_posts_query' ); 
Categories
WooCommerce WordPress

Change “Add to Cart” text on single product page

<?php

function chetan_custom_cart_button_text() {

    return __( 'My Button Text', 'chetan' );

}

add_filter( 'add_to_cart_text', 'chetan_custom_cart_button_text' ); // WooCommerce < 2.1
add_filter( 'woocommerce_product_single_add_to_cart_text', 'chetan_custom_cart_button_text' ); // WooCommerce 2.1 +
Categories
WooCommerce WordPress

WooCommerce – Update number of items in cart and total after Ajax

add_filter('add_to_cart_fragments', 'chet_tips_woocommerce_header_add_to_cart_fragment');

function chet_tips_woocommerce_header_add_to_cart_fragment( $fragments ) {
  global $woocommerce;

  ob_start();

  ?>
  <span class="cart-count"><?php echo $woocommerce->cart->cart_contents_count; ?></span>
  <?php

  $fragments['span.cart-count'] = ob_get_clean();

  return $fragments;

}