Website Consultancy and Free Advice

WooCommerce Single Product Page Snippets & Hooks 2017

UPDATE: These essential wooCommerce snippets are updated on a daily basis – 10 Snippets 16/05/2017

With many of our clients using wooCommerce we have built up quite a list of wooCommerce snippets, which we are willing to share and help you improve your WordPress eCommerce store.

We have split the snippets into different categories for you to find the snippets you require quickly. If you have any trouble implementing these snippets please contact us and we will beable to help you out.

Single Product Page

Quick Links:

  1. CHANGE “OUT OF STOCK” TO “SOLD OUT”
  2. REDIRECT USER TO CHECKOUT PAGE ONCE ADD TO CART CLICKED
  3. CHANGE ADD TO CART BUTTON TEXT
  4. RENAME A PRODUCT TAB
  5. REMOVE ONE OR MORE GENERIC PRODUCT PAGE TABS
  6. REORDER THE GENERIC WOOCOMMERCE TABS
  7. ADD A CUSTOM FIELD TO A PRODUCT VARIATION
  8. DISPLAY “PRODUCT IN CART” INSTEAD OF “ADD TO CART” BUTTON

1. Change “Out of Stock” to “Sold Out”

A few clients wanted us to change out of stock to sold out. This snippet does exactly that, qucik and easy!
Instructions: Add to your Theme functions.php file


add_filter('woocommerce_get_availability', 'availability_filter_func');

function availability_filter_func($availability)
{
	$availability['availability'] = str_ireplace('Out of stock', 'Sold Out', $availability['availability']);
	return $availability;
}

2. Redirect user to Checkout page once Add to Cart clicked

Instructions: Add to your Theme functions.php file


add_filter ('add_to_cart_redirect', 'redirect_to_checkout');

function redirect_to_checkout() {
    global $woocommerce;
    $checkout_url = $woocommerce->cart->get_checkout_url();
    return $checkout_url;
}

3. Change Add To Cart button text

Requested time after time for clients A/B testing to change the Add to Cart text, quick easy solution below.
Instructions: Add to your Theme functions.php file


function woocom_custom_cart_button_text() {
	return __('My New Text', 'woocommerce');
}
add_filter('single_add_to_cart_text', 'woocom_custom_cart_button_text');



wooCommerce product page tabs snippets

4. Rename a Product Tab

The snippet changes the tab “Description” name to “Delivery Info”. The generic tabs are – ‘description’, ‘reviews’, ‘additional_information’.
Instructions: Add to your Theme functions.php file


add_filter( 'woocommerce_product_tabs', 'woocomm_rename_tab', 98);
function woocomm_rename_tab($tabs) {

 $tabs['description']['title'] = 'Delivery info';

 return $tabs;
}

5. Remove one or more generic product page tabs

You may want to remove one of the product tabs or all of them, just use the unset line of code to do this.
Instructions: Add to your Theme functions.php file


function woo_remove_product_tab($tabs) {
    unset( $tabs['description'] ); // Remove the description tab
    unset( $tabs['reviews'] ); 	   // Remove the reviews tab
    unset( $tabs['additional_information'] );  	// Remove the additional information tab

   return $tabs; 
}
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tab', 98);

6. Reorder the generic wooCommerce tabs

Instructions: Add to your Theme functions.php file


add_filter( 'woocommerce_product_tabs', 'woo_reorder_tabs', 98 );
function woo_reorder_tabs( $tabs ) {

	$tabs['reviews']['priority'] = 5;			// Reviews first
	$tabs['description']['priority'] = 10;			// Description second
	$tabs['additional_information']['priority'] = 15;	// Additional information third

	return $tabs;
}

7. ADD A CUSTOM FIELD TO A PRODUCT VARIATION

Instructions: Add to your Theme functions.php file


//Display Fields
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
//JS to add fields for new variations
add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
//Save variation fields
add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 );

function variable_fields( $loop, $variation_data ) { 
echo "";
echo '';
echo '
'; echo ''; echo '
'; echo ''; echo ''; echo ''; echo ''; echo '
'; echo ''; echo '
'; echo ''; echo ''; function variable_fields_process( $post_id ) { if (isset( $_POST['variable_sku'] ) ) : $variable_sku = $_POST['variable_sku']; $variable_post_id = $_POST['variable_post_id']; $variable_custom_field = $_POST['my_custom_field']; for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) : $variation_id = (int) $variable_post_id[$i]; if ( isset( $variable_custom_field[$i] ) ) { update_post_meta( $variation_id, '_my_custom_field', stripslashes( $variable_custom_field[$i] ) ); } endfor; endif; }


8. DISPLAY "PRODUCT IN CART" INSTEAD OF "ADD TO CART" BUTTON

Instructions: Add to your Theme functions.php file


/**
 * Change the add to cart button text on single product pages
 */
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );

function woo_custom_cart_button_text() {

	global $woocommerce;
	
	foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
		$_product = $values['data'];
	
		if( get_the_ID() == $_product->id ) {
			return __('Already in cart!', 'woocommerce');
		}
	}
	
	return __('Add to cart', 'woocommerce');
}

/**
 * Change the add to cart text on product archives
 */
add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' );

function woo_archive_custom_cart_button_text() {

	global $woocommerce;
	
	foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
		$_product = $values['data'];
	
		if( get_the_ID() == $_product->id ) {
			return __('Already in cart', 'woocommerce');
		}
	}
	
	return __('Add to cart', 'woocommerce');
}

If you require further assistance please visit WooCommerce help pages, or contact us and we can help you.