function aviweb_renaming_order_status( $order_statuses ) { foreach ( $order_statuses as $key => $status ) { if ( 'wc-processing' === $key ) { $order_statuses['wc-processing'] = _x( 'In progress', 'Order status', 'woocommerce' ); } if ( 'wc-completed' === $key ) { $order_statuses['wc-completed'] = _x( 'Delivered', 'Order status', 'woocommerce' ); } } return $order_statuses; } add_filter( 'wc_order_statuses', 'aviweb_renaming_order_status' );
woocommerce
Get WooCommerce order information like billing, shipping & date.
$order_id = 1234; $order_data = new WC_Order($order_id); //Order Date $order_date = $order_data->order_date; //Billing information $billing = $order_data->get_address('billing'); //Shipping information $shipping = $order_data->get_address('shipping');
Get all WooCommerce processing and completed order.
global $wpdb; $orders = $wpdb->get_results( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'shop_order' AND post_status in ('wc-completed', 'wc-processing')");
Allow only one product into woocommerce cart
add_filter( ‘woocommerce_add_to_cart_validation’, ‘aviweb_only_one_in_cart’, 99, 2 );
function aviweb_only_one_in_cart( $passed, $added_product_id ) {
// empty cart and new item will replace previous
wc_empty_cart();
return $passed;
}
Disable the Password Strength in WooCommerce
function aviweb_remove_password_strength() {
wp_dequeue_script( ‘wc-password-strength-meter’ );
}
add_action( ‘wp_print_scripts’, ‘aviweb_remove_password_strength’, 10 );
How to check selected product in previous customer orders or not
$seletedProduct = 25;
$exist = ‘no’;
if(is_user_logged_in()){ // Check for customer login
$orders = get_posts( array(
‘numberposts’ => -1,
‘meta_key’ => ‘_customer_user’,
‘meta_value’ => get_current_user_id(),
‘post_type’ => ‘shop_order’,
‘post_status’ => array(‘wc-completed’, ‘wc-processing’)
) );
foreach ($orders as $order) {
$WC_Order = new WC_Order($order->ID);
$items = $WC_Order->get_items();
foreach ( $items as $item ) {
if($item[‘product_id’] == $seletedProduct){
$exist = ‘yes’;
}
}
}
}
if($exist == ‘no’){
echo ‘Product not ordered’;
}else{
echo ‘Product already ordered’;
}
Sort Reviews by date on Woocommerce
add_filter( ‘woocommerce_product_review_list_args’, ‘aviweb_reviews_first’ );
function aviweb_reviews_first($args) {
$args[‘reverse_top_level’] = true;
return $args;
}
Remove shipping method from WooCommerce cart programmatically
add_filter( ‘woocommerce_package_rates’, ‘aviweb_hide_free_shipping_class’, 10, 2 );
function aviweb_hide_free_shipping_class( $rates, $package ) {
echo $in_cart = false;
foreach( WC()->cart->cart_contents as $key => $values ) {
$product_id = $values[‘product_id’];
if ( $product_id == 2 ) {
$in_cart = true;
break;
}
}
if( $in_cart ) {
unset( $rates[‘flat_rate:7’] );
}
return $rates;
}
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;
}
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’ );