“Enhance your WooCommerce store’s functionality with these 20 essential code snippets.”

Woocommerce is the most popular platform for selling products online. By using woo commerce plugin, you can sell any product, whether it’s physical or non-physical.WordPress provide many woocommerce-friendly plugins. Using those plugins, you can easily customise woo-commerce functionalities and enhance website features.

However, customizing your WooCommerce store can be difficult if you’re unfamiliar with coding. Fortunately, many WooCommerce snippets are available that can help you add functionality, modify your store’s behaviour, and improve your customers’ user experience.
Before customizing woo commerce store, I assume you already have an online store. But if you have not created one yet, this blog post might be helpful for you,

In this article, we have combined the 20 best helpful woocommerce code snippets for WordPress.By using these woocommerce snippets, you can enhance your woocommerce store’s functionality.

“20 BEST Woocommerce Code Snippets”

1: Code to Add a custom message on the checkout page:

add_action( 'woocommerce_review_order_before_payment', 'custom_checkout_message' );
 
function custom_checkout_message() {
    echo '<p>Custom message goes here.</p>';


2:Disable coupon fields on the checkout page

add_filter( 'woocommerce_coupons_enabled', '__return_false' );

3:Remove the “Related Products” section

remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );

4:Change the number of related products shown:

function change_number_related_products() {
    $args['posts_per_page'] = 4;
    return $args;
}
add_filter( 'woocommerce_output_related_products_args', 'change_number_related_products' );

5:Code to remove the “Add to Cart” button:

// remove Add to Cart button on product pages
function remove_add_to_cart_button() {
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action( 'woocommerce_single_product_summary', 'remove_add_to_cart_button', 1 );

Note: code is taken from https://www.websitebuilderinsider.com/how-do-i-hide-add-to-cart-in-woocommerce

6:Disable zoom on product images

add_action( 'wp_enqueue_scripts', 'remove_wc_gallery_zoom_script', 99 );
function remove_wc_gallery_zoom_script() {
    wp_dequeue_script( 'zoom' );
    wp_dequeue_script( 'photoswipe-ui-default' );
}

7:Remove product categories from the shop page

remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 )

8:Change the number of products shown per page

add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 20 );
function new_loop_shop_per_page( $cols ) {
    $cols = 12;
    return $cols;
}

9:Add a custom field to the checkout page

add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
    echo '<div id="my_custom_checkout_field"><h3>' . __('My Field') . '</h3>';
    woocommerce_form_field( 'my_field_name', array(
        'type' => 'text',
        'class' => array( 'my-field-class form-row-wide' ),
        'label' => __('My Field Label'),
        'placeholder' => __('My Field Placeholder'),
        ), $checkout->get_value( 'my_field_name' ) );
    echo '</div>';
}

10:Change the “Proceed to Checkout” button text

add_filter( 'woocommerce_order_button_text', 'woo_custom_order_button_text' );
function woo_custom_order_button_text() {
    return __( 'My Custom Button Text' );
}

11:Change Button text

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
 
function woo_custom_cart_button_text() {
    return __( 'My Custom Button Text' );
}

12:Remove product reviews

remove_action( 'woocommerce_product_tabs', 'woocommerce_product_reviews_tab', 30 );

13:Removing sorting Dropdown

remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );

14:Remove product tabs:

add_filter( 'woocommerce_product_tabs', 'remove_product_tabs', 98 );
function remove_product_tabs( $tabs ) {
    unset( $tabs['description'] );
    return $tabs;
}

15:Remove the “SKU” field

add_filter( 'woocommerce_product_sku_enabled', '__return_false'

16:Have on-sale products

function woo_have_onsale_products() {
	
	global $woocommerce;

	// Get products on sale
	$product_ids_on_sale = array_filter( woocommerce_get_product_ids_on_sale() );

	if( !empty( $product_ids_on_sale ) ) {
		return true;
	} else {
		return false;
	}
	
}

// Example:
if( woo_have_onsale_products() ) {
	echo 'have onsale products';
} else {
	echo 'no onsale product';
}

17: 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( 'shoud have an order with a minimum of %s to place order.' , $minimum ) );
	}

18:Order by date, price or title on the shop page

add_filter('woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby');
 
function custom_default_catalog_orderby() {
     return 'date'; //
}

  19:Redirect add to cart button to the checkout page

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;
}

20 : Add email recipient when the order complete

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)

These are the most common and important code snippets which we covered in this blog post. You can customize the woo-commerce checkout field and enhance product features using woo-commerce code snippets.
Please comment below if you still have trouble using these code snippets. I would love to help you. Thanks!:)

Categorized in: