Let’s hack your Woocommerce shop a bit. That is, in this post I’ll show you 24 simple Woocommerce hacks that will save your time and money.
Now, in order to make this work, add your chosen snippets shown to your child theme’s functions.php file or better yet, use a snippet manager like Code Snippets.
There is also a WpCodeBox plugin, which is my favorite code snippets manager for WordPress. This is a premium plugin and if you’re interested, then grab WPCodeBox with a nice 20% discount here (SAVE 20% Coupon WPSH20).
Video: 24 Simple Woocommerce Hacks For Every Store
In this video I will show you how to add all those snippets and how will they affect your site.
How to activate Woocommerce catalog mode and add enquiry form on single product pages?
This snipper here below activates catalog mode and displays a product enquiry form on single product page
add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTSremove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONSremove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS// Add an enquiry form on Woocommerce single product pageadd_action( 'woocommerce_single_product_summary', 'single_product_message', 20 );functionsingle_product_message() {echo'<p class="woocommerce-message">Send us an enquiry </p>';echodo_shortcode(''); // Your contact form shortcode goes here. }
How to Add conditional Woocommerce product category page messages
With the help of this code snippet here you can add a message to all Woocommerce product category pages.
<span role="button" tabindex="0" data-code="// Custom message on Woocommerce shop page and all category pages
add_action( 'woocommerce_before_main_content', 'shop_message', 20 );
function shop_message() {
echo '<p class="woocommerce-message">Free shipping for all orders
// Custom message on Woocommerce shop page and all category pagesadd_action( 'woocommerce_before_main_content', 'shop_message', 20 );functionshop_message() {echo'<p class="woocommerce-message">Free shipping for all orders</p>'; // Change your message here}
How to add message to specific Woocommerce category page?
If you need to add a message to the scpecific Woocommerce product category page (Storage catogory, for example) then use this code instead.
<span role="button" tabindex="0" data-code="// Message for specific category
add_action( 'woocommerce_before_main_content', 'category_message', 20 );
function category_message() {
if ( is_product_category( 'storage' ) ) { // Change your vategory slug here
echo '<p class="woocommerce-message">Estimated shipping time: 2-3 weeks
// Message for specific categoryadd_action( 'woocommerce_before_main_content', 'category_message', 20 );functioncategory_message() {if ( is_product_category( 'storage' ) ) { // Change your vategory slug hereecho'<p class="woocommerce-message">Estimated shipping time: 2-3 weeks</p>'; // Change your message here}}
How to add one message to specific Woocommerce category page and another message to all other category pages?
Now lets add a one message to the Sotrage category page and another message to all other categories.
<span role="button" tabindex="0" data-code="// One message for specific category (storage) and another for all other categories
add_action( 'woocommerce_before_shop_loop', 'conditional_message', 1 );
function conditional_message() {
if ( is_product_category( 'storage' ) ) { // Change your vategory slug here
echo '<p class="woocommerce-message">Estimated delivery time: 2-3 weeks</p>';// Change your Storage category message here
} else {
echo '<p class="woocommerce-message">Estimated delivery time: 1 week
// One message for specific category (storage) and another for all other categoriesadd_action( 'woocommerce_before_shop_loop', 'conditional_message', 1 );functionconditional_message() {if ( is_product_category( 'storage' ) ) { // Change your vategory slug hereecho'<p class="woocommerce-message">Estimated delivery time: 2-3 weeks</p>';// Change your Storage category message here } else {echo'<p class="woocommerce-message">Estimated delivery time: 1 week</p>'; // Change your shop-wide message here}}
How to remove category product count in product archives?
As you probably have seen the category loop has a product count shown next to the category name. This code will hide Woocommerce product count in product archives.
How to remove category product count in product archives?
As you probably have seen the category loop has a product count shown next to the category name. This code will hide Woocommerce product count in product archives.
How to display custom product badges with checking a checkbox?
Next, let’s add a checkbox at your single product edit page. If you check this then the text “Free shipping” is shown on the single product page below the title. See these screenshots here below, and you’ll see how it will also display it for the badge we’re going to create in the next chapter.
If you would like to change the text shown in the badge, then just modify it in line 34. Also, change the text in lines 7 and 9 accordingly.
Now, let’s display custom product badges with checking a checkbox.
How to display Woocommerce stock amount and stock status on Woocommerce archive pages?
Sometimes there is a need to display Woocommerce stock amount and stock status on Woocommerce arhcive pages. Therefore, this snippet will help you out.
It will display Woocommerce stock amoutn and stock statuses like this:
25 in stock
Out of stock
In stock
Available on backorder
For variable products it may be “In stock (some items)”
//* Add stock status to archive pages
add_action( 'woocommerce_after_shop_loop_item', 'wpsh_add_stock_status_archive', 3 );
function wpsh_add_stock_status_archive() {
global $product;
$availability = $append = null;
// Add status for single products
if( $product->is_type( 'simple' ) ) {
// Abandon if stock management is disabled on any variation
if( ! array_filter( $availability ) )
return;
$status[] = $availability[ 'class' ];
}
/**
* Compile final output and class based on
* availability classes set by WooCommerce
*/
if( in_array( 'in-stock', $status ) ) {
$output = __( 'In stock', 'wp-clips' );
$class = 'in-stock';
}
elseif( in_array( 'available-on-backorder', $status ) ) {
$output = __( 'Available on backorder', 'wp-clips' );
$class = 'available-on-backorder';
}
elseif( in_array( 'out-of-stock', $status ) ) {
$output = __( 'Out of stock', 'wp-clips' );
$class = 'out-of-stock';
}
// Append output if some items out of stock or available on backorder
if( ( in_array( 'available-on-backorder', $status ) && $class == 'in-stock' ) ||
( in_array( 'out-of-stock', $status ) && $class != 'out-of-stock' ) )
$append = ' ' . __( '(some items)', 'wp-clips' );
}
// Output only if set
if( isset( $availability ) ){
echo '<span class="archive-stock ' . esc_attr( $class ) . '">' . esc_html( $output ) . esc_html( $append ) . '
//* Enqueue scriptsadd_action( 'wp_enqueue_scripts', 'wpsh_stock_status_archive', 11 );functionwpsh_stock_status_archive() {// Activate clip styleswp_enqueue_style( 'wpsh-stock-status-archive-style', plugins_url( 'clip-style.css', __FILE__ ), array(),'1.0.0' );}//* Add stock status to archive pagesadd_action( 'woocommerce_after_shop_loop_item', 'wpsh_add_stock_status_archive', 3 );functionwpsh_add_stock_status_archive() {global $product; $availability = $append =null;// Add status for single productsif( $product->is_type( 'simple' ) ) { $availability = $product->get_availability(); $class = $availability[ 'class' ]; $output = $availability[ 'availability' ]; }// Add status for variable productselseif( $product->is_type( 'variable' ) ) { $status =array();// Get status class for each variationforeach ( $product->get_children() as $child_id ) { $variation = $product->get_child( $child_id ); $availability = $variation->get_availability();// Abandon if stock management is disabled on any variationif( !array_filter( $availability ) )return; $status[] = $availability[ 'class' ]; }/** * Compile final output and class based on * availability classes set by WooCommerce */if( in_array( 'in-stock', $status ) ) { $output =__( 'In stock', 'wp-clips' ); $class ='in-stock'; }elseif( in_array( 'available-on-backorder', $status ) ) { $output =__( 'Available on backorder', 'wp-clips' ); $class ='available-on-backorder'; }elseif( in_array( 'out-of-stock', $status ) ) { $output =__( 'Out of stock', 'wp-clips' ); $class ='out-of-stock'; }// Append output if some items out of stock or available on backorderif( ( in_array( 'available-on-backorder', $status ) && $class =='in-stock' ) || ( in_array( 'out-of-stock', $status ) && $class !='out-of-stock' ) ) $append =' '.__( '(some items)', 'wp-clips' ); }// Output only if set if( isset( $availability ) ){echo'<span class="archive-stock '.esc_attr( $class ) .'">'.esc_html( $output ) .esc_html( $append ) .'</span>'; }}
As with the other snippets you can customize it with this piece of CSS.
/* Display Woocommerce stock amount and stock status on Woocommerce archive pages */
.archive-stock {
font-size: 13px;
margin: 5px 0px 10px 0px;
}
How to change Woocommerce add to cart button text if product is in backorder?
IThere are people who are confused about the backorders and different Woocommerce stock statuses. This lead to to problem where custome did not realize that the product was available on backorder (that means out of stock) and ordered it knowing that it will ship soon.
Therefore, you may want to change Woocommerce add to cart button text only for backorder products. Now, if you combine it with the custom field (shown above) then the end result will look like this:
Looks good, isn’t it? So, use this code snippet.
// changes Woocommerce Single product page add to cart button only text if product is in backorderadd_filter( 'woocommerce_product_single_add_to_cart_text', 'wc_ninja_change_backorder_button', 10, 2 );functionwc_ninja_change_backorder_button( $text, $product ){if ( $product->is_on_backorder( 1 ) ) { $text =__( 'Pre-order', 'woocommerce' ); }return $text;}// changes Woocommerce category page add to cart button only text if product is in backorderadd_filter( 'woocommerce_product_add_to_cart_text', 'wc_ninja_change_backorder_button1', 10, 2 );functionwc_ninja_change_backorder_button1( $text, $product ){if ( $product->is_on_backorder( 1 ) ) { $text =__( 'Pre-order', 'woocommerce' ); }return $text; }
How to create and display custom product field?
First, let’s create a new Woocommerce single product custom field and let’s output it in the desired place. The end result will look like the one here below on the screenshot.
So, just grab this code and use it accordingly.
<span role="button" tabindex="0" data-code="// Add custom field to Woocommerce backend under General tab
add_action( 'woocommerce_product_options_general_product_data', 'wpsh_add_text_field' );
function wpsh_add_text_field() {
woocommerce_wp_text_input( array(
'id' => '_shipping_time',
'label' => __( 'Shipping info', 'woocommerce' ),
'description' => __( 'This is a custom field, you can write here anything you want.', 'woocommerce' ),
'desc_tip' => 'true',
'type' => 'text'
) );
}
// Save custom field values
add_action( 'woocommerce_admin_process_product_object', 'wpsh_save_field', 10, 1 );
function wpsh_save_field( $product ) {
if ( isset( $_POST['_shipping_time'] ) ) {
$product->update_meta_data( '_shipping_time', sanitize_text_field( $_POST['_shipping_time'] ) );
}
}
// Display this custom field on Woocommerce single product pages
add_action( 'woocommerce_product_meta_end', 'wpsh_display_on_single_product_page', 10 );
function wpsh_display_on_single_product_page() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get meta
$text = $product->get_meta( '_shipping_time' );
// NOT empty
if ( ! empty ( $text ) ) {
echo '<div class="woocommerce-message">Estimated shipping time: ' . $text . '</div>';
}
}
}
// Display this custom field on Woocommerce archive pages
add_action( 'woocommerce_after_shop_loop_item', 'wpsh_display_on_archive_page', 10 );
function wpsh_display_on_archive_page() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get meta
$text = $product->get_meta( '_shipping_time' );
// NOT empty
if ( ! empty ( $text ) ) {
echo '<div class="custom-text">Estimated shipping time: ' . $text . '
// Add custom field to Woocommerce backend under General tabadd_action( 'woocommerce_product_options_general_product_data', 'wpsh_add_text_field' );functionwpsh_add_text_field() {woocommerce_wp_text_input( array('id'=>'_shipping_time','label'=>__( 'Shipping info', 'woocommerce' ),'description'=>__( 'This is a custom field, you can write here anything you want.', 'woocommerce' ),'desc_tip'=>'true','type'=>'text' ) );}// Save custom field valuesadd_action( 'woocommerce_admin_process_product_object', 'wpsh_save_field', 10, 1 );functionwpsh_save_field( $product ) {if ( isset( $_POST['_shipping_time'] ) ) { $product->update_meta_data( '_shipping_time', sanitize_text_field( $_POST['_shipping_time'] ) ); }}// Display this custom field on Woocommerce single product pagesadd_action( 'woocommerce_product_meta_end', 'wpsh_display_on_single_product_page', 10 );functionwpsh_display_on_single_product_page() {global $product;// Is a WC productif ( is_a( $product, 'WC_Product' ) ) {// Get meta $text = $product->get_meta( '_shipping_time' );// NOT emptyif ( !empty ( $text ) ) {echo'<div class="woocommerce-message">Estimated shipping time: '. $text .'</div>'; } }}// Display this custom field on Woocommerce archive pagesadd_action( 'woocommerce_after_shop_loop_item', 'wpsh_display_on_archive_page', 10 );functionwpsh_display_on_archive_page() {global $product;// Is a WC productif ( is_a( $product, 'WC_Product' ) ) {// Get meta $text = $product->get_meta( '_shipping_time' );// NOT emptyif ( !empty ( $text ) ) {echo'<div class="custom-text">Estimated shipping time: '. $text .'</div>'; } }}
How to create custom tab?
This snippet here below creates new custom Woocommerce single product tab and it contains text and contact form. Just replace the content as you like and you’re good to go.
add_filter( 'woocommerce_product_tabs', 'custom_tab' );functioncustom_tab( $tabs ) { // Adds the new tab $tabs['form'] =array('title'=>__( 'Send us an enquiry', 'woocommerce' ),'priority'=>30,'callback'=>'custom_tab_content' );return $tabs;}functioncustom_tab_content() {// The new tab contentecho'<h4>Send us an enquiry</h4>';echo'<p>Lorem ipsum dolor sit amet consectetur adipiscing elit ultricies convallis volutpat, placerat proin scelerisque eget velit tellus at nibh risus. </p>';echodo_shortcode( '' );}
How to display “You save” for sale price?
Take a look at the screenshot here below. This is what these snippets are doing for you.
So, in order to make it work like that use this snippet. Pay attention, that this one here below works only with simple products. For variable products take a look at the nex snippet.
<span role="button" tabindex="0" data-code="function you_save_text_for_product() {
global $product;
// works for Simple and Variable type
$regular_price = get_post_meta( $product->get_id(), '_regular_price', true ); // 36.32
$sale_price = get_post_meta( $product->get_id(), '_sale_price', true ); // 24.99
If you would like to customize the look of it then take a look at the you_save_price CCS class. Now go to Customizer >> Additional CSS and add this small piece of CSS to customize it.
Now, this code above shows the saved amount only for simple products. If you would also like to show it for variable products, then add this piece of code.
<span role="button" tabindex="0" data-code="// For product variations (on variable products)
add_filter( 'woocommerce_available_variation', 'variable_product_saving_amount', 10, 3 );
function variable_product_saving_amount( $data, $product, $variation ) {
How to Display a Contact Form Only When a Woocommerce Product is Out Of Stock?
Code Snippets plugin is a free plugin that allows you to run PHP/HTML/CSS/JavaScript code snippets on your site without the need to modify functions.php. The plugin can be found here. Another option is to paste the code to your child theme’s functions.php file.
<span role="button" tabindex="0" data-code="/* Show contact form instead of "Out Of Stock" message */
add_action('woocommerce_single_product_summary', 'out_of_stock_show_form', 20);
function out_of_stock_show_form() {
global $product;
if(!$product->is_in_stock( )) {
echo '<div class="your-form">';
echo 'Unfortunately this product is out of stock.<br>Contact us for more information';
echo do_shortcode('[your-shortcode]'); // Replace with your own contact form shortcode
echo '</div>';
}
}
/* This will show your contact form when the variation is out of stock */
add_filter( 'woocommerce_available_variation', 'variation_out_of_stock_show_form', 10, 3 );
function variation_out_of_stock_show_form( $data, $product, $variation ) {
if( ! $data['is_in_stock'] )
{
$data['availability_html'] = '<div class="your-form">';
$data['availability_html'] .= 'Unfortunately this product is out of stock.<br>Contact us for more information';
$data['availability_html'] .= do_shortcode('[your-shortcode]'); // Replace with your own contact form shortcode
$data['availability_html'] .= '
/* Show contact form instead of "Out Of Stock" message */add_action('woocommerce_single_product_summary', 'out_of_stock_show_form', 20);functionout_of_stock_show_form() {global $product;if(!$product->is_in_stock( )) {echo'<div class="your-form">';echo'Unfortunately this product is out of stock.<br>Contact us for more information';echodo_shortcode('[your-shortcode]'); // Replace with your own contact form shortcodeecho'</div>'; }}/* This will show your contact form when the variation is out of stock */add_filter( 'woocommerce_available_variation', 'variation_out_of_stock_show_form', 10, 3 );functionvariation_out_of_stock_show_form( $data, $product, $variation ) {if( ! $data['is_in_stock'] ) { $data['availability_html'] ='<div class="your-form">'; $data['availability_html'] .='Unfortunately this product is out of stock.<br>Contact us for more information'; $data['availability_html'] .=do_shortcode('[your-shortcode]'); // Replace with your own contact form shortcode $data['availability_html'] .='</div>'; }return $data;}
Pay attention to the line which contains [your-shortcode]. This is the place you should add your contact form shortcode.
How to disable (grey out) selection of out-of-stock variation?
For your customers, it is much easier to choose only the available variations. Therefore, I’ll show you how to disable /grey out) a selection of out-of-stock product variations.
How to add custom stock status to products in WooCommerce?
Woocommerce comes with default stock statuses “in stock”, “out of stock” and “available on backorder”. If you would like to add custom stock status to products in WooCommerce then use this snippet here below.
This snippet here below adds four additional stock statuses to Woocommerce.
Pre order
Due in stock in 14 days
Due in stock in 21 days
Due in stock in 31 days
This is the end result.
Pay attention that to add or remove your custom stock statuses you need to remove some lines accordingly. Take a look at the video above that shows how to do that (see video starting at 2:10) .
<span role="button" tabindex="0" data-code="// Add custom stock status to products in WooCommerce
function filter_woocommerce_product_stock_status_options( $status ) {
// Add new statuses
$status['pre_order'] = __( 'Pre order', 'woocommerce' );
$status['due_in_14'] = __( 'Due in stock in 14 days', 'woocommerce' );
$status['due_in_21'] = __( 'Due in stock in 21 days', 'woocommerce' );
$status['due_in_31'] = __( 'Due in stock in 31 days', 'woocommerce' );
return $status;
}
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );
// Availability text
function filter_woocommerce_get_availability_text( $availability, $product ) {
// Get stock status
switch( $product->get_stock_status() ) {
case 'pre_order':
$availability = __( 'Pre order', 'woocommerce' );
break;
case 'due_in_14':
$availability = __( 'Due in stock in 14 days', 'woocommerce' );
break;
case 'due_in_21':
$availability = __( 'Due in stock in 21 days', 'woocommerce' );
break;
case 'due_in_31':
$availability = __( 'Due in stock in 31 days', 'woocommerce' );
break;
}
// Admin stock html
function filter_woocommerce_admin_stock_html( $stock_html, $product ) {
// Simple
if ( $product->is_type( 'simple' ) ) {
// Get stock status
$product_stock_status = $product->get_stock_status();
// Variable
} elseif ( $product->is_type( 'variable' ) ) {
foreach( $product->get_visible_children() as $variation_id ) {
// Get product
$variation = wc_get_product( $variation_id );
// Get stock status
$product_stock_status = $variation->get_stock_status();
/*
Currently the status of the last variant in the loop will be displayed.
So from here you need to add your own logic, depending on what you expect from your custom stock status.
By default, for the existing statuses. The status displayed on the admin products list table for variable products is determined as:
– Product should be in stock if a child is in stock.
– Product should be out of stock if all children are out of stock.
– Product should be on backorder if all children are on backorder.
– Product should be on backorder if at least one child is on backorder and the rest are out of stock.
*/
}
}
// Stock status
switch( $product_stock_status ) {
case 'pre_order':
$stock_html = '<mark class="pre-order" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'Pre order', 'woocommerce' ) . '</mark>';
break;
case 'due_in_14':
$stock_html = '<mark class="due-in-14" style="background:transparent none;color:#666;font-weight:700;line-height:1;">' . __( 'Due in stock in 14 days', 'woocommerce' ) . '</mark>';
break;
case 'due_in_21':
$stock_html = '<mark class="due-in-21" style="background:transparent none;color:#2a2a2a;font-weight:700;line-height:1;">' . __( 'Due in stock in 21 days', 'woocommerce' ) . '</mark>';
break;
case 'due_in_31':
$stock_html = '<mark class="due-in-31" style="background:transparent none;color:#2a2a2a;font-weight:700;line-height:1;">' . __( 'Due in stock in 31 days', 'woocommerce' ) . '
// Add custom stock status to products in WooCommercefunctionfilter_woocommerce_product_stock_status_options( $status ) {// Add new statuses $status['pre_order'] =__( 'Pre order', 'woocommerce' ); $status['due_in_14'] =__( 'Due in stock in 14 days', 'woocommerce' ); $status['due_in_21'] =__( 'Due in stock in 21 days', 'woocommerce' ); $status['due_in_31'] =__( 'Due in stock in 31 days', 'woocommerce' );return $status;}add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );// Availability textfunctionfilter_woocommerce_get_availability_text( $availability, $product ) {// Get stock statusswitch( $product->get_stock_status() ) {case'pre_order': $availability =__( 'Pre order', 'woocommerce' );break;case'due_in_14': $availability =__( 'Due in stock in 14 days', 'woocommerce' );break;case'due_in_21': $availability =__( 'Due in stock in 21 days', 'woocommerce' );break;case'due_in_31': $availability =__( 'Due in stock in 31 days', 'woocommerce' );break; }return $availability; }add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );// Availability CSS classfunctionfilter_woocommerce_get_availability_class( $class, $product ) {// Get stock statusswitch( $product->get_stock_status() ) {case'pre_order': $class ='pre-order';break;case'due_in_14': $class ='due-in-14';break;case'due_in_21': $class ='due-in-21';break;case'due_in_31': $class ='due-in-31';break; }return $class;}add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );// Admin stock htmlfunctionfilter_woocommerce_admin_stock_html( $stock_html, $product ) {// Simpleif ( $product->is_type( 'simple' ) ) {// Get stock status $product_stock_status = $product->get_stock_status();// Variable } elseif ( $product->is_type( 'variable' ) ) {foreach( $product->get_visible_children() as $variation_id ) {// Get product $variation =wc_get_product( $variation_id );// Get stock status $product_stock_status = $variation->get_stock_status();/* Currently the status of the last variant in the loop will be displayed. So from here you need to add your own logic, depending on what you expect from your custom stock status. By default, for the existing statuses. The status displayed on the admin products list table for variable products is determined as: - Product should be in stock if a child is in stock. - Product should be out of stock if all children are out of stock. - Product should be on backorder if all children are on backorder. - Product should be on backorder if at least one child is on backorder and the rest are out of stock. */ } }// Stock statusswitch( $product_stock_status ) {case'pre_order': $stock_html ='<mark class="pre-order" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">'.__( 'Pre order', 'woocommerce' ) .'</mark>';break;case'due_in_14': $stock_html ='<mark class="due-in-14" style="background:transparent none;color:#666;font-weight:700;line-height:1;">'.__( 'Due in stock in 14 days', 'woocommerce' ) .'</mark>';break;case'due_in_21': $stock_html ='<mark class="due-in-21" style="background:transparent none;color:#2a2a2a;font-weight:700;line-height:1;">'.__( 'Due in stock in 21 days', 'woocommerce' ) .'</mark>';break;case'due_in_31': $stock_html ='<mark class="due-in-31" style="background:transparent none;color:#2a2a2a;font-weight:700;line-height:1;">'.__( 'Due in stock in 31 days', 'woocommerce' ) .'</mark>';break; }return $stock_html;}add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );
How to remove Woocommerce reviews tab?
See another video I made on how to create, rename, remove and reorder custom product tabs.
How to create custom order statuses for Woocommerce?
Now let’s take a look at how to create custom order status for Woocommerce. With the help of this snippet I’m going to create a custom status called “In-progress” that is displayed on both backend and front-end order pages.
How to display customer phone and email in Woocommerce orders table?
Take a look at the screenshot below, and you’ll see that there is a customer phone and email that is displayed in Woocommerce orders table. If you would like to achieve the same result, then use this snippet here below.
How to autocomplete Woocommerce processing orders?
Why would you need to autocomplete Woocommerce processing orders? For example, if you’re selling virtual product (Courses etc.) and you would like to add an access to it right after payments.
But (just “but) what if you would like to autocomplete all Woocommerce orders? That is evan on-hold orders? If this is the case then use this nippet here below.
// Auto Complete all WooCommerce orders.add_action( 'woocommerce_thankyou', 'wpsh_complete_all_orders' );functionwpsh_complete_all_orders( $order_id ) { if ( ! $order_id ) {return; } $order =wc_get_order( $order_id ); $order->update_status( 'completed' );}
How to Add Purchased products tab to Woocommerce my account page?
Now let’s add Purchased products tab to Woocommerce my account page (se the screenshot above). This tabb displays your recently purchased products in the separate tabe.
<span role="button" tabindex="0" data-code="// Add Purchased products tab to Woocommerce my account page
// here we hook the My Account menu links and add our custom one
add_filter( 'woocommerce_account_menu_items', 'wpsh_purchased_products', 40 );
function wpsh_purchased_products( $menu_links ){
// Set 5 in this line to something else if you would like to move the position of the tab
return array_slice( $menu_links, 0, 5, true )
+ array( 'purchased-products' => 'Purchased Products' )
+ array_slice( $menu_links, 2, NULL, true );
// Add purchased porducts as a tab content
add_action( 'woocommerce_account_purchased-products_endpoint', 'wpsh_purchased_products_content' );
function wpsh_purchased_products_content() {
global $wpdb;
// Purchased products are sorted by date
$purchased_products_ids = $wpdb->get_col(
$wpdb->prepare(
"
SELECT itemmeta.meta_value
FROM " . $wpdb->prefix . "woocommerce_order_itemmeta itemmeta
INNER JOIN " . $wpdb->prefix . "woocommerce_order_items items
ON itemmeta.order_item_id = items.order_item_id
INNER JOIN $wpdb->posts orders
ON orders.ID = items.order_id
INNER JOIN $wpdb->postmeta ordermeta
ON orders.ID = ordermeta.post_id
WHERE itemmeta.meta_key = '_product_id'
AND ordermeta.meta_key = '_customer_user'
AND ordermeta.meta_value = %s
ORDER BY orders.post_date DESC
",
get_current_user_id()
)
);
// Add Purchased products tab to Woocommerce my account page// here we hook the My Account menu links and add our custom oneadd_filter( 'woocommerce_account_menu_items', 'wpsh_purchased_products', 40 );functionwpsh_purchased_products( $menu_links ){// Set 5 in this line to something else if you would like to move the position of the tabreturnarray_slice( $menu_links, 0, 5, true )+array( 'purchased-products'=>'Purchased Products' )+array_slice( $menu_links, 2, NULL, true );}add_action( 'init', 'wpsh_purchased_products_endpoint' );functionwpsh_purchased_products_endpoint() {add_rewrite_endpoint( 'purchased-products', EP_PAGES );}// Add purchased porducts as a tab contentadd_action( 'woocommerce_account_purchased-products_endpoint', 'wpsh_purchased_products_content' );functionwpsh_purchased_products_content() {global $wpdb;// Purchased products are sorted by date $purchased_products_ids = $wpdb->get_col( $wpdb->prepare(" SELECT itemmeta.meta_value FROM ". $wpdb->prefix ."woocommerce_order_itemmeta itemmeta INNER JOIN ". $wpdb->prefix ."woocommerce_order_items items ON itemmeta.order_item_id = items.order_item_id INNER JOIN $wpdb->posts orders ON orders.ID = items.order_id INNER JOIN $wpdb->postmeta ordermeta ON orders.ID = ordermeta.post_id WHERE itemmeta.meta_key = '_product_id' AND ordermeta.meta_key = '_customer_user' AND ordermeta.meta_value = %s ORDER BY orders.post_date DESC ",get_current_user_id() ) );// Don’t display duplicate products $purchased_products_ids =array_unique( $purchased_products_ids );if( !empty( $purchased_products_ids ) ) {echo'<div class="woocommerce-message">Purchased products</div>'; $purchased_products =newWP_Query( array('post_type'=>'product','post_status'=>'publish','post__in'=> $purchased_products_ids,'orderby'=>'post__in','posts_per_page'=>-1, ) );woocommerce_product_loop_start();while ( $purchased_products->have_posts() ) : $purchased_products->the_post();wc_get_template_part( 'content', 'product' );endwhile;woocommerce_product_loop_end();woocommerce_reset_loop();wp_reset_postdata(); } else {// Change this text if needed)echo'Unfortunately you have no purchases yet'; }}
How to add “Order again” button to Woocommerce my account page?
As menitonet above, we’re goint to add “Order again” button to Woocommerce my account page. Use this snippet here below.
How to add “Cancel” button to Woocommerce my account page?
Take a look at this screenshot here. As you see, there are two custom buttons (“Cancel” and “Order again” buttons). So, if you would like to add “Cancel” button to Woocommerce my account page, then use this snippet here below.
NB! Pay attention at the line 8. In it you can set the delay in days. Currently it is set to 3 dayst, that is withnin 3 days it’s allowed to cancel the order.
// Add "Cancel" button to Woocommerce my account pageadd_filter( 'woocommerce_valid_order_statuses_for_cancel', 'wpsh_add_cancel_button', 20, 2 );functionwpsh_add_cancel_button( $statuses, $order ='' ){// Set HERE the order statuses where you want the cancel button to appear $custom_statuses =array( 'pending', 'processing', 'on-hold', 'failed' );// Set HERE the delay (in days) $duration =3; // 3 days// UPDATE: Get the order ID and the WC_Order objectif( !is_object( $order ) &&isset($_GET['order_id']) ) $order =wc_get_order( absint( $_GET['order_id'] ) ); $delay = $duration*24*60*60; // (duration in seconds) $date_created_time =strtotime($order->get_date_created()); // Creation date time stamp $date_modified_time =strtotime($order->get_date_modified()); // Modified date time stamp $now =strtotime("now"); // Now time stamp// Using Creation date time stampif ( ( $date_created_time + $delay ) >= $now ) return $custom_statuses;elsereturn $statuses;}
How to add Woocommerce product ID column?
Next, let’s customize Woocommerce admin dashboard further. Sometimes you may need to visually see your Woocommerce product ID columns. This snippet will help you out.