
WooCommerce is fast becoming the most popular e-commerce software on the web. We’ve started getting into it in a big way, offering training days and where you can learn more about it. Actually, WooCommerce is really powerful tool and easily expandable. It has many hooks that can be used for nearly everything, and that’s what make WooCommerce so good.
This list of Woocommerce code snippets will help you to tailor your on-line shop to how you want it. I have picked out from the support forum some useful snippets to help you customize WooCommerce. All these snippets must be pasted in the functions.php file within your theme folder.
1. Remove WooCommerce breadcrumbs
1 2 | /* Remove breadcrumbs*/ remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0); |
2. Remove WooCommerce Tabs
1 2 | /* Remove WooCommerce Tabs */ remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10); |
3. Change The Add to Cart Text on single product pages
1 2 3 4 5 6 7 | add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' ); // < 2.1 add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 + function woo_custom_cart_button_text() { return __( 'My Button Text', 'woocommerce' ); } |
4. Customize Checkout Field Order
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /* Customize Checkout Field Order */ add_filter( 'woocommerce_checkout_fields', 'reorder_woo_fields' ); function reorder_woo_fields( $fields ) { //move these around in the order you'd like $fields2['billing']['billing_first_name'] = $fields['billing']['billing_first_name']; $fields2['billing']['billing_last_name'] = $fields['billing']['billing_last_name']; $fields2['billing']['billing_company'] = $fields['billing']['billing_company']; $fields2['billing']['billing_address_1'] = $fields['billing']['billing_address_1']; $fields2['billing']['billing_address_2'] = $fields['billing']['billing_address_2']; $fields2['billing']['billing_city'] = $fields['billing']['billing_city']; $fields2['billing']['billing_postcode'] = $fields['billing']['billing_postcode']; $fields2['billing']['billing_state'] = $fields['billing']['billing_state']; $fields2['billing']['billing_country'] = $fields['billing']['billing_country']; $fields2['billing']['billing_email'] = $fields['billing']['billing_email']; $fields2['billing']['billing_phone'] = $fields['billing']['billing_phone']; //just copying these (keeps the standard order) $fields2['shipping'] = $fields['shipping']; $fields2['account'] = $fields['account']; $fields2['order'] = $fields['order']; return $fields2; } |
5. Rename A Product Tab
1 2 3 4 5 6 | /* Rename a product tab */ add_filter( 'woocommerce_product_tabs', 'woo_rename_tab', 98); function woo_rename_tab($tabs) { $tabs['description']['title'] = 'More info'; return $tabs; } |
6. Make account checkout fields required
1 2 3 4 5 6 7 8 9 | add_filter( 'woocommerce_checkout_fields', 'woo_filter_account_checkout_fields' ); function woo_filter_account_checkout_fields( $fields ) { $fields['account']['account_username']['required'] = true; $fields['account']['account_password']['required'] = true; $fields['account']['account_password-2']['required'] = true; return $fields; } |
7. Add Custom Field To Edit Address Page
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* Add Custom Field To Edit Address Page */ function woo_add_edit_address_fields( $fields ) { $new_fields = array( 'date_of_birth' => array( 'label' => __( 'Date of birth', 'woocommerce' ), 'required' => false, 'class' => array( 'form-row' ), ), ); $fields = array_merge( $fields, $new_fields ); return $fields; } add_filter( 'woocommerce_default_address_fields', 'woo_add_edit_address_fields' ); |
8. Set Mimimum Order Amount
1 2 3 4 5 6 7 8 9 | /* Set Minimum Order Amount */ add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' ); function wc_minimum_order_amount() { global $woocommerce; $minimum = 50; if ( $woocommerce->cart->get_cart_total(); < $minimum ) { $woocommerce->add_error( sprintf( 'You must have an order with a minimum of %s to place your order.' , $minimum ) ); } } |
9. Replace shop page title
1 2 3 4 5 6 7 8 | add_filter( 'woocommerce_page_title', 'woo_shop_page_title'); function woo_shop_page_title( $page_title ) { if( 'Shop' == $page_title) { return "My new title"; } } |
10. Redirect add to cart button to checkout page
1 2 3 4 5 6 7 | 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; } |
11. Remove specific (or all) of the product tabs
1 2 3 4 5 6 7 8 9 10 11 | 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; } |
12. Automatically add product to cart on visit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | add_action( 'init', 'add_product_to_cart' ); function add_product_to_cart() { if ( ! is_admin() ) { global $woocommerce; $product_id = 64; $found = false; //check if product already in cart if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) { foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) { $_product = $values['data']; if ( $_product->id == $product_id ) $found = true; } // if product not found, add it if ( ! $found ) $woocommerce->cart->add_to_cart( $product_id ); } else { // if no products in cart, add it $woocommerce->cart->add_to_cart( $product_id ); } } } } |
13. Add email recipient when order completed
1 2 3 4 5 | function woo_extra_email_recipient($recipient, $object) { $recipient = $recipient . ', your@email.com'; return $recipient; } add_filter( 'woocommerce_email_recipient_customer_completed_order', 'woo_extra_email_recipient', 10, 2); |
14. Customise the currency or symbol
1 2 3 4 5 6 7 8 9 10 11 12 | add_filter( 'woocommerce_currencies', 'add_my_currency' ); function add_my_currency( $currencies ) { $currencies['ABC'] = __( 'Currency name', 'woocommerce' ); return $currencies; } add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2); function add_my_currency_symbol( $currency_symbol, $currency ) { switch( $currency ) { case 'ABC': $currency_symbol = '$'; break; } return $currency_symbol; } |
15. Change the ‘out of stock’ with any text
1 2 3 4 5 6 | 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; } |
16. Change columns in shop page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function woo_product_columns_frontend() { global $woocommerce; // Default Value also used for categories and sub_categories $columns = 4; // Product List if ( is_product_category() ) : $columns = 4; endif; //Related Products if ( is_product() ) : $columns = 2; endif; //Cross Sells if ( is_checkout() ) : $columns = 4; endif; return $columns; } add_filter('loop_shop_columns', 'woo_product_columns_frontend'); |
17. Order by price, date or title on shop page
1 2 3 4 5 | add_filter('woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby'); function custom_default_catalog_orderby() { return 'date'; // Can also use title and price } |
17. Remove Related Products Output
1 2 3 4 | function wc_remove_related_products( $args ) { return array(); } add_filter('woocommerce_related_products_args','wc_remove_related_products', 10); |
18. Change number of related products output
1 2 3 4 5 6 7 8 9 10 11 12 | function woo_related_products_limit() { global $product; $args['posts_per_page'] = 6; return $args; } add_filter( 'woocommerce_output_related_products_args', 'jk_related_products_args' ); function jk_related_products_args( $args ) { $args['posts_per_page'] = 4; // 4 related products $args['columns'] = 2; // arranged in 2 columns return $args; } |
19. Change the placeholder image
1 2 3 4 5 6 7 8 9 10 11 12 13 | add_action( 'init', 'custom_fix_thumbnail' ); function custom_fix_thumbnail() { add_filter('woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src'); function custom_woocommerce_placeholder_img_src( $src ) { $upload_dir = wp_upload_dir(); $uploads = untrailingslashit( $upload_dir['baseurl'] ); $src = $uploads . '/2012/07/thumb1.jpg'; return $src; } } |
20. Change number of products displayed per page
1 2 | // Display 24 products per page. Goes in functions.php add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 24;' ), 20 ); |