Enter your keyword

Woocommerce

How to add Adding Registration fields to the Woocommerce Registration form

 //Add Registration fields to the form 
add_filter( 'register_form', 'adding_custom_registration_fields' );
function adding_custom_registration_fields( ) {
        echo '<div class="form-row form-row-wide"><label for="reg_role">'.__('Role', 'woocommerce').' <span class="required">*</span></label>';
        echo '<select name="wp_capabilities">
        <option value="customer">Customer</option>
        <option value="designer">Designer</option>
        </select>';
        echo '</div>';
 }
  
//Validation registration form  after submission using the filter registration_errors
add_filter('registration_errors', 'registration_errors_validation', 10,3);
function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
    global $woocommerce;
    extract($_POST); // extracting $_POST into separate variables
    if($wp_capabilities == '' ) {
    $woocommerce->add_error( __( 'Please, fill in all the required fields.', 'woocommerce' ) );
    }
    return $reg_errors;
}
//Updating use meta after registration successful registration
add_action('woocommerce_created_customer','adding_extra_reg_fields');
function adding_extra_reg_fields($user_id) {
extract($_POST);
// update user role using wordpress function
wp_update_user( array ('ID' => $user_id, 'role' => $wp_capabilities ) ) ;
}

How to Add/Remove New Columns on wp-admin Products Listing Page

// ADD NEW COLUMN
add_filter( 'manage_edit-product_columns', 'show_product_columns',15 );
function show_product_columns($columns){
//remove column
        unset($columns['sku']);
        unset($columns['product_tag']);
        unset($columns['product_type']);
//add column
        $columns['author'] = __( 'Author');
        return $columns;
}
add_action( 'manage_product_posts_custom_column', 'product_column_author', 10, 2 );
function product_column_author( $column, $postid ) {
    if ( $column == 'author' ) {
    echo $postid;
    }
}

How to add New Field in General Settings in WordPress

add_filter('admin_init', 'my_general_settings_on_dswebsolution');
function my_general_settings_on_dswebsolution()
{
    register_setting('general', 'general_field_dswebsolution', 'esc_attr');
    add_settings_field('general_field_dswebsolution', '<label for="general_field_dswebsolution">'.__('General Field Name' , 'general_field_dswebsolution' ).'</label>' , 'my_general_settings_dswebsolution_html', 'general');
}
function my_general_settings_dswebsolution_html()
{
    $value = get_option( 'general_field_dswebsolution', '' );
    echo '<input type="text" id="general_field_dswebsolution" name="general_field_dswebsolution" value="' . $value . '" />';
}

How to get User Role in WordPress

 function get_user_role() {
     global $current_user;
     $user_roles = $current_user->roles;
     $user_role = array_shift($user_roles);
     return $user_role; 
 }

How to Add a custom user role in WordPress

$result = add_role( 'designer', __('Designer' ),
	array(
    	    'read' => true, // true allows this capability
            'edit_posts' => true, // Allows user to edit their own posts
            'edit_pages' => true, // Allows user to edit pages
            'edit_others_posts' => true, // Allows user to edit others posts not just their own
            'create_posts' => true, // Allows user to create new posts
            'manage_categories' => true, // Allows user to manage post categories
            'publish_posts' => true, // Allows the user to publish, otherwise posts stays in draft mode
            'edit_themes' => false, // false denies this capability. User can't edit your theme
            'install_plugins' => false, // User cant add new plugins
            'update_plugin' => false, // User can't update any plugins
            'update_core' => false // user cant perform core updates
            )
       );

How to add Custom Status in Post/Custom Post Type in WordPress

function unapproved_post_status(){
    register_post_status( 'unapproved', array(
    'label'                     => _x( 'Unapproved', 'post' ),
    'public'                    => true,
    'exclude_from_search'       => false,
    'show_in_admin_all_list'    => true,
    'show_in_admin_status_list' => true,
    'label_count'               => _n_noop('Unapproved <span class="count">(%s)</span>', 'Unapproved <span class="count">(%s)</span>' ), ) );
}
add_action( 'init', 'unapproved_post_status' );

How to change Woocommerce Currency Symbol

add_filter( 'woocommerce_currencies', 'add_inr_currency');
add_filter( 'woocommerce_currency_symbol', 'add_inr_currency_symbol');
function add_inr_currency( $currencies ) {
$currencies['INR'] = 'INR';
return $currencies;
}
function add_inr_currency_symbol( $symbol ) {
$currency = get_option( 'woocommerce_currency' );
switch( $currency ) {
case 'INR': $symbol = 'Rs.'; break;
}
return $symbol;
}

How to Disable Search Feature in WordPress

function fb_filter_query( $query, $error = true ) {
if (is_search()){
$query->is_search = false; 
$query->query_vars[s] = false; 
$query->query[s] = false;    // to error 
if ( $error == true )  $query->is_404 = true;  
}
}
add_action( 'parse_query', 'fb_filter_query'); 
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );

Replace the WooCommerce default page Navigation

If you need to then manually replace the WooCommerce default page navigation then add this code to your current themes functions.php file:

/**
* Replace WooCommerce Default Pagination with WP-PageNavi Pagination
*
* @author WPSnacks.com
* @link http://www.wpsnacks.com
*/
remove_action('woocommerce_pagination', 'woocommerce_pagination', 10);
function woocommerce_pagination() {
wp_pagenavi();
}
add_action( 'woocommerce_pagination', 'woocommerce_pagination', 10);

WP-PageNavi

Show cart contents/total in WooCommerce

To display the cart contents and total in your template use something like:

<a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>

To ajaxify your cart viewer so it updates when an item is added (via ajax) use:

// Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php)
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
  ob_start();
  ?>
  <a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a> 
  <?php  
  $fragments['a.cart-contents'] = ob_get_clean();
  return $fragments;
}

Change the `add to cart` text on product archives in WooCommerce

add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' );                        // < 2.1
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_custom_cart_button_text' );    // 2.1 +
function woo_custom_cart_button_text() {
        return __( 'My Button Text', 'woocommerce' );
}

Customise `add to cart` text on single product pages in WooCommerce

Add the following to your functions.php file replacing 'my button text' with whatever you want it to say:

add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' );                                // < 2.1
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );    // 2.1 +
function woo_custom_cart_button_text() {
        return __( 'Buy Now', 'woocommerce' );
}

How to automatically complete orders in WooCommerce

Automatically mark orders for virtual products as Completed after a successful payment by adding this snippet into your functions.php:

/**
* Auto Complete all WooCommerce orders.
* Add to theme functions.php file
*/

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
    global $woocommerce;
    if ( !$order_id )
        return;
    $order = new WC_Order( $order_id );
    $order->update_status( 'completed' );
}

How to change Registration page in wordpress

function ds_change_register_link($link, $action=null, $query=null)
{
    $parsed_link = parse_url($link);
    if ($parsed_link['query'] == 'action=register') {
        $link = site_url().'/registration/';
    }
    return $link;
}
add_filter('tml_page_link', 'ds_change_register_link');

Search / Filter posts on Title/Content/Taxonomy and Tags

<?php 
$searchkeys = 'searchkeyword';
global $wpdb;
$querysearch=" SELECT * FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->term_taxonomy, $wpdb->terms WHERE ($wpdb->terms.name = '$searchkeys' OR $wpdb->posts.post_content LIKE '%$searchkeys%' OR $wpdb->posts.post_title LIKE '%$searchkeys%') AND $wpdb->posts.post_type = 'post' AND $wpdb->posts.ID = $wpdb->term_relationships.object_id AND  $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id ORDER BY $wpdb->posts.post_date DESC";

$get_results = $wpdb->get_results($querysearch, OBJECT_K);
?>

Custom Shortcode For Call WordPress Menu

function menu($atts, $content = null)
    {
        extract(shortcode_atts(array(
                'theme_location' => '',
                'menu' => '',
                'container' => 'div',
                'container_class' => '',
                'container_id' => '',
                'menu_class' => 'menu',
                'menu_id' => '',
                'echo' => true,
                'fallback_cb' => 'wp_page_menu',
                'before' => '',
                'after' => '',
                'link_before' => '',
                'link_after' => '',
                'depth' => 0,
                'walker' => ''
        ) , $atts));
        return wp_nav_menu(array(
                'theme_location' => $theme_location,
                'menu' => $menu,
                'container' => $container,
                'container_class' => $container_class,
                'container_id' => $container_id,
                'menu_class' => $menu_class,
                'menu_id' => $menu_id,
                'echo' => false,
                'fallback_cb' => $fallback_cb,
                'before' => $before,
                'after' => $after,
                'link_before' => $link_before,
                'link_after' => $link_after,
                'depth' => $depth,
                'walker' => $walker
        ));
    }
// Create the shortcode 
add_shortcode("menu", "menu");
[menu menu=Top Navigation menu_class=main_menu]  // Here "Top Navigation" is Menu Name

Remove Particular Body Class in WordPress

add_filter('body_class', 'remove_body_class', 20, 2);
function remove_body_class($wp_classes)
    {
        foreach($wp_classes as $key =--> $value)
            {
                if ($value == 'right-sidebar') unset($wp_classes[$key]);
            }
        return $wp_classes;
    }