Improve Woocommerce with code snippet

calendar
icon view 1516
icon comment 0
icon time 5 min.

In the list of best practices when customizing our website developed in WordPress we find the one of adding hooks to the functions.php file of our child template (in case we are using a third party template).

What WordPress Hooks are?

For those who don’t know what hooks are, hooks are functions that can be applied to an action or filter in WordPress to change its default functionality in the system.

Thanks to the huge WordPress community, there is a lot of documentation on the internet, with code snippets ready to copy/paste and use in our site instantly.

Woocommerce hooks

In this article I am going to share those code snippets that have been helpful to me in the development of WordPress websites as a repository.

Woocommerce code snippets

Redirect user to the Checkout page

If we want to redirect the user directly to the Woocommerce “Checkout” page once the user adds a product to the cart, add this code.

function cod_redirect_checkout_add_cart( $url ) {
$page = get_page_by_title( "Checkout" );
$url = get_permalink($page->ID);
return $url;
}

Delete fields at checkout

In a purchase process, the fewer data the user must enter to complete the order, the better. If you do not need any of the default ones you can use this hook.

In this case we will remove the “Company name” field, but we can hide the one we need.

function quadlayers_remove_checkout_fields( $fields ) {
unset($fields['billing']['billing_company']);
return $fields;
}

Remove links to product details

Si por algún motivo no te interesa que el usuario acceda a la página de detalle del producto puedes eliminar el enlace en la página del catálogo con este código.

If for some reason you are not interested in the user accessing the product detail page you can remove the link on the catalog page with this code.

remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );

Disables the zoom effect on the product image

Si no es necesario visualizar al detalle la imagen del producto, puede que te interese deshabilitar esta opción para mejorar la experiencia de usuario y no distraerlo con funciones que no aporten valor durante la navegación.

If it is not necessary to display the product image in detail, you may want to disable this option to improve the user experience and not distract the user with features that do not add value during navigation.

add_action( 'wp', 'njengah_remove_zoom_effect_theme_support', 99 );
function njengah_remove_zoom_effect_theme_support() {
remove_theme_support( 'wc-product-gallery-zoom' );
remove_theme_support( 'wc-product-gallery-lightbox' );
remove_theme_support( 'wc-product-gallery-slider' );
}

Select product variations from the catalog page

By default Woocommerce allows you to select product variations from the details page, if you need to add this option while the user is browsing the store catalog use this code.

add_action( 'woocommerce_before_shop_loop', 'handsome_bearded_guy_select_variations' );

function handsome_bearded_guy_select_variations() {
remove_action( ‘woocommerce_after_shop_loop_item’, ‘woocommerce_template_loop_add_to_cart’, 10 );
add_action( ‘woocommerce_after_shop_loop_item’, ‘woocommerce_template_single_add_to_cart’, 30 );
}

Deletes the quantity field

You don’t want the user to be able to select the quantity of a product? With this code you automatically remove the quantity field from the form on the detail page.

function cw_remove_quantity_fields( $return, $product ) {
return true;
}
add_filter( 'woocommerce_is_sold_individually', 'cw_remove_quantity_fields', 10, 2 );

Removes product tabs on the detail page

Depending on the features product you have in the ecommerce, you may want to temporarily hide some tabs on the detail page. By editing the code you can choose to keep the one you want. For example keep the comments and additional information, because the short description is enough.

add_filter( 'woocommerce_product_tabs', 'my_remove_all_product_tabs', 98 );
function my_remove_all_product_tabs( $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;
}

Hide all shipping methods when free shipping methods are available

If you offer the user a free shipping method, why show them other options? This code snippet will allow you to hide other shipping methods and leave only free shipping.


add_filter('woocommerce_available_shipping_methods', 'hide_all_shipping_when_free_is_available', 10, 1);
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'];
// 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;
}

SMTP send function

We can find ourselves in the situation that when finalizing an order or recovering the password, the user does not receive the corresponding emails. It may happen that when working with shared hostings, most of them have blocked the sending of emails from unverified accounts. To solve this problem we must configure the SMTP server in WordPress.

We can also make use of plugins that will facilitate the process, but if we can do without third parties better. You only have to edit the parameters of the email account and the SMTP server of your hosting in the code that you can see below and that’s it.


add_action('phpmailer_init','send_smtp_email');
function send_smtp_email( $phpmailer )
{
// Define que estamos enviando por SMTP
$phpmailer->isSMTP();
//La dirección del HOST del servidor de correo SMTP p.e. smtp.midominio.com
$phpmailer->Host = "your server smtp address";
// Uso autenticación por SMTP (true|false)
$phpmailer->SMTPAuth = true;
// Puerto SMTP - Suele ser el 25, 465 o 587
$phpmailer->Port = "587";
// Usuario de la cuenta de correo
$phpmailer->Username = "user name";
// Contraseña para la autenticación SMTP
$phpmailer->Password = "password";
// El tipo de encriptación que usamos al conectar - ssl (deprecated) o tls
$phpmailer->SMTPSecure = "tls";
$phpmailer->From = "tucuenta@decorreo.com";
$phpmailer->FromName = "Tu nombre";
}

These are just some of the code snippets that we can use thanks to the hooks offered by Woocommerce through its API. The list will grow over time.

In this page you can see all the hooks available for Woocommerce

Buscar artículos por:

Categoría/s: Design and development web


Entradas relacionadas

Comentarios

Aún no hay comentarios. Se el primero en dejar tu opinión sobre este artículo.

Leave a Reply

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

*

Al comentar aceptas nuestra política de privacidad y política de cookies