Listed here are 12 helpful Woocommerce snippets and hacks I exploit on most of my websites. So, I want to share them with you. If you wish to use them then both paste them inside you theme’s operate.php file or higher but, use Code Snippets plugin. In the event you to that then you’ll not lose the modifications throughout your subsequent theme swap.
Kadence theme
SAVE 10% COUPON: SIMPLEHACKS
Video: How you can add Woocommerce snippets & hacks
On this video I’ll present you learn how to add all these snippets and the way will they have an effect on your web site.
How you can cover Uncategorized product class from Woocommerce store web page and widget?
This code right here beneath hides Uncategorized class from store web page
// Disguise Uncategorized product class from store web page
add_filter( 'woocommerce_product_subcategories_args', 'hide_uncategorized_cat_from_shop_page' );
operate hide_uncategorized_cat_from_shop_page( $args ) {
$args['exclude'] = get_option( 'default_product_cat' );
return $args;
}
This code right here beneath hides Uncategorized class from the sidebar widget
// Disguise Uncategorized product class from widget
add_filter( 'woocommerce_product_categories_widget_args', 'hide_uncategorized_cat_from_widget' );
operate hide_uncategorized_cat_from_widget( $args ) {
$args['exclude'] = get_option( 'default_product_cat' );
return $args;
}
How you can cover class product rely in product archives?
add_filter( 'woocommerce_subcategory_count_html', '__return_false' );
How you can take away Woocommerce opinions tab?
See one other video I made on learn how to create, rename, take away and reorder customized product tabs.
add_filter( 'woocommerce_product_tabs', 'remove_review_tab', 98 );
operate remove_review_tab( $tabs ) {
unset( $tabs['reviews'] ); // Take away the opinions tab
return $tabs;
}
How you can activate Woocommerce catalogue mode with out additional plugin?
add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS
Bonus hack: How you can add an enquiry type on Woocommerce single product web page?
Additionally, if you want to activate a list mode and present a product enquiry type on single product web page, then use this code as an alternative.
add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS
// Add an enquiry type on Woocommerce single product web page
add_action( 'woocommerce_single_product_summary', 'single_product_message', 20 );
operate single_product_message() {
echo '<p class="woocommerce-message">Ship us an enquiry </p>';
echo do_shortcode(''); // Your contact type shortcode goes right here.
}
If you’re then see this video on learn how to add enquiry type on product web page.
How you can present out of inventory merchandise final on Woocommerce product archive pages?
add_action( 'woocommerce_product_query', 'out_of_stock_last', 999 );
operate out_of_stock_last( $question ) {
if ( is_admin() ) return;
$question->set( 'meta_key', '_stock_status' );
$question->set( 'orderby', array( 'meta_value' => 'ASC' ) );
}
How you can present featured merchandise and/or contact type on Merchandise not discovered web page?
// You need to use all Woocommerce shortcodes right here beneath. See out there shortcodes right here https://docs.woocommerce.com/doc/woocommerce-shortcodes/
add_action( 'woocommerce_no_products_found', 'featured_products_on_not_found', 20 );
operate featured_products_on_not_found() {
echo '<h4>' . __( 'No merchandise have been discovered matching your choice. Though... Chances are you'll be concerned with these merchandise', 'area' ) . '</h4>';
echo do_shortcode( '' ); // Right here goes your shortcode
echo '<h4>' . __( 'Can’t discover your product? Ship us an enquiry', 'area' ) . '</h4>';
echo do_shortcode( '' ); // Right here goes your shortcode
}
How you can take away Woocommerce “Ship to a distinct tackle?” choice?
add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false');
How you can take away Woocommerce checkout fields with out a plugin?
Right here beneath are all of the fields you’ll be able to take away.
// Take away checkout fields
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
operate custom_override_checkout_fields( $fields ) {
// take away billing fields
unset($fields['billing']['billing_first_name']); // Billing First identify
unset($fields['billing']['billing_last_name']); // Billing Final identify
unset($fields['billing']['billing_company']); // Billing firm
unset($fields['billing']['billing_address_1']); // Billing Tackle 1
unset($fields['billing']['billing_address_2']); // Billing Tackle 2
unset($fields['billing']['billing_city']); // Billing metropolis
unset($fields['billing']['billing_postcode']); // Billing postcode
unset($fields['billing']['billing_country']); // Billing nation
unset($fields['billing']['billing_state']); // Billing state
unset($fields['billing']['billing_phone']); // Billing telephone
unset($fields['billing']['billing_email']); // Billing e-mail
// take away delivery fields
unset($fields['shipping']['shipping_first_name']); // Delivery first identify
unset($fields['shipping']['shipping_last_name']); // Delivery final identify
unset($fields['shipping']['shipping_company']); // Delivery firm
unset($fields['shipping']['shipping_address_1']); // Delivery tackle 1
unset($fields['shipping']['shipping_address_2']); // Delivery tackle 2
unset($fields['shipping']['shipping_city']); // Delivery metropolis
unset($fields['shipping']['shipping_postcode']); // Delivery postcode
unset($fields['shipping']['shipping_country']); // Delivery nation
unset($fields['shipping']['shipping_state']); // Delivery state
// take away order remark fields
unset($fields['order']['order_comments']); // Order feedback
return $fields;
}
You don’t have to stick all of them, as an alternative use solely these you want. For instance, if it’s essential take away billing tackle 2, state and firm fields then use this code right here beneath.
// Take away checkout fields
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
operate custom_override_checkout_fields( $fields ) {
// take away billing fields
unset($fields['billing']['billing_company']); // Billing firm
unset($fields['billing']['billing_address_2']); // Billing Tackle 2
unset($fields['billing']['billing_state']); // Billing state
return $fields;
}
How you can make Woocommerce checkout fields non-compulsory?
Listen that “true” =required and if you’d like the sphere to be non-compulsory then set it to “false”.
add_filter( 'woocommerce_default_address_fields' , 'optional_default_address_fields' );
operate optional_default_address_fields( $address_fields ) {
$address_fields['first_name']['required'] = true;
$address_fields['last_name']['required'] = true;
$address_fields['company']['required'] = false;
$address_fields['address_1']['required'] = true;
$address_fields['address_2']['required'] = false;
$address_fields['country']['required'] = false;
$address_fields['postcode']['required'] = false;
$address_fields['city']['required'] = false;
$address_fields['state']['required'] = false;
return $address_fields;
}
// For billing e-mail and telephone fields
add_filter('woocommerce_billing_fields', 'optional_checkout_fields1', 1000, 1);
operate optional_checkout_fields1( $fields ) {
$fields['billing_email']['required'] = true;
$fields['billing_phone']['required'] = false;
return $fields;
}
And as soon as extra, for those who want a solely couple of fields to be non-compulsory then simply paste the traces you want. For instance, this snippet makes nation and telephone fields non-compulsory.
add_filter( 'woocommerce_default_address_fields' , 'optional_default_address_fields' );
operate optional_default_address_fields( $address_fields ) {
$address_fields['country']['required'] = false;
return $address_fields;
}
// For billing e-mail and telephone fields
add_filter('woocommerce_billing_fields', 'optional_checkout_fields1', 1000, 1);
operate optional_checkout_fields1( $fields ) {
$fields['billing_phone']['required'] = false;
return $fields;
}
Filter by featured merchandise in admin
world $typenow, $wp_query;
if ($typenow=='product') :
// Featured/ Not Featured
$output .= "<choose identify='featured_status' id='dropdown_featured_status'>";
$output .= '<choice worth="">'.__( 'Select standing', 'woocommerce' ).'</choice>';
$output .="<choice worth='featured' ";
if ( isset( $_GET['featured_status'] ) ) $output .= chosen('featured', $_GET['featured_status'], false);
$output .=">".__( 'Featured', 'woocommerce' )."</choice>";
$output .="<choice worth='regular' ";
if ( isset( $_GET['featured_status'] ) ) $output .= chosen('regular', $_GET['featured_status'], false);
$output .=">".__( 'Non-featured', 'woocommerce' )."</choice>";
$output .="
operate featured_products_filter() {
world $typenow, $wp_query;
if ($typenow=='product') :
// Featured/ Not Featured
$output .= "<choose identify='featured_status' id='dropdown_featured_status'>";
$output .= '<choice worth="">'.__( 'Select standing', 'woocommerce' ).'</choice>';
$output .="<choice worth='featured' ";
if ( isset( $_GET['featured_status'] ) ) $output .= chosen('featured', $_GET['featured_status'], false);
$output .=">".__( 'Featured', 'woocommerce' )."</choice>";
$output .="<choice worth='regular' ";
if ( isset( $_GET['featured_status'] ) ) $output .= chosen('regular', $_GET['featured_status'], false);
$output .=">".__( 'Non-featured', 'woocommerce' )."</choice>";
$output .="</choose>";
echo $output;
endif;
}
add_action('restrict_manage_posts', 'featured_products_filter');
operate featured_products_filter_query( $question ) {
world $typenow;
if ( $typenow == 'product' ) {
// Subtypes
if ( ! empty( $_GET['featured_status'] ) ) {
if ( $_GET['featured_status'] == 'featured' ) {
$question->query_vars['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'area' => 'slug',
'phrases' => 'featured',
);
} elseif ( $_GET['featured_status'] == 'regular' ) {
$question->query_vars['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'area' => 'slug',
'phrases' => 'featured',
'operator' => 'NOT IN',
);
}
}
}
}
add_filter( 'parse_query', 'featured_products_filter_query' );
Filter by sale merchandise in admin
world $wp_query;
$chosen = 0;
if (isset($_GET['product_sale'])) {
$chosen = (int)$_GET['product_sale'];
}
$output .= '
<choose id="dropdown_product_sale" identify="product_sale">
<choice worth="">Sale worth filter</choice>
<choice chosen="chosen" worth="1">On sale</choice>
<choice chosen="chosen" worth="2">Common worth</choice>
</choose>
';
return $output;
}
add_action('woocommerce_product_filters', 'backend_onsale_filter');
/*
* Woocommerce Filter by on sale the place assertion
*/
operate backend_onsale_where_statement($the place) {
world $wp_query, $wpdb;
if (!is_admin() || $_GET['post_type'] != "product" || !isset($_GET['product_sale']) || $_GET['product_sale']
operate backend_onsale_filter($output) {
world $wp_query;
$chosen = 0;
if (isset($_GET['product_sale'])) {
$chosen = (int)$_GET['product_sale'];
}
$output .= '
<choose id="dropdown_product_sale" identify="product_sale">
<choice worth="">Sale worth filter</choice>
<choice chosen="chosen" worth="1">On sale</choice>
<choice chosen="chosen" worth="2">Common worth</choice>
</choose>
';
return $output;
}
add_action('woocommerce_product_filters', 'backend_onsale_filter');
/*
* Woocommerce Filter by on sale the place assertion
*/
operate backend_onsale_where_statement($the place) {
world $wp_query, $wpdb;
if (!is_admin() || $_GET['post_type'] != "product" || !isset($_GET['product_sale']) || $_GET['product_sale'] <= 0) {
return $the place;
}
$productsIDs = [];
if ($_GET['product_sale'] == 1) {
$querystr = '
SELECT p.ID
FROM ' . $wpdb->posts . ' p
WHERE p.ID IN (
SELECT post_id FROM ' . $wpdb->postmeta . ' pm WHERE pm.meta_key = "_sale_price" AND pm.meta_value > ''
)
';
$pageposts = $wpdb->get_results($querystr, OBJECT);
$productsIDs = array_map(operate($n){
return $n->ID;
}, $pageposts);
} elseif ($_GET['product_sale'] == 2) {
$querystr = '
SELECT p.ID
FROM ' . $wpdb->posts . ' p
WHERE p.ID NOT IN (
SELECT post_id FROM ' . $wpdb->postmeta . ' pm WHERE pm.meta_key = "_sale_price" AND pm.meta_value > ''
)
';
$pageposts = $wpdb->get_results($querystr, OBJECT);
$productsIDs = array_map(operate($n){
return $n->ID;
}, $pageposts);
}
$the place .= ' AND ' . $wpdb->posts . '.ID IN (' . implode(",", $productsIDs) . ') ';
return $the place;
}
add_filter('posts_where' , 'backend_onsale_where_statement');
Disable Woocommerce Analytics & different bloat
add_filter( 'woocommerce_admin_disabled', '__return_true' );