Remove states from woo-commerce checkout form

Please put below code in functions.php
add_filter( ‘woocommerce_states’, ‘aviweb_remove_states’ );

function aviweb_remove_states( $states ) {
unset($states[‘US’][‘AK’]);
unset($states[‘US’][‘HI’]);
return $states;
}

Advertisements

Add new extra fields for WooCommerce registration with role set

add_action( ‘woocommerce_register_form_start’, ‘aviweb_extra_register_fields’ );
/**
*
* Validate the extra register fields.
*
*/
function aviweb_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST[‘user_type’] ) && empty( $_POST[‘user_type’] ) ) {
$validation_errors->add( ‘user_type_error’, __( ‘<strong>Error</strong>: User type is required!.’, ‘woocommerce’ ) );
}
}
add_action( ‘woocommerce_register_post’, ‘aviweb_validate_extra_register_fields’, 10, 3 );
/**
*
* Save the extra register fields.
*
*/
function aviweb_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST[‘user_type’] ) ) {
update_user_meta( $customer_id, ‘user_type’, sanitize_text_field( $_POST[‘user_type’] ) );
$my_user = new WP_User( $customer_id );
if($_POST[‘user_type’] == “subscriber”){
$my_user->set_role( “subscriber” );
}else if($_POST[‘user_type’] == “customer”){
$my_user->set_role( “customer” );
}
}
}
add_action( ‘woocommerce_created_customer’, ‘aviweb_save_extra_register_fields’ );

Restrict specific products in Woo-Commerce shop

Put below code in functions.php

add_action( ‘woocommerce_product_query’, ‘aviweb_product_query’ );add_action( ‘woocommerce_product_query’, ‘aviweb_product_query’ );
function aviweb_product_query( $q_array ){

$product_ids_on_sale = array(); $product_ids_on_sale[] = 7076;

$q_array->set( ‘post__not_in’, $product_ids_on_sale );

}

post__in -> products within array

post__not_in -> products not in array

Change order of billing fields on woo-commerce checkout page

We can change fields order based on priority please find below code to change FIELD ORDER

add_filter(“woocommerce_checkout_fields”, “order_fields”);

function order_fields($fields) {

$fields[‘billing’][‘billing_first_name’][‘priority’] = 1;
$fields[‘billing’][‘billing_last_name’][‘priority’] = 2;
$fields[‘billing’][‘billing_company’][‘priority’] = 3;
$fields[‘billing’][‘billing_email’][‘priority’] = 4;
$fields[‘billing’][‘billing_phone’][‘priority’] = 5;
$fields[‘billing’][‘billing_country’][‘priority’] = 6;
$fields[‘billing’][‘billing_address_1’][‘priority’] = 7;
$fields[‘billing’][‘billing_address_2’][‘priority’] = 8;
$fields[‘billing’][‘billing_city’][‘priority’] = 9;
$fields[‘billing’][‘billing_state’][‘priority’] = 10;
$fields[‘billing’][‘billing_postcode’][‘priority’] = 11;

return $fields;
}