Custom Woo-Commerce functions to increase the functionality of your Woo-Commerce store. You will need to add the below codes in your child themes functions.php file in order for them to work
Rename my account page for not logged in users
In order for this code to work, make sure that My Account is spelt exactly the same on your menu as it is in the code below. You may change Login/Register to any text that you would like.
add_filter( 'wp_nav_menu_items', 'menu_item_label', 9999, 2 );
function menu_item_label( $items, $args ) {
if ( ! is_user_logged_in() ) {
$items = str_replace( "My Account", "Login/Register", $items );
}
return $items;
}
Add Shortcode after add to cart
I created a template using elementor and then used the template shortcode to display the below

add_action ( 'woocommerce_single_product_summary', 'add_shortcode', 40 );
function add_shortcode() {
global $product;
echo do_shortcode('insert shortcode here');
}
Rename the add to cart button if product already added to cart

//Rename the add to cart button on the Product page if product already added to cart
add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_add_cart_button' );
function product_add_cart_button( $label ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
if( get_the_ID() == $product->get_id() ) {
$label = __('Insert your text here', 'woocommerce');
}
}
return $label;
}
//Rename the add to cart button on the Shop page if product already added to cart
add_filter( 'woocommerce_product_add_to_cart_text', 'shop_add_cart_button', 99, 2 );
function shop_add_cart_button( $label, $product ) {
if ( $product->get_type() == 'simple' && $product->is_purchasable() && $product->is_in_stock() )
{
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if( get_the_ID() == $_product->get_id() ) {
$label = __('Insert your text here', 'woocommerce');
}
}
}
return $label;
}
Display Featured Products on WooCommerce Thank You Page
add_action( 'woocommerce_thankyou', 'upsells_thankyou' );
function upsells_thankyou() {
echo '<h2>You May Like</h2>';
echo do_shortcode( '[products limit="3" columns="1" visibility="featured" ]' );
}
Display Shipping Zones & Methods on WooCommerce Single Product Page
add_action( 'woocommerce_after_add_to_cart_form', 'shipping_rates_single_product' );
function shipping_rates_single_product() {
$zones = WC_Shipping_Zones::get_zones();
echo '<div><i class="fas fa-truck"></i> ' . __( 'Shipping', 'woocommerce' );
echo '<table>';
foreach ( $zones as $zone_id => $zone ) {
echo '<tr><td>';
echo $zone['zone_name'] . '</td><td>';
$zone_shipping_methods = $zone['shipping_methods'];
foreach ( $zone_shipping_methods as $index => $method ) {
$instance = $method->instance_settings;
$cost = $instance['cost'] ? $instance['cost'] : $instance['min_amount'];
echo $instance['title'] . ' ' . wc_price( $cost ) . '<br>';
}
echo '</td></tr>';
}
echo '</table></div>';
}
Set Min/Max Quantity
add_filter( 'woocommerce_quantity_input_args', 'bloomer_woocommerce_quantity_changes', 10, 2 );
function bloomer_woocommerce_quantity_changes( $args, $product ) {
if ( ! is_cart() ) {
$args['input_value'] = 4; // Start from this value (default = 1)
$args['max_value'] = 10; // Max quantity (default = -1)
$args['min_value'] = 4; // Min quantity (default = 0)
$args['step'] = 2; // Increment/decrement by this value (default = 1)
} else {
// Cart's "min_value" is already 0 and we don't need "input_value"
$args['max_value'] = 10; // Max quantity (default = -1)
$args['step'] = 2; // Increment/decrement by this value (default = 0)
// COMMENT OUT FOLLOWING IF STEP < MIN_VALUE
// $args['min_value'] = 4; // Min quantity (default = 0)
}
return $args;
}
// For variable products
add_filter( 'woocommerce_available_variation', 'bloomer_woocommerce_quantity_min_variation', 9999, 3 );
function bloomer_woocommerce_quantity_min_variation( $args, $product, $variation ) {
$args['min_qty'] = 5;
return $args;
}
Secure payment image on Checkout Page
add_action( 'woocommerce_review_order_after_submit', 'secure_payments' );
function secure_payments() {
echo '<img src="https://starliteaviation.online/wp-content/uploads/2022/08/payments-single.png" style="margin: 1em auto">';
}
Bonus: Add Privacy / Popi (South African) Policy Checkbox on WooCommerce My Account Registration Form
add_action( 'woocommerce_register_form', 'add_registration_privacy_policy', 11 );
function add_registration_privacy_policy() {
woocommerce_form_field( 'privacy_policy_reg', array(
'type' => 'checkbox',
'class' => array('form-row privacy'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true,
'label' => 'I\'ve read and accept the <a href="add your website privacy policy link">Privacy Policy</a>',
));
}
// Show error if user does not tick
add_filter( 'woocommerce_registration_errors', 'validate_privacy_registration', 10, 3 );
function validate_privacy_registration( $errors, $username, $email ) {
if ( ! is_checkout() ) {
if ( ! (int) isset( $_POST['privacy_policy_reg'] ) ) {
$errors->add( 'privacy_policy_reg_error', __( 'Privacy Policy Consent is Required!', 'woocommerce' ) );
}
}
return $errors;
}
Remember to subscribe to my LinkedIn Newsletter.
