User Registration
Custom fields for user Registration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| function custom_woocommerce_registration_fields() {
?>
<p class="form-row form-row-wide">
<label for="reg_first_name"><?php esc_html_e( 'First Name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="first_name" id="reg_first_name" value="<?php if ( ! empty( $_POST['first_name'] ) ) echo esc_attr( wp_unslash( $_POST['first_name'] ) ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_last_name"><?php esc_html_e( 'Last Name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="last_name" id="reg_last_name" value="<?php if ( ! empty( $_POST['last_name'] ) ) echo esc_attr( wp_unslash( $_POST['last_name'] ) ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_phone"><?php esc_html_e( 'Phone Number', 'woocommerce' ); ?></label>
<input type="text" class="input-text" name="phone" id="reg_phone" value="<?php if ( ! empty( $_POST['phone'] ) ) echo esc_attr( wp_unslash( $_POST['phone'] ) ); ?>" />
</p>
<?php
}
add_action( 'woocommerce_register_form', 'custom_woocommerce_registration_fields' );
|
Validate Fields
1
2
3
4
5
6
7
8
9
10
| function custom_woocommerce_validate_fields( $errors, $username, $email ) {
if ( empty( $_POST['first_name'] ) ) {
$errors->add( 'first_name_error', __( 'First name is required!', 'woocommerce' ) );
}
if ( empty( $_POST['last_name'] ) ) {
$errors->add( 'last_name_error', __( 'Last name is required!', 'woocommerce' ) );
}
return $errors;
}
add_filter( 'woocommerce_registration_errors', 'custom_woocommerce_validate_fields', 10, 3 );
|
Save Fields
Save fields to the user meta
1
2
3
4
5
6
7
8
9
10
11
12
| function custom_woocommerce_save_fields( $customer_id ) {
if ( isset( $_POST['first_name'] ) ) {
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['first_name'] ) );
}
if ( isset( $_POST['last_name'] ) ) {
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['last_name'] ) );
}
if ( isset( $_POST['phone'] ) ) {
update_user_meta( $customer_id, 'phone', sanitize_text_field( $_POST['phone'] ) );
}
}
add_action( 'woocommerce_created_customer', 'custom_woocommerce_save_fields' );
|
Display fields in user admin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| function custom_woocommerce_show_fields( $user ) {
?>
<h3><?php esc_html_e( 'Custom Registration Fields', 'woocommerce' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="first_name"><?php esc_html_e( 'First Name', 'woocommerce' ); ?></label></th>
<td><input type="text" name="first_name" id="first_name" value="<?php echo esc_attr( get_user_meta( $user->ID, 'first_name', true ) ); ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label for="last_name"><?php esc_html_e( 'Last Name', 'woocommerce' ); ?></label></th>
<td><input type="text" name="last_name" id="last_name" value="<?php echo esc_attr( get_user_meta( $user->ID, 'last_name', true ) ); ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label for="phone"><?php esc_html_e( 'Phone Number', 'woocommerce' ); ?></label></th>
<td><input type="text" name="phone" id="phone" value="<?php echo esc_attr( get_user_meta( $user->ID, 'phone', true ) ); ?>" class="regular-text" /></td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'custom_woocommerce_show_fields' );
add_action( 'edit_user_profile', 'custom_woocommerce_show_fields' );
|
Get other templates
(e.g. product attributes) passing attributes and including the file.
Get Product ID
1
| $product->get_id(); (fixes the error: "Notice: id was called incorrectly. Product properties should not be accessed directly")
|
usage
1
2
| global $product;
$id = $product->get_id();
|
Get Product General Info
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| $product->get_type();
$product->get_name();
$product->get_slug();
$product->get_date_created();
$product->get_date_modified();
$product->get_status();
$product->get_featured();
$product->get_catalog_visibility();
$product->get_description();
$product->get_short_description();
$product->get_sku();
$product->get_menu_order();
$product->get_virtual();
get_permalink( $product->get_id() );
|
Get Product Prices
1
2
3
4
5
6
| $product->get_price();
$product->get_regular_price();
$product->get_sale_price();
$product->get_date_on_sale_from();
$product->get_date_on_sale_to();
$product->get_total_sales();
|
Get Product Tax, Shipping & Stock
1
2
3
4
5
6
7
8
9
| $product->get_tax_status();
$product->get_tax_class();
$product->get_manage_stock();
$product->get_stock_quantity();
$product->get_stock_status();
$product->get_backorders();
$product->get_sold_individually();
$product->get_purchase_note();
$product->get_shipping_class_id();
|
Get Product Dimensions
1
2
3
4
5
| $product->get_weight();
$product->get_length();
$product->get_width();
$product->get_height();
$product->get_dimensions();
|
Get Linked Products
1
2
3
| $product->get_upsell_ids();
$product->get_cross_sell_ids();
$product->get_parent_id();
|
Get Product Variations
1
2
| $product->get_attributes();
$product->get_default_attributes();
|
In this case you will need to loop through all the items present in the order, and then apply the rules above.
Get $product object from $order / $order_id
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| $order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product = wc_get_product( $item['product_id'] );
// Now you have access to (see above)...
$product->get_type();
$product->get_name();
// etc.
// etc.
}
|
Get Product Taxonomies
1
2
3
| $product->get_categories();
$product->get_category_ids();
$product->get_tag_ids();
|
Get Product Downloads
1
2
3
4
| $product->get_downloads();
$product->get_download_expiry();
$product->get_downloadable();
$product->get_download_limit();
|
Get Product Images
1
2
3
| $product->get_image_id();
$product->get_image();
$product->get_gallery_image_ids();
|
Get Product Reviews
1
2
3
4
| $product->get_reviews_allowed();
$product->get_rating_counts();
$product->get_average_rating();
$product->get_review_count();
|
Product object
If you have access to the product ID (usually the do_action
or apply_filters
will make this possible to you), you have to get the product object first. Then, do the exact same things as above.
1
2
3
4
5
6
7
8
9
10
11
|
// Get $product object from product ID
$product = wc_get_product( $product_id );
// Now you have access to (see above)...
$product->get_type();
$product->get_name();
// etc.
// etc.
|
Cart object
How to get the product information inside the Cart? In this case, once again, you will need to loop through all the items present in the cart, and then apply the rules above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| // Get $product object from Cart object
$cart = WC()->cart->get_cart();
foreach( $cart as $cart_item ){
$product = wc_get_product( $cart_item'[product_id'] );
// Now you have access to (see above)...
$product->get_type(;
$product->)get_name();
// etc.
// etc.
}
|
Remove product sorting default dropdown
1
2
3
4
5
| add_action('init','delay_remove');
function delay_remove() {
remove_action( 'woocommerce_after_shop_loop', 'woocommerce_catalog_ordering', 10 );
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 10 );
}
|
Create account by default in woocommerce checkout.
1
2
3
| add_filter('woocommerce_create_account_default_checked' , function ($checked){
return true;
});
|
Remove shipping from cart page
1
2
3
4
5
6
7
| function disable_shipping_calc_on_cart( $show_shipping ) {
if( is_cart() ) {
return false;
}
return $show_shipping;
}
add_filter( 'woocommerce_cart_ready_to_calc_shipping', 'disable_shipping_calc_on_cart', 99 );
|
Links