Tips on how to customise Woocommerce Single Product Web page | 14 Helpful Hacks
September 4, 2024
On this video I’m going to indicate you 14 helpful Woocommerce single product web page hacks you need to use in each retailer. If you need to make use of these snippets right here beneath then simply add those you want inside your little one theme’s capabilities.php file or higher but, use Code Snippets plugin. This manner you’re not going to lose these modifications in case you’re switching your theme.
WpCodeBox is my favourite code snippets supervisor for WordPress. It is a premium plugin and in the event you’re , then seize WPCodeBox with a pleasant 20% low cost right here (SAVE 20% Coupon WPSH20).
Additionally, check out the video tutorial right here beneath after which it is going to in all probability be a bit simpler to know what’s what.
Video: Tips on how to use Woocommerce Single Product Web page Hacks
Tips on how to create and show {custom} product subject?
First, let’s create a brand new Woocommerce single product {custom} subject and let’s output it within the desired place. The tip outcome will appear like the one right here beneath on the screenshot.
So, simply seize this code and use it accordingly.
<span position="button" tabindex="0" data-code="// Add {custom} subject to Woocommerce backend beneath Common tab
add_action( 'woocommerce_product_options_general_product_data', 'wpsh_add_text_field' );
perform wpsh_add_text_field() {
woocommerce_wp_text_input( array(
'id' => '_shipping_time',
'label' => __( 'Delivery information', 'woocommerce' ),
'description' => __( 'It is a {custom} subject, you may write right here something you need.', 'woocommerce' ),
'desc_tip' => 'true',
'kind' => 'textual content'
) );
}
// Show this practice subject on Woocommerce single product pages
add_action( 'woocommerce_product_meta_end', 'wpsh_display_on_single_product_page', 10 );
perform wpsh_display_on_single_product_page() {
world $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get meta
$textual content = $product->get_meta( '_shipping_time' );
// NOT empty
if ( ! empty ( $textual content ) ) {
echo '<div class="woocommerce-message">Estimated transport time: ' . $textual content . '</div>';
}
}
}
// Show this practice subject on Woocommerce archive pages
add_action( 'woocommerce_after_shop_loop_item', 'wpsh_display_on_archive_page', 10 );
perform wpsh_display_on_archive_page() {
world $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get meta
$textual content = $product->get_meta( '_shipping_time' );
// NOT empty
if ( ! empty ( $textual content ) ) {
echo '<div class="custom-text">Estimated transport time: ' . $textual content . '
// Add {custom} subject to Woocommerce backend beneath Common tabadd_action( 'woocommerce_product_options_general_product_data', 'wpsh_add_text_field' );performwpsh_add_text_field() {woocommerce_wp_text_input( array('id'=>'_shipping_time','label'=>__( 'Delivery information', 'woocommerce' ),'description'=>__( 'It is a {custom} subject, you may write right here something you need.', 'woocommerce' ),'desc_tip'=>'true','kind'=>'textual content' ) );}// Save {custom} subject valuesadd_action( 'woocommerce_admin_process_product_object', 'wpsh_save_field', 10, 1 );performwpsh_save_field( $product ) {if ( isset( $_POST['_shipping_time'] ) ) { $product->update_meta_data( '_shipping_time', sanitize_text_field( $_POST['_shipping_time'] ) ); }}// Show this practice subject on Woocommerce single product pagesadd_action( 'woocommerce_product_meta_end', 'wpsh_display_on_single_product_page', 10 );performwpsh_display_on_single_product_page() {world $product;// Is a WC productif ( is_a( $product, 'WC_Product' ) ) {// Get meta $textual content = $product->get_meta( '_shipping_time' );// NOT emptyif ( !empty ( $textual content ) ) {echo'<div class="woocommerce-message">Estimated transport time: '. $textual content .'</div>'; } }}// Show this practice subject on Woocommerce archive pagesadd_action( 'woocommerce_after_shop_loop_item', 'wpsh_display_on_archive_page', 10 );performwpsh_display_on_archive_page() {world $product;// Is a WC productif ( is_a( $product, 'WC_Product' ) ) {// Get meta $textual content = $product->get_meta( '_shipping_time' );// NOT emptyif ( !empty ( $textual content ) ) {echo'<div class="custom-text">Estimated transport time: '. $textual content .'</div>'; } }}
Tips on how to create {custom} tab?
This snippet right here beneath creates new {custom} Woocommerce single product tab and it accommodates textual content and call type. Simply substitute the content material as you want and also you’re good to go.
add_filter( 'woocommerce_product_tabs', 'custom_tab' );performcustom_tab( $tabs ) { // Provides the brand new tab $tabs['form'] =array('title'=>__( 'Ship us an enquiry', 'woocommerce' ),'precedence'=>30,'callback'=>'custom_tab_content' );return $tabs;}performcustom_tab_content() {// The brand new tab content materialecho'<h4>Ship 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( '' );}
Tips on how to transfer Woocommerce associated merchandise and upsells product in a tab?
It’s straightforward. If you wish to transfer Woocommerce associated merchandise in a tab then use this snippet.
/* Associated merchandise IN CUSTOM TAB */// Take away associated merchandise from authentic locationremove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);// Create a brand new {custom} tabadd_filter( 'woocommerce_product_tabs', 'related_product_tab' );performrelated_product_tab( $tabs ) { $tabs['related-products'] =array('title'=>__( 'Associated merchandise', 'woocommerce' ),'precedence'=>50,'callback'=>'related_product_tab_content');return $tabs;}// Add associated merchandise inside new tabperformrelated_product_tab_content() {woocommerce_output_related_products();}
If you wish to transfer Woocommerce upsell merchandise in a tab then use this snippet.
/* UPSELLS IN CUSTOM TAB */// Take away uplsells merchandise from authentic locationremove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );// Create a brand new {custom} tabadd_filter( 'woocommerce_product_tabs', 'upsells_product_tab' );performupsells_product_tab( $tabs ) { $tabs['upsells-products'] =array('title'=>__( 'Appropriate merchandise', 'woocommerce' ),'precedence'=>50,'callback'=>'upsells_tab_content');return $tabs;}// Add upsells inside new tabperformupsells_tab_content() {woocommerce_upsell_display();}
Tips on how to scroll on to Woocommerce product tab and open it routinely?
This code snippet right here beneath provides hyperlinks above add to cart button. If you happen to click on on the hyperlink it is going to scroll to the product tab and routinely opens it. If you happen to’re utilizing {custom} tabs then add a brand new hyperlink with the {custom} tab identify.
// LINK TO SCROLL TO DESC TAB
if ( $post->post_content ) {
echo '<p><a category="jump-to-tab" href="#tab-description">' . __( 'Learn extra', 'woocommerce' ) . ' →</a></p>';
}
// LINK TO SCROLL TO ADDITIONAL INFO TAB
if ( $product && ( $product->has_attributes() || apply_filters( 'wc_product_enable_dimensions_display', $product->has_weight() || $product->has_dimensions() ) ) ) {
echo '<p><a category="jump-to-tab" href="#tab-additional_information">' . __( 'Extra data', 'woocommerce' ) . ' →</a></p>';
}
// LINK TO SCROLL TO REVIEWS TAB
if ( comments_open() ) {
echo '<p><a category="jump-to-tab" href="#tab-reviews">' . __( 'Critiques', 'woocommerce' ) . ' →</a></p>';
}
?>
<script>
jQuery(doc).prepared(perform($){
$('a.jump-to-tab').click on(perform(e){
e.preventDefault();
var tabhash = $(this).attr("href");
var tabli = 'li.' + tabhash.substring(1);
var tabpanel = '.panel' + tabhash;
$(".wc-tabs li").every(perform() {
if ( $(this).hasClass("lively") ) {
$(this).removeClass("lively");
}
});
$(tabli).addClass("lively");
$(".woocommerce-tabs .panel").css("show","none");
$(tabpanel).css("show","block");
$('html,physique').animate({scrollTop:$(tabpanel).offset().high -150},'sluggish'); // Set your offset by altering -150 worth
});
});
</script>
add_action( 'woocommerce_single_product_summary', 'scroll_to_product_tab', 21 );performscroll_to_product_tab() {world $put up, $product; // LINK TO SCROLL TO DESC TABif ( $put up->post_content ) {echo'<p><a category="jump-to-tab" href="#tab-description">'.__( 'Learn extra', 'woocommerce' ) .' →</a></p>'; }// LINK TO SCROLL TO ADDITIONAL INFO TABif ( $product && ( $product->has_attributes() ||apply_filters( 'wc_product_enable_dimensions_display', $product->has_weight() || $product->has_dimensions() ) ) ) {echo'<p><a category="jump-to-tab" href="#tab-additional_information">'.__( 'Extra data', 'woocommerce' ) .' →</a></p>'; }// LINK TO SCROLL TO REVIEWS TABif ( comments_open() ) {echo'<p><a category="jump-to-tab" href="#tab-reviews">'.__( 'Critiques', 'woocommerce' ) .' →</a></p>'; }?><script>jQuery(doc).prepared(perform($){ $('a.jump-to-tab').click on(perform(e){e.preventDefault();vartabhash= $(this).attr("href");vartabli='li.'+tabhash.substring(1);vartabpanel='.panel'+tabhash; $(".wc-tabs li").every(perform() {if ( $(this).hasClass("lively") ) { $(this).removeClass("lively"); } }); $(tabli).addClass("lively"); $(".woocommerce-tabs .panel").css("show","none"); $(tabpanel).css("show","block"); $('html,physique').animate({scrollTop:$(tabpanel).offset().high-150},'sluggish'); // Set your offset by altering -150 worth }); });</script><?php}
Tips on how to create a direct hyperlink to your product tab?
Possibly that you must ship a hyperlink with the particular product critiques to somebody and isntead of sending him/her the product hyperlink you may ship the hyperlink to the overview tab itself. So, use this snippet right here beneath and ship the hyperlink in a type https://yoursite.com&/demo-product/#tab-reviews
If you wish to hyperlink to another tab then simply substitute #tab-reviews with the proper tab hyperlink.
// when the tab is chosen replace the url with the hash
$( '.tabs a' ).click on( perform() {
window.location.hash = 'tab-' + $(this).father or mother('li').attr("class").substitute(' lively', '').substitute('_tab', '');
});
});
</script>
performdirect_link_to_product_tabs() {if( is_product() ) {?><scriptkind="textual content/javascript">jQuery(doc).prepared(perform($) {if( window.location.hash ) {// Varsvartab=window.location.hash.substitute('#', '');// Tabs $( 'li.description_tab' ).removeClass( 'lively' ); $( 'a[href="#'+tab+'"]' ).father or mother( 'li' ).addClass( 'lively' );// Tabs content material $( '#tab-description' ).cover(); $( '#tab-'+tab.substitute( 'tab-', '' ) ).present(); }// when the tab is chosen replace the url with the hash $( '.tabs a' ).click on( perform() { window.location.hash='tab-'+ $(this).father or mother('li').attr("class").substitute(' lively', '').substitute('_tab', ''); }); });</script><?php }}add_action( 'wp_footer', 'direct_link_to_product_tabs', 30 );
Tips on how to show “You save” on the market worth?
Check out the screenshot right here beneath. That is what these snippets are doing for you.
So, with a view to make it work like that use this snippet.
<span position="button" tabindex="0" data-code="perform you_save_text_for_product() {
world $product;
// works for Easy and Variable kind
$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 need to customise the look of it then check out the you_save_price CCS class. Now go to Customizer >> Extra CSS and add this small pies of CSS to customise it.
Now, this code above exhibits saved quantity solely for easy merchandise. If you need to indicate it additionally for variable merchandise, then add this piece of code.
// Get the product classes set within the product
$phrases = wp_get_post_terms( get_the_id(), 'product_cat' );
// Test that there’s at leat one product class set for the product
if(sizeof($phrases) > 0){
// Get the primary product class WP_Term object
$time period = reset($phrases);
// Get the time period hyperlink (button hyperlink)
$hyperlink = get_term_link( $time period, 'product_cat' );
// Button textual content
$textual content = __('← Again to class','woocommerce') . ' <sturdy>' . $term->identify . '</sturdy>';
add_action ('woocommerce_share','add_back_product_category_button', 5);performadd_back_product_category_button(){// Get the product classes set within the product $phrases =wp_get_post_terms( get_the_id(), 'product_cat' );// Test that there's at leat one product class set for the productif(sizeof($phrases) >0){// Get the primary product class WP_Term object $time period =reset($phrases);// Get the time period hyperlink (button hyperlink) $hyperlink =get_term_link( $time period, 'product_cat' );// Button textual content $textual content =__('← Again to class','woocommerce') .' <sturdy>'. $time period->identify .'</sturdy>';// Outputecho'<div class="back-to-category"><a href="'.esc_url($hyperlink).'" title="'.$textual content.'" '.$time period->slug.'">'.$textual content.'</a></div>'; }}
If you happen to woulkd prefer to customise this Woocommerce Again to class button then use this piece of CSS code.
Tips on how to change Woocommerce add to cart button textual content if product is in backorder?
I’ve had clients who’re confused in regards to the backorders they usually haven’t realized that the product is out of inventory and it should be pre-ordered. Due to this fact I wanted to vary Woocommerce add to cart button textual content for backorder merchandise. Now, if I mix it with the {custom} subject (hack #1 above) then the tip outcome will appear like this:
Seems to be good, isn’t it? So, use this code snippet.
Tips on how to add NEW badge for the Woocommerce merchandise newer than 30 days?
This snippet right here beneath will add NEW badge for the Woocommerce merchandise which can be newer than 30 days. In order for you it to be kind of than 30 days then change Newness in days quantity.
Tips on how to add estimated supply date and time in your Woocommerce single product web page?
With the assistance of this snippet it is possible for you to so as to add a estimated supply date and time in your Woocommerce single product web page. Check out the screenshot right here beneath.
add_action('woocommerce_after_add_to_cart_button', 'delivery_estimate');performdelivery_estimate() {date_default_timezone_set('Europe/Tallinn');// if FRI/SAT/SUN supply can be MONif (date('N') >=5) { $del_day =date("l jS F", strtotime("subsequent monday")); $order_by ="Monday"; }// if MON/THU after 4PM supply can be day after tomorrowelseif (date('H') >=16) { $del_day =date("l jS F", strtotime("tomorrow + 2 days")); $order_by ="day after tomorrow"; }// if MON/THU earlier than 4PM supply can be TOMORROWelse { $del_day =date("l jS F", strtotime("tomorrow")); $order_by ="at the moment"; } $html ="<br><div class='woocommerce-message' type='clear:each'>Order by 4PM {$order_by} for supply on {$del_day}</div>";echo $html;}
Tips on how to present estimated supply date for particular class?
If that you must present estimated supply date for particular product class then that you must add two traces of code. So, add this line slightly below your perform identify row. Additionally, you would want so as to add } signal to the final line.
if ( has_term( 'furnishings', 'product_cat' ) ) { // Change "furnishings" with your personal class slug
Simply substitute “furnishings” slug with your personal product class slug. So, that is the total code that can present estimated supply date for Furnishings class.
add_action( 'woocommerce_after_add_to_cart_form', 'specific_category_delivery_estimate' );performspecific_category_delivery_estimate() {if ( has_term( 'furnishings', 'product_cat' ) ) { // Change "furnishings" with your personal class slugdate_default_timezone_set( 'Europe/Tallinn' ); // if FRI/SAT/SUN supply can be MONif ( date( 'N' ) >=5 ) { $del_day =date( "l jS F", strtotime( "subsequent tuesday" ) ); $order_by ="Monday"; } // if MON/THU after 4PM supply can be day after tomorrowelseif ( date( 'H' ) >=16 ) { $del_day =date( "l jS F", strtotime( "tomorrow + 1 day" ) ); $order_by ="tomorrow"; } // if MON/THU earlier than 4PM supply can be TOMORROWelse { $del_day =date( "2 jS F", strtotime( "tomorrow" ) ); $order_by ="at the moment"; } $html ="<br><div class='woocommerce-message' type='clear:each'>Order by 4PM {$order_by} for supply on {$del_day}</div>";echo $html;}}
Tips on how to present estimated supply date for particular class or product?
Now, if that you must present this on a particular product web page then it really works equally as with the classes byt this time add this row as an alternative and substitute “426” with your personal product ID.
if ( is_single( '426' ) ) { // Change "426" with your personal product ID
And the total code for a particular product is that this.
add_action( 'woocommerce_after_add_to_cart_form', 'specific_product_delivery_estimate' );performspecific_product_delivery_estimate() {if ( is_single( '426' ) ) {date_default_timezone_set( 'Europe/Tallinn' ); // if FRI/SAT/SUN supply can be MONif ( date( 'N' ) >=5 ) { $del_day =date( "l jS F", strtotime( "subsequent tuesday" ) ); $order_by ="Monday"; } // if MON/THU after 4PM supply can be day after tomorrowelseif ( date( 'H' ) >=16 ) { $del_day =date( "l jS F", strtotime( "tomorrow + 1 day" ) ); $order_by ="tomorrow"; } // if MON/THU earlier than 4PM supply can be TOMORROWelse { $del_day =date( "2 jS F", strtotime( "tomorrow" ) ); $order_by ="at the moment"; } $html ="<br><div class='woocommerce-message' type='clear:each'>Order by 4PM {$order_by} for supply on {$del_day}</div>";echo $html;}}
Tips on how to add Woocommerce product enquiry type?
This snippet right here provides a button beneath your Add to cart button. If you happen to click on on the button then the contact type opens up. Simply substitute the contact type shortcode inside this snippet (line 6).
add_action( 'woocommerce_single_product_summary', 'enquiry_form', 30 );performenquiry_form() {echo'<button kind="submit" id="trigger_cf" class="button alt" type="margin-bottom: 20px;">Ship us your enquiry</button>';echo'<div id="product_inq" type="show:none">';echodo_shortcode('[your-shortcode-here]');echo'</div>';}add_action( 'woocommerce_single_product_summary', 'enquiry_form_1', 40);performenquiry_form_1() {?><scriptkind="textual content/javascript">jQuery('#trigger_cf').on('click on', perform(){if ( jQuery(this).textual content() =='Ship us your enquiry' ) {jQuery('#product_inq').css("show","block");jQuery("#trigger_cf").html('Shut'); } else {jQuery('#product_inq').cover();jQuery("#trigger_cf").html('Ship us your enquiry'); } });</script><?php}
You may also use conditionals. For instance, for the Clothes class merchandise I’m displaying one type and for all different merchandise one other type. simply substitute the class slug inside “clothes” a part of the code.
add_action( 'woocommerce_single_product_summary', 'enquiry_form', 30 );performenquiry_form() {if ( has_term( 'clothes', 'product_cat' ) ) {echo'<button kind="submit" id="trigger_cf" class="button alt">Ship us your enquiry</button>';echo'<div id="product_inq" type="show:none">';echodo_shortcode('');echo'</div>';} else {echo'<button kind="submit" id="trigger_cf" class="button alt" type="margin-bottom: 20px;">Ship us your enquiry</button>';echo'<div id="product_inq" type="show:none">';echodo_shortcode('');echo'</div>';}}add_action( 'woocommerce_single_product_summary', 'enquiry_form_1', 40);performenquiry_form_1() {?><scriptkind="textual content/javascript">jQuery('#trigger_cf').on('click on', perform(){if ( jQuery(this).textual content() =='Ship us your enquiry' ) {jQuery('#product_inq').css("show","block");jQuery("#trigger_cf").html('Shut'); } else {jQuery('#product_inq').cover();jQuery("#trigger_cf").html('Ship us your enquiry'); } });</script><?php}
Tips on how to disable (gray out) number of out-of-stock variation?
On your clients it’s a lot simpler to decide on solely the variations which can be obtainable. Due to this fact, I’ll present you how you can disable /gray out) number of out-of-stock product variations.
Tips on how to embrace a product thumbail alongside the identify and worth of Woocommerce grouped merchandise?
This small piece of code will add a product thumbnail for the Woocommerce product grouped merchandise desk. If you happen to wan to vary the picture measurement then simply modify the 40, 40 half.