Functions.php : Woocommerce Tricks

These are for my woocommerce custom codes and as always, please remember to create a child theme before installing these codes onto the functions.php file of your current theme.

To know how to create your own child theme, I recommend the Child Theme Configurator plugin.

When that is done, Activate your new child theme and Navigate to Appearance > Editor > functions.php

*** Also useful to note is anything is possible with Woocommerce with regards to custom filters and actions, here is their Action and Filter Hook Reference.***

To add a username field on checkout.

				
					function custom_override_checkout_fields( $fields ) {
$fields['account']['account_username']['placeholder'] = 'No symbols please';
$fields['account']['account_username']['label'] = 'Username';
$fields['account']['account_username']['type'] = 'text';
$fields['account']['account_username']['required'] = true;
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
				
			

To change the field label on the checkout form. For more field labels you can customize, click here.

				
					function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_company'] = array(
'label' => __('Dealership Name', 'woocommerce'),
'placeholder' => _x('Dealership Name', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
				
			

To customize add-to-cart button text by product category.

				
					function custom_product_add_to_cart_text() {
global $product;
$terms = get_the_terms( $product->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->slug; // $term->name works too
break;
}
switch($product_cat)
{
case 'category1' || 'category2' || 'category3'; // Subscription Products
return 'Sign Up Now'; break;
case 'category5'; // Variable Products
return 'Price Options'; break;
case 'category7'; // One-time payment Deals
return 'Buy Now'; break;
default;
return 'Add to Cart'; break;
}
}
add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_product_add_to_cart_text' );
				
			
If you have questions or need any help understanding and implementing these changes, please let me know at delice@wpquicksupport.com

Tools we are proud to recommend

Set an appointment with a specialist
to get the support you need.

Loading...
WP Quick Support strives to ensure the protection of your personal information through responsible practices. All personal information collected by WP Quick Support is done so exclusively with your consent, by means of a form posted on our website, an email received from you or by telephone. No information is collected automatically.