Welcome

Hello! I am a PHP programmer with 15 years experience. I specialise in  WordPress (& WooCommerce), CakePHP, CubeCart along with all things PHP. This is my blog – for programmers; a valuable resource of programming issues and fixes. For employers; an insight into my programming skills and clients.

Autogenerating a WooCommerce SKU Stock Code

Here is a little bit of framework code that will automatically generate an SKU code in WooCommerce.
It takes the selected product category and post ID to generate a unique stock code for the product being added.

// auto generates a stock code
add_action( 'save_post', 'save_woocommerce_auto_sku' );
function save_woocommerce_auto_sku( $post_id ) {
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		if ($_REQUEST['tax_input']) {
			$term = get_term_by('id',$_REQUEST['tax_input']['product_cat'][1],'product_cat');
			if ($term) {
				$acronym = "";
				$name = explode(' ', (string)$term->name);
 
				foreach ($name as $w) {
					$acronym .= $w[0];
				}
				$SKU = strtoupper($acronym) . str_pad($post_id, 4);
			}
			update_post_meta( $post_id, '_sku', $SKU);
		}
	}
}

 

Adding WooCommerce Cart Items link to WordPress menus

Here’s a quick function to create a nav menu item that contains cart items and a cart link. Useful to blend in WooCommerce features with an existing menu and design.

function add_last_nav_item($items) {
global $woocommerce;
 
return $items .= '<li class="menu-item-cart-item">CART: <a class="cart-contents" href="'.$woocommerce->cart->get_cart_url().'" title="View your shopping cart">'.sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count).' - '. $woocommerce->cart->get_cart_total().'</a></li>';
}
add_filter('wp_nav_menu_items','add_last_nav_item');

Just edit the $items variable to suit.

About Us   David Gavin Home   Oldfields Log Store

Modifying WooCommerce Product Sort Options

You can easily modify the WooCommerce product sort options to remove any unnecessary sort options for your WooCommerce store.
Simply add this to your theme’s functions.php and customise to suit your needs.

function custom_woocommerce_catalog_orderby( $sortby ) {
unset($sortby['rating']);
unset($sortby['price']);
unset($sortby['desc']);
 
return $sortby;
}
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );

 

You can change the following options;

  • menu_order - Default sorting
  • popularity –  Sort by popularity
  • rating - Sort by average rating
  • date - Sort by newness
  • price - Sort by price: low to high
  • price-desc - Sort by price: high to low

 

Auto match 404 strings and redirect

A recent client had a product list built from posts and pages. I transformed it into a WooCommerce store. Google had indexed their inventory as posts and pages. We needed to rewrite these automatically to the new style listing of WooCommerce’s products custom post type.

I wrote a small 404 handler that would search WooCommerce products for the requested page slug (these always matched).

The following code would be in your theme’s 404.php page.

$parsed_url = explode('-',basename($_SERVER['REDIRECT_URL']));
 
if (isset($parsed_url[0])) {
 
	$parsed_url[0] = sanitize_title($parsed_url[0]);
 
	$search_posts = get_posts(array(
		'post_type' => 'products',
		'suppress_filters' => TRUE,
		'posts_per_page' => '1',
		's' => $parsed_url[0]
	));
 
	if ($search_posts) {
		wp_safe_redirect(get_permalink($search_posts[0]->ID), 301);
		exit();
	}
}

Show saving on WooCommerce sales items

WooCommerce will automatically add sales CSS to items on sale and show you the regular price, however it doesn’t show you the total saved.
Here’s how to show the saving.

Home   Advanced Muscle Design

Add the following to your theme’s functions.php.

add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
 
	$percentage = round( $product->regular_price - $product->sale_price  ,2 );
 
	return $price . sprintf( __(' <br /><span class="price_save">SAVE &pound;%s', 'woocommerce' ), $percentage . '</span>' );
 
}
 
add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_custom_variable_sales_price', 10, 2 );
function woocommerce_custom_variable_sales_price( $price, $variable ) {
 
	$reg_price = get_post_meta( $variable->children[0], '_regular_price', true );
	$sale_price = get_post_meta( $variable->children[0], '_sale_price', true );
 
	$percentage = round( $reg_price - $sale_price  ,2 );
 
	return $price . sprintf( __(' <br /><span class="price_save">SAVE &pound;%s', 'woocommerce' ), $percentage . '</span>' );
 
}

Loading WordPress Contact Form 7 Javascript and Styling Only when Necessary

The Contact Form 7 plugin seems to be the best available plugin of it’s kind. Robust, flexible and infinitely customisable. It appears on most of my WordPress installs along with Flamingo and sometimes Really Simple CAPTCHA.

Unfortunately, one annoyance is that In it’s default settings, Contact Form 7 loads its JavaScript and CSS stylesheet on every page. Here’s how I fixed this so it only loads when necessary. Continue Reading…

Conditional Woocommerce Product Tabs

The WooCommerce product tabs are pretty powerful once you understand the hooks and actions. See Modifying or Removing Product Tabs in Woocommerce.
That post discusses how to modify or hide product tabs in WooCommerce. This post describes how you can conditionally show them. For example, only show a tab if isn’t empty.

Here we are adding an Ingredients tab. Ingredients are stored in Post Meta as ingredients.

Continue Reading…

[Snippet] Modifying or Removing Product Tabs in Woocommerce

Here’s how to modify or remove product tabs Woocommerce.
Add the following code to your functions.php file.

add_filter('woocommerce_product_tabs', 'woo_remove_product_tabs', 98);
function woo_remove_product_tabs( $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; 
}

This will remove all the tabs, modify to suit your requirements.