Add woocommerce support to theme
Following php code snippet should be added into functions.php of your active theme.
function dw_add_woocommerce_support() { add_theme_support( ‘woocommerce’ ); }
add_action( ‘after_setup_theme’, ‘dw_add_woocommerce_support’ );
Add extra field in woocommerce registration form
Sometimes, it is required to add an extra field in the woocommerce registration form. By default, woocommerce registration form requires only an email address. For example, if you want to make the input of a phone number mandatory while registration is done by the customer, you can add the following php code snippet to functions.php of your active theme.
function woocom_save_extra_register_fields($customer_id)
{
if (isset($_POST[“billing_phone”]))
{
update_user_meta($customer_id, “billing_phone”, sanitize_text_field($_POST[“billing_phone”]));
}
}
add_action(“woocommerce_created_customer”, “woocom_save_extra_register_fields”);
function woocom_validate_extra_register_fields( $username, $email, $validation_errors )
{
global $wpdb;
if (isset($_POST[“billing_phone”]) && empty($_POST[“billing_phone”]) )
{
$validation_errors->add(“billing_phone_error”, __(“Phone no. is required !”, “woocommerce”));
}
$results = $wpdb->get_results(‘select * from `wp_usermeta` where meta_key = “billing_phone” and meta_value = “‘.$_POST[“billing_phone”].'”‘);
if ( $results ) {
$validation_errors->add( ‘billing_phone_error’, __( ‘An account with this phone number is already registered.’, ‘woocommerce’ ) );
}
return $validation_errors;
}
add_action(“woocommerce_register_post”, “woocom_validate_extra_register_fields”, 10, 3);
function woocom_extra_register_fields()
{
$billing_phone = “”;
if ( ! empty( $_POST[‘billing_phone’] ) ) { $billing_phone = esc_attr_e( $_POST[‘billing_phone’] ); }
echo ‘<p class=”woocommerce-form-row woocommerce-form-row–wide form-row form-row-wide”>’;
echo ‘<label for=”billing_phone”>Phone No.<span class=”required”>*</span></label>’;
echo ‘<input type=”text” class=”input-text” name=”billing_phone” id=”billing_phone” value=”‘.$billing_phone.'”/>’;
echo ‘</p>’;
}
add_action( “woocommerce_register_form”, “woocom_extra_register_fields” );
Add star signs in product review woocommerce registration form
add_action(‘woocommerce_after_shop_loop_item’, ‘add_star_rating’ );
function add_star_rating()
{
global $woocommerce, $product;
$average = $product->get_average_rating();
echo ‘<div class=”star-rating”><span style=”width:’.( ( $average / 5 ) * 100 ) . ‘%”><strong itemprop=”ratingValue” class=”rating”>’.$average.'</strong> ‘.__( ‘out of 5’, ‘woocommerce’ ).'</span></div>’;
}
Display logged in user name in WordPress menu
Do not forget to create a menu item #profile_name# in the menu where the logged-in user name has to be displayed. The following php code snippet goes into the functions.php of the active theme.
function give_profile_name(){
$user=wp_get_current_user();
if(!is_user_logged_in())
$name = “Welcome GUEST”;
else
$name= “Welcome “.$user->user_login;
return $name;
}
add_shortcode(‘profile_name’, ‘give_profile_name’);
add_filter( ‘wp_nav_menu_objects’, ‘my_dynamic_menu_items’ );
function my_dynamic_menu_items( $menu_items ) {
foreach ( $menu_items as $menu_item ) {
if ( ‘#profile_name#’ == $menu_item->title ) {
global $shortcode_tags;
if ( isset( $shortcode_tags[‘profile_name’] ) ) {
$menu_item->title = call_user_func( $shortcode_tags[‘profile_name’] );
}
}
}
return $menu_items;
}
Remove menu link from my account page of user dashboard of woocommerce
For example, if you want to remove menu links to “Dashboard” and “Downloads” from the “My Account” page of the user dashboard for WooCommerce, please add the following code snippet to the functions.php file of your active theme:
add_filter ( ‘woocommerce_account_menu_items’, ‘dw_remove_my_account_links’ );
function dw_remove_my_account_links( $menu_links ){
unset( $menu_links[‘dashboard’] );
unset( $menu_links[‘downloads’] );
return $menu_links;
}