24 great code snippets for Woocommerce

This article has not been updated for WooCommerce to the latest version. I will update again later.

In addition to the beautiful themes for Woocommerce and plugins to extend its functionality, we can take advantage of its built-in hooks to customize the sales page to our liking. If you are using Woocommerce, it would be helpful to have a look at the list of code snippets below to see if there is one that suits your needs, just copy and paste it into the functions.php file in your application. theme you are using.

1. Remove the word “Product” from the navigation bar


/*
* Hide "Products" in WooCommerce breadcrumb
*/
function woo_custom_filter_breadcrumbs_trail ( $trail ) {
foreach ( $trail as $k => $v ) {
if ( strtolower( strip_tags( $v ) ) == ‘products’ ) {
unset( $trail[$k] );
break;
}
}

return $trail;
}

add_filter( ‘woo_breadcrumbs_trail’, ‘woo_custom_filter_breadcrumbs_trail’, 10 );

2. Manually add products to the cart every time a visitor enters

Replace the number 64 in the code with the ID of the product that you need to automatically add to the cart.

24 great code snippets for Woocommerce 15

/*
* Add item to cart on visit
*/
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 );
}
}
}
add_action( ‘init’, ‘add_product_to_cart’ );

3. Add your own currency symbols


add_filter( ‘woocommerce_currencies’, ‘add_my_currency’ );

function add_my_currency( $currencies ) {
$currencies[‘VNĐ’] = __( ‘Vietnam Dong’, ‘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 ‘VNĐ’: $currency_symbol = ‘$’; break;
}
return $currency_symbol;
}

4. Replace “Add to Cart” or “Add to Cart”


/**
* Change the add to cart text on single product pages
*/
function woo_custom_cart_button_text() {
return __(‘My Button Text’, ‘woocommerce’);
}
add_filter(‘single_add_to_cart_text’, ‘woo_custom_cart_button_text’);

/**
* Change the add to cart text on product archives
*/
function woo_archive_custom_cart_button_text() {
return __( ‘My Button Text’, ‘woocommerce’ );
}
add_filter( ‘add_to_cart_text’, ‘woo_archive_custom_cart_button_text’ );

24 great code snippets for Woocommerce 15

5. Automatically redirect to checkout page after adding to cart

Now this feature does not need to use code anymore, you can turn it on in Woocommerce -> Settings -> Products -> Display and choose Redirect to the cart page after successful addition.

See more:  Introducing the WordPress Theme Programming series 2015

6. Send email after paying with coupon


/**
* WooCommerce Extra Feature
* ————————–
*
* Send an email each time an order with coupon(s) is completed
* The email contains coupon(s) used during checkout process
*
*/
function woo_email_order_coupons( $order_id ) {
$order = new WC_Order( $order_id );

if( $order->get_used_coupons() ) {

$to = ‘[email protected]’;
$subject = ‘New Order Completed’;
$headers = ‘From: My Name ‘ . "rn";

$message = ‘A new order has been completed.n’;
$message .= ‘Order ID: ‘.$order_id.’n’;
$message .= ‘Coupons used:n’;

24 great code snippets for Woocommerce 15

foreach( $order->get_used_coupons() as $coupon) {
$message .= $coupon.’n’;
}
@wp_mail( $to, $subject, $message, $headers );
}
}
add_action( ‘woocommerce_thankyou’, ‘woo_email_order_coupons’ );

7. Change number of related products


/**
* WooCommerce Extra Feature
* ————————–
*
* Change number of related products on product page
* Set your own value for ‘posts_per_page’
*
*/
function woo_related_products_limit() {
global $product;

$args = array(
‘post_type’ => ‘product’,
‘no_found_rows’ => 1,
‘posts_per_page’ => 6,
‘ignore_sticky_posts’ => 1,
‘orderby’ => $orderby,
‘post__in’ => $related,
‘post__not_in’ => array($product->id)
);
return $args;
}
add_filter( ‘woocommerce_related_products_args’, ‘woo_related_products_limit’ );

8. Do not display products in a certain category on the Shop page

Change the word shoes to the slug of the product category you need to remove, you can add more by filling in like array( 'shoes', 'computer' ).


/**
* Remove products from shop page by category
*
*/
function woo_custom_pre_get_posts_query( $q ) {

if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;

24 great code snippets for Woocommerce 15

if ( ! is_admin() && is_shop() ) {

$q->set( ‘tax_query’, array(array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
‘terms’ => array( ‘shoes’ ), // Don’t display products in the shoes category on the shop page
‘operator’ => ‘NOT IN’
)));

}

remove_action( ‘pre_get_posts’, ‘custom_pre_get_posts_query’ );

24 great code snippets for Woocommerce 15

}
add_action( 'pre_get_posts', 'woo_custom_pre_get_posts_query' );

9. Turn off tabs like Review, Description in products


/**
* Remove product tabs
*
*/
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);

24 great code snippets for Woocommerce 15

10. Replace the word “Free” with any word


/**
* WooCommerce Extra Feature
* ————————–
*
* Replace "Free!" by a custom string
*
*/
function woo_my_custom_free_message() {
return "Liên hệ để lấy giá";
}

add_filter(‘woocommerce_free_price_html’, ‘woo_my_custom_free_message’);

11. Hide other shipping methods when the free method is active


// Hide ALL shipping options when free shipping is available
add_filter( ‘woocommerce_available_shipping_methods’, ‘hide_all_shipping_when_free_is_available’ , 10, 1 );

/**
* Hide ALL Shipping option when free shipping is available
*
* @param array $available_methods
*/
function hide_all_shipping_when_free_is_available( $available_methods ) {

if( isset( $available_methods[‘free_shipping’] ) ) :

// Get Free Shipping array into a new array
$freeshipping = array();
$freeshipping = $available_methods[‘free_shipping’];

24 great code snippets for Woocommerce 15

// Empty the $available_methods array
unset( $available_methods );

// Add Free Shipping back into $avaialble_methods
$available_methods = array();
$available_methods[] = $freeshipping;

endif;

return $available_methods;
}

24 great code snippets for Woocommerce 15

12. Fix notification after adding to cart


/**
* Custom Add To Cart Messages
* Add this to your theme functions.php file
**/
add_filter( ‘woocommerce_add_to_cart_message’, ‘custom_add_to_cart_message’ );
function custom_add_to_cart_message() {
global $woocommerce;

// Output success messages
if (get_option(‘woocommerce_cart_redirect_after_add’)==’yes’) :

$return_to = get_permalink(woocommerce_get_page_id(‘shop’));

$message = sprintf(‘<a href="https://TechtipsNReview.com/wordpress/wp-plugin/%s" class="button">%s</a> %s’, $return_to, __(‘Continue Shopping &rarr;’, ‘woocommerce’), __(‘Product successfully added to your cart.’, ‘woocommerce’) );

else :

24 great code snippets for Woocommerce 15

$message = sprintf(‘<a href="https://TechtipsNReview.com/wordpress/wp-plugin/%s" class="button">%s</a> %s’, get_permalink(woocommerce_get_page_id(‘cart’)), __(‘View Cart &rarr;’, ‘woocommerce’), __(‘Product successfully added to your cart.’, ‘woocommerce’) );

endif;

return $message;
}

13. Add your own province/city (States)


/**
* Code goes in functions.php or a custom plugin. Replace XX with the country code your changing.
*/
add_filter( ‘woocommerce_states’, ‘custom_woocommerce_states’ );

function custom_woocommerce_states( $states ) {

24 great code snippets for Woocommerce 15

$states[‘XX’] = array(
‘XX1’ => ‘State 1’,
‘XX2’ => ‘State 2’
);

return $states;
}

14. Change the number of thumbnail images in the product


add_filter ( ‘woocommerce_product_thumbnails_columns’, ‘xx_thumb_cols’ );
function xx_thumb_cols() {
return 4; // .last class applied to every 4th thumbnail
}

15. Display the image representing the product category on the category page


add_action( ‘woocommerce_archive_description’, ‘woocommerce_category_image’, 2 );
function woocommerce_category_image() {
if ( is_product_category() ){
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, ‘thumbnail_id’, true );
$image = wp_get_attachment_url( $thumbnail_id );
if ( $image ) {
echo ‘<img src="https://TechtipsNReview.com/wordpress/wp-plugin/’ . $image . ‘" alt="" />’;
}
}
}

16. Fix “Add to Cart” button link


add_filter(‘add_to_cart_redirect’, ‘custom_add_to_cart_redirect’);

function custom_add_to_cart_redirect() {
/**
* Replace with the url of your choosing
* e.g. return ‘http://www.yourshop.com/’
*/
return get_permalink( get_option(‘woocommerce_checkout_page_id’) );
}

17. Extend the address input field


add_filter(‘woocommerce_billing_fields’, ‘custom_woocommerce_billing_fields’);

function custom_woocommerce_billing_fields( $fields ) {

24 great code snippets for Woocommerce 15

$fields[‘billing_address_1’][‘class’] = array( ‘form-row-wide’ );

$fields[‘billing_address_2’][‘class’] = array( ‘form-row-wide’ );

return $fields;
}

18. Product display loop example

<ul class="products">
<?php $args = array( ‘post_type’ => ‘product’,
‘posts_per_page’ => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( ‘content’, ‘product’ );
endwhile;
} else {
echo __( ‘No products found’ );
}
wp_reset_postdata();
?>
</ul>

24 great code snippets for Woocommerce 15

<!–/.products–>

19. Fix the number of products displayed on each page


// Display 24 products per page. Goes in functions.php
add_filter( ‘loop_shop_per_page’, create_function( ‘$cols’, ‘return 24;’ ), 20 );

20. Hide related products


/*
* wc_remove_related_products
*
* Clear the query arguments for related products so none show.
* Add this code to your theme functions.php file.
*/
function wc_remove_related_products( $args ) {
return array();
}
add_filter(‘woocommerce_related_products_args’,’wc_remove_related_products’, 10);

21. Remove custom product sort menu


remove_action( ‘woocommerce_before_shop_loop’, ‘woocommerce_catalog_ordering’, 30 );

22. Rounded product photos

This is added to the style.css file in the theme:

.woocommerce .woocommerce-tabs {border: 1px solid #e6e6e6}

23. Put the product description below the product photo


add_action(‘woocommerce_after_shop_loop_item_title’,’woocommerce_template_single_excerpt’, 5);

24. Turn off the SKU . code function

add_filter( ‘wc_product_sku_enabled’, ‘__return_false’ );

There are many other great functions, but for complex functions, it is better to use a plugin. I will have a summary of useful plugins for Woocommerce later.

See more:  How to Use Emoji smileys in WordPress 4.2


Source: 24 great code snippets for Woocommerce
– TechtipsnReview

, , ,

Leave a Reply

Your email address will not be published. Required fields are marked *