Best WooCommerce Code Snippets: Add to Cart Link & More

Create a Direct “Add To Cart” Link

Sometimes you just need to let the user add a product in the cart by clicking on a link. It’s really easy to do, just add ?add-to-cart=974 at the end of any link.

In this example, 974 is the product id, and should be replaced by the id of the product you want to add to cart.

http://yoursite.com/checkout/?add-to-cart=974

Remove Address Fields in Checkout

If you’re selling digital goods, you don’t necessarily need to ask the buyer for his address. In order to remove the address fields from the checkout, insert the following code into your theme functions.php file:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
 
function custom_override_checkout_fields( $fields ) {
    unset($fields['billing']['billing_first_name']);
    unset($fields['billing']['billing_last_name']);
    unset($fields['billing']['billing_company']);
    unset($fields['billing']['billing_address_1']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_city']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_country']);
    unset($fields['billing']['billing_state']);
    unset($fields['billing']['billing_phone']);
    unset($fields['order']['order_comments']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_company']);
    unset($fields['billing']['billing_last_name']);
    unset($fields['billing']['billing_city']);
    return $fields;
}

Source: WP Mayor

Define the Number of Products Displayed Per Page

A simple but effective way to define how many products should be displayed per page. On this example, return 25 means that 25 products will be shown, update this value with the desired number of products and append this line you your theme’s functions.php file.

add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 25;' ), 20 );

Source: James Koster

Add a Message Above The Login/Register Form

Here’s a simple way to automatically add a custom message above the login/registration form. This code should be inserted into your functions.php file.

add_action( 'woocommerce_before_customer_login_form', 'jk_login_message' );
function jk_login_message() {
    if ( get_option( 'woocommerce_enable_myaccount_registration' ) == 'yes' ) {
	?>
		<div class="woocommerce-info">
			<p><?php _e( 'Returning customers login. New users register for next time so you can:' ); ?></p>
			<ul>
				<li><?php _e( 'View your order history' ); ?></li>
				<li><?php _e( 'Check on your orders' ); ?></li>
				<li><?php _e( 'Edit your addresses' ); ?></li>
				<li><?php _e( 'Change your password' ); ?></li>
			</ul>
		</div>
	<?php
	}
}

Source: James Koster

Remove WooCommerce Breadcrumbs

If you don’t think the breadcrumbs are useful, you can remove them easily by adding the code below into your theme’s functions.php file.

add_action( 'init', 'jk_remove_wc_breadcrumbs' );

function jk_remove_wc_breadcrumbs() {
    remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
}

Source: James Koster

Redirect the “Add To Cart” Button to the Checkout Page

A very interesting hack which allows you to automatically redirect users to the checkout page once a product has been added to the cart. As many other hacks from this article, this code should go to your functions.php file.

function add_to_cart_checkout_redirect() {
    wp_safe_redirect( get_permalink( get_option( 'woocommerce_checkout_page_id' ) ) );
    die();
}

add_action( 'woocommerce_add_to_cart', 'add_to_cart_checkout_redirect', 16 );

Source: Kloon

Replace “Out of Stock” by “Sold”

If you’re selling unique objects, it makes much more sense to display “sold” instead of the default “out of stock” label. To do so, you simply have to paste this code in your functions.php file.

add_filter('woocommerce_get_availability', 'availability_filter_func');

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

Source: WP Explorer

Restrict Shipping to Selected Countries

If you’re only willing to ship goods to specific countries, you can use the code below to do it. Countries can be added on the array on line 6. Code has to be in your functions.php file to work.

function woo_override_checkout_fields( $fields ) { 

	$fields['shipping']['shipping_country'] = array(
		'type'      => 'select',
		'label'     => __('My New Country List', 'woocommerce'),
		'options' 	=> array('AU' => 'Australia')
	);

	return $fields; 
} 
add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields' );

Source: WP Explorer

Display “Product Already in Cart” Instead of “Add to Cart” button

If a visitor has already added a specific product to his cart, it can be a great thing to notify them if an attempt to add the same product occurs.

Here’s a simple code snippet to do that. Just add it to your functions.php file.

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 - Add Again?', 'woocommerce');
		}
	}
	return __('Add to cart', 'woocommerce');
}
 
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');
}

Add Custom Page Navigation to WooCommerce

WooCommerce, like WordPress, doesn’t use page navigation by default. This can be changed easily on WordPress with the WP PageNavi plugin. When using WooCommerce, it requires a little hack to work.

The first thing to do is to install the WP PageNavi plugin. Once done, simply open your functions.php file and paste the following code in it.

remove_action('woocommerce_pagination', 'woocommerce_pagination', 10);
function woocommerce_pagination() {
		wp_pagenavi(); 		
	}
add_action( 'woocommerce_pagination', 'woocommerce_pagination', 10);

Source: WP Snacks